Search in sources :

Example 51 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 52 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)

Example 53 with Document

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

the class HtmlUnknownTagInspection method createOptionsPanel.

@NotNull
protected static JComponent createOptionsPanel(@NotNull final HtmlUnknownElementInspection inspection) {
    final JPanel result = new JPanel(new BorderLayout());
    final JPanel internalPanel = new JPanel(new BorderLayout());
    result.add(internalPanel, BorderLayout.NORTH);
    final Ref<FieldPanel> panelRef = new Ref<>();
    final FieldPanel additionalAttributesPanel = new FieldPanel(null, null, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent event) {
            Messages.showTextAreaDialog(panelRef.get().getTextField(), StringUtil.wordsToBeginFromUpperCase(inspection.getPanelTitle()), inspection.getClass().getSimpleName(), s -> reparseProperties(s), strings -> StringUtil.join(strings, ","));
        }
    }, null);
    ((JButton) additionalAttributesPanel.getComponent(1)).setIcon(PlatformIcons.OPEN_EDIT_DIALOG_ICON);
    panelRef.set(additionalAttributesPanel);
    additionalAttributesPanel.getTextField().getDocument().addDocumentListener(new DocumentAdapter() {

        @Override
        protected void textChanged(DocumentEvent e) {
            final Document document = e.getDocument();
            try {
                final String text = document.getText(0, document.getLength());
                if (text != null) {
                    inspection.updateAdditionalEntries(text.trim());
                }
            } catch (BadLocationException e1) {
                inspection.getLogger().error(e1);
            }
        }
    });
    final JCheckBox checkBox = new JCheckBox(inspection.getCheckboxTitle());
    checkBox.setSelected(inspection.isCustomValuesEnabled());
    checkBox.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            final boolean b = checkBox.isSelected();
            if (b != inspection.isCustomValuesEnabled()) {
                inspection.enableCustomValues(b);
                additionalAttributesPanel.setEnabled(inspection.isCustomValuesEnabled());
            }
        }
    });
    internalPanel.add(checkBox, BorderLayout.NORTH);
    internalPanel.add(additionalAttributesPanel, BorderLayout.CENTER);
    additionalAttributesPanel.setPreferredSize(new Dimension(150, additionalAttributesPanel.getPreferredSize().height));
    additionalAttributesPanel.setEnabled(inspection.isCustomValuesEnabled());
    additionalAttributesPanel.setText(inspection.getAdditionalEntries());
    return result;
}
Also used : ActionListener(java.awt.event.ActionListener) TemplateLanguageFileViewProvider(com.intellij.psi.templateLanguages.TemplateLanguageFileViewProvider) NonNls(org.jetbrains.annotations.NonNls) ConfigurableTemplateLanguageFileViewProvider(com.intellij.psi.templateLanguages.ConfigurableTemplateLanguageFileViewProvider) TemplateLanguageUtil(com.intellij.psi.templateLanguages.TemplateLanguageUtil) Nls(org.jetbrains.annotations.Nls) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) Messages(com.intellij.openapi.ui.Messages) DocumentEvent(javax.swing.event.DocumentEvent) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) PlatformIcons(com.intellij.util.PlatformIcons) StringUtil(com.intellij.openapi.util.text.StringUtil) FileViewProvider(com.intellij.psi.FileViewProvider) FieldPanel(com.intellij.ui.FieldPanel) BadLocationException(javax.swing.text.BadLocationException) LocalQuickFixOnPsiElement(com.intellij.codeInspection.LocalQuickFixOnPsiElement) ActionEvent(java.awt.event.ActionEvent) java.awt(java.awt) ChangeTemplateDataLanguageAction(com.intellij.psi.templateLanguages.ChangeTemplateDataLanguageAction) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Function(com.intellij.util.Function) Document(javax.swing.text.Document) NotNull(org.jetbrains.annotations.NotNull) LangBundle(com.intellij.lang.LangBundle) Ref(com.intellij.openapi.util.Ref) DocumentAdapter(com.intellij.ui.DocumentAdapter) javax.swing(javax.swing) ActionEvent(java.awt.event.ActionEvent) DocumentAdapter(com.intellij.ui.DocumentAdapter) DocumentEvent(javax.swing.event.DocumentEvent) Document(javax.swing.text.Document) Ref(com.intellij.openapi.util.Ref) ActionListener(java.awt.event.ActionListener) FieldPanel(com.intellij.ui.FieldPanel) BadLocationException(javax.swing.text.BadLocationException) NotNull(org.jetbrains.annotations.NotNull)

Example 54 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 55 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)

Aggregations

Document (javax.swing.text.Document)64 BadLocationException (javax.swing.text.BadLocationException)29 DocumentEvent (javax.swing.event.DocumentEvent)10 DefaultStyledDocument (javax.swing.text.DefaultStyledDocument)8 DocumentAdapter (com.intellij.ui.DocumentAdapter)7 HTMLDocument (javax.swing.text.html.HTMLDocument)7 ActionEvent (java.awt.event.ActionEvent)5 PlainDocument (javax.swing.text.PlainDocument)5 ActionListener (java.awt.event.ActionListener)4 File (java.io.File)3 IOException (java.io.IOException)3 DocumentListener (javax.swing.event.DocumentListener)3 Element (javax.swing.text.Element)3 FieldPanel (com.intellij.ui.FieldPanel)2 CheckBox (com.intellij.util.ui.CheckBox)2 NumberFormat (java.text.NumberFormat)2 ParseException (java.text.ParseException)2 AbstractAction (javax.swing.AbstractAction)2 AbstractDocument (javax.swing.text.AbstractDocument)2 SimpleAttributeSet (javax.swing.text.SimpleAttributeSet)2