Search in sources :

Example 91 with Document

use of javax.swing.text.Document in project intellij-community by JetBrains.

the class FileTextFieldImpl method processChosenFromCompletion.

private void processChosenFromCompletion(boolean nameOnly) {
    final LookupFile file = getSelectedFileFromCompletionPopup();
    if (file == null)
        return;
    if (nameOnly) {
        try {
            final Document doc = myPathTextField.getDocument();
            int caretPos = myPathTextField.getCaretPosition();
            if (myFinder.getSeparator().equals(doc.getText(caretPos, 1))) {
                for (; caretPos < doc.getLength(); caretPos++) {
                    final String eachChar = doc.getText(caretPos, 1);
                    if (!myFinder.getSeparator().equals(eachChar))
                        break;
                }
            }
            int start = caretPos > 0 ? caretPos - 1 : caretPos;
            while (start >= 0) {
                final String each = doc.getText(start, 1);
                if (myFinder.getSeparator().equals(each)) {
                    start++;
                    break;
                }
                start--;
            }
            int end = start < caretPos ? caretPos : start;
            while (end <= doc.getLength()) {
                final String each = doc.getText(end, 1);
                if (myFinder.getSeparator().equals(each)) {
                    break;
                }
                end++;
            }
            if (end > doc.getLength()) {
                end = doc.getLength();
            }
            if (start > end || start < 0 || end > doc.getLength()) {
                setTextToFile(file);
            } else {
                replacePathComponent(file, caretPos, start, end);
            }
        } catch (BadLocationException e) {
            LOG.error(e);
        }
    } else {
        setTextToFile(file);
    }
}
Also used : Document(javax.swing.text.Document) BadLocationException(javax.swing.text.BadLocationException)

Example 92 with Document

use of javax.swing.text.Document in project intellij-community by JetBrains.

the class BaseInspection method prepareNumberEditor.

protected JFormattedTextField prepareNumberEditor(@NonNls final String fieldName) {
    final NumberFormat formatter = NumberFormat.getIntegerInstance();
    formatter.setParseIntegerOnly(true);
    final JFormattedTextField valueField = new JFormattedTextField(formatter);
    Object value = ReflectionUtil.getField(getClass(), this, null, fieldName);
    valueField.setValue(value);
    valueField.setColumns(2);
    // hack to work around text field becoming unusably small sometimes when using GridBagLayout
    valueField.setMinimumSize(valueField.getPreferredSize());
    UIUtil.fixFormattedField(valueField);
    final Document document = valueField.getDocument();
    document.addDocumentListener(new DocumentAdapter() {

        @Override
        public void textChanged(DocumentEvent evt) {
            try {
                valueField.commitEdit();
                final Number number = (Number) valueField.getValue();
                ReflectionUtil.setField(BaseInspection.this.getClass(), BaseInspection.this, int.class, fieldName, number.intValue());
            } catch (ParseException e) {
            // No luck this time. Will update the field when correct value is entered.
            }
        }
    });
    return valueField;
}
Also used : DocumentAdapter(com.intellij.ui.DocumentAdapter) ParseException(java.text.ParseException) Document(javax.swing.text.Document) DocumentEvent(javax.swing.event.DocumentEvent) NumberFormat(java.text.NumberFormat)

Example 93 with Document

use of javax.swing.text.Document in project ACS by ACS-Community.

the class FeedbackArea method append.

protected void append(String feedback) {
    Document doc = outputArea.getDocument();
    if (doc == null) {
        return;
    }
    try {
        doc.insertString(doc.getLength(), feedback, null);
    } catch (BadLocationException exc) {
    }
    int newCaretPosition = outputArea.getCaretPosition();
    int newLength = doc.getLength();
    int tooMuch = Math.max(newLength - maxLength, 0);
    if (newLength > maxLength) {
        newCaretPosition -= (newLength - maxLength);
    }
    int caretPosition;
    if (!scrollLock)
        caretPosition = doc.getLength() - tooMuch;
    else {
        if (newCaretPosition >= 0)
            caretPosition = newCaretPosition;
        else
            caretPosition = 0;
    }
    outputArea.setCaretPosition(caretPosition);
    if (newLength > maxLength) {
        try {
            doc.remove(0, newLength - maxLength);
        } catch (BadLocationException exc1) {
        }
    }
}
Also used : PlainDocument(javax.swing.text.PlainDocument) Document(javax.swing.text.Document) BadLocationException(javax.swing.text.BadLocationException)

Example 94 with Document

use of javax.swing.text.Document in project joda-time by JodaOrg.

the class AgeCalculator method addTopArea.

private void addTopArea(Container container) {
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    panel.add(fixedHeight(new JLabel("Birthdate")));
    panel.add(Box.createHorizontalStrut(10));
    final JTextField birthdateField = new JTextField(iBirthdateStr + ' ');
    Document doc = birthdateField.getDocument();
    doc.addDocumentListener(new DocumentListener() {

        public void insertUpdate(DocumentEvent e) {
            update(e);
        }

        public void removeUpdate(DocumentEvent e) {
            update(e);
        }

        public void changedUpdate(DocumentEvent e) {
            update(e);
        }

        private void update(DocumentEvent e) {
            iBirthdateStr = birthdateField.getText();
            updateResults();
        }
    });
    panel.add(fixedHeight(birthdateField));
    panel.add(Box.createHorizontalStrut(10));
    Object[] ids = DateTimeZone.getAvailableIDs().toArray();
    final JComboBox zoneSelector = new JComboBox(ids);
    zoneSelector.setSelectedItem(DateTimeZone.getDefault().getID());
    panel.add(fixedSize(zoneSelector));
    zoneSelector.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            String id = (String) zoneSelector.getSelectedItem();
            iChronology = ISOChronology.getInstance(DateTimeZone.forID(id));
            updateResults();
        }
    });
    container.add(fixedHeight(panel));
}
Also used : JPanel(javax.swing.JPanel) DocumentListener(javax.swing.event.DocumentListener) JComboBox(javax.swing.JComboBox) ActionEvent(java.awt.event.ActionEvent) BoxLayout(javax.swing.BoxLayout) JLabel(javax.swing.JLabel) JTextField(javax.swing.JTextField) Document(javax.swing.text.Document) DocumentEvent(javax.swing.event.DocumentEvent) ActionListener(java.awt.event.ActionListener)

Example 95 with Document

use of javax.swing.text.Document in project intellij-community by JetBrains.

the class IpnbEditablePanel method getText.

public String getText(int from) {
    if (myEditing && myEditableTextArea != null) {
        final Document document = myEditableTextArea.getDocument();
        final int to = document.getLength();
        return getText(from, to);
    }
    return null;
}
Also used : Document(javax.swing.text.Document) RelativePoint(com.intellij.ui.awt.RelativePoint)

Aggregations

Document (javax.swing.text.Document)170 BadLocationException (javax.swing.text.BadLocationException)85 DocumentEvent (javax.swing.event.DocumentEvent)15 DefaultStyledDocument (javax.swing.text.DefaultStyledDocument)15 HTMLDocument (javax.swing.text.html.HTMLDocument)15 Element (javax.swing.text.Element)14 AbstractDocument (javax.swing.text.AbstractDocument)11 StringReader (java.io.StringReader)10 PlainDocument (javax.swing.text.PlainDocument)10 DocumentAdapter (com.intellij.ui.DocumentAdapter)8 Matcher (java.util.regex.Matcher)8 JTextComponent (javax.swing.text.JTextComponent)8 ActionEvent (java.awt.event.ActionEvent)7 DocumentListener (javax.swing.event.DocumentListener)7 HTMLEditorKit (javax.swing.text.html.HTMLEditorKit)7 IOException (java.io.IOException)6 PatternSyntaxException (java.util.regex.PatternSyntaxException)6 Highlighter (javax.swing.text.Highlighter)5 FieldDefinition (com.revolsys.record.schema.FieldDefinition)4 Point (java.awt.Point)4