Search in sources :

Example 46 with Document

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

the class ClassHasNoToStringMethodInspection method createOptionsPanel.

/**
     * Creates the options panel in the settings for user changeable options.
     *
     * @return the options panel
     */
@Override
public JComponent createOptionsPanel() {
    JPanel panel = new JPanel(new GridBagLayout());
    GridBagConstraints constraints = new GridBagConstraints();
    constraints.gridx = 0;
    constraints.gridy = 0;
    constraints.weightx = 0.0;
    constraints.anchor = GridBagConstraints.WEST;
    constraints.fill = GridBagConstraints.NONE;
    panel.add(new JLabel("Exclude classes (reg exp):"), constraints);
    final JFormattedTextField excludeClassNamesField = new JFormattedTextField(new RegExFormatter());
    excludeClassNamesField.setValue(excludeClassNamesPattern);
    excludeClassNamesField.setColumns(25);
    excludeClassNamesField.setInputVerifier(new RegExInputVerifier());
    excludeClassNamesField.setFocusLostBehavior(JFormattedTextField.COMMIT);
    excludeClassNamesField.setMinimumSize(excludeClassNamesField.getPreferredSize());
    UIUtil.fixFormattedField(excludeClassNamesField);
    Document document = excludeClassNamesField.getDocument();
    document.addDocumentListener(new DocumentAdapter() {

        @Override
        protected void textChanged(DocumentEvent e) {
            try {
                excludeClassNamesField.commitEdit();
                excludeClassNamesPattern = (Pattern) excludeClassNamesField.getValue();
                excludeClassNames = excludeClassNamesPattern.pattern();
            } catch (final Exception ignore) {
            }
        }
    });
    constraints.gridx = 1;
    constraints.gridy = 0;
    constraints.weightx = 1.0;
    constraints.anchor = GridBagConstraints.NORTHWEST;
    constraints.fill = GridBagConstraints.NONE;
    panel.add(excludeClassNamesField, constraints);
    final CheckBox excludeExceptionCheckBox = new CheckBox("Ignore exception classes", this, "excludeException");
    constraints.gridx = 0;
    constraints.gridy = 1;
    constraints.gridwidth = 2;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    panel.add(excludeExceptionCheckBox, constraints);
    final CheckBox excludeDeprecatedCheckBox = new CheckBox("Ignore deprecated classes", this, "excludeDeprecated");
    constraints.gridy = 2;
    panel.add(excludeDeprecatedCheckBox, constraints);
    final CheckBox excludeEnumCheckBox = new CheckBox("Ignore enum classes", this, "excludeEnum");
    constraints.gridy = 3;
    panel.add(excludeEnumCheckBox, constraints);
    final CheckBox excludeAbstractCheckBox = new CheckBox("Ignore abstract classes", this, "excludeAbstract");
    constraints.gridy = 4;
    panel.add(excludeAbstractCheckBox, constraints);
    final CheckBox excludeInTestCodeCheckBox = new CheckBox("Ignore test classes", this, "excludeTestCode");
    constraints.gridy = 5;
    panel.add(excludeInTestCodeCheckBox, constraints);
    final CheckBox excludeInnerClasses = new CheckBox("Ignore inner classes", this, "excludeInnerClasses");
    constraints.gridy = 6;
    constraints.weighty = 1.0;
    panel.add(excludeInnerClasses, constraints);
    return panel;
}
Also used : Pattern(java.util.regex.Pattern) DocumentAdapter(com.intellij.ui.DocumentAdapter) Document(javax.swing.text.Document) DocumentEvent(javax.swing.event.DocumentEvent) RegExInputVerifier(com.intellij.codeInspection.ui.RegExInputVerifier) PatternSyntaxException(java.util.regex.PatternSyntaxException) RegExFormatter(com.intellij.codeInspection.ui.RegExFormatter) CheckBox(com.intellij.util.ui.CheckBox)

Example 47 with Document

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

the class SwingHelper method scrollToReference.

public static boolean scrollToReference(JEditorPane view, String reference) {
    reference = StringUtil.trimStart(reference, "#");
    List<String> toCheck = Arrays.asList("a", "h1", "h2", "h3", "h4");
    Document document = view.getDocument();
    if (document instanceof HTMLDocument) {
        List<Element> list = new ArrayList<>();
        for (Element root : document.getRootElements()) {
            getAllElements(root, list, toCheck);
        }
        for (Element element : list) {
            AttributeSet attributes = element.getAttributes();
            String nm = (String) attributes.getAttribute(HTML.Attribute.NAME);
            if (nm == null)
                nm = (String) attributes.getAttribute(HTML.Attribute.ID);
            if ((nm != null) && nm.equals(reference)) {
                try {
                    int pos = element.getStartOffset();
                    Rectangle r = view.modelToView(pos);
                    if (r != null) {
                        Rectangle vis = view.getVisibleRect();
                        r.y -= 5;
                        r.height = vis.height;
                        view.scrollRectToVisible(r);
                        return true;
                    }
                } catch (BadLocationException ex) {
                //ignore
                }
            }
        }
    }
    return false;
}
Also used : AttributeSet(javax.swing.text.AttributeSet) HTMLDocument(javax.swing.text.html.HTMLDocument) Element(javax.swing.text.Element) HTMLDocument(javax.swing.text.html.HTMLDocument) Document(javax.swing.text.Document) BadLocationException(javax.swing.text.BadLocationException)

Example 48 with Document

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

the class ChangesTrackingTableView method addChangeListener.

private static void addChangeListener(final Component component, final ChangeListener listener, Disposable parentDisposable) {
    if (component instanceof CellEditorComponentWithBrowseButton) {
        addChangeListener(((CellEditorComponentWithBrowseButton) component).getChildComponent(), listener, parentDisposable);
    } else if (component instanceof JTextField) {
        final DocumentAdapter documentListener = new DocumentAdapter() {

            @Override
            protected void textChanged(DocumentEvent e) {
                listener.stateChanged(new ChangeEvent(component));
            }
        };
        final Document document = ((JTextField) component).getDocument();
        document.addDocumentListener(documentListener);
        Disposer.register(parentDisposable, new Disposable() {

            @Override
            public void dispose() {
                document.removeDocumentListener(documentListener);
            }
        });
    } else if (component instanceof JComboBox) {
        final ActionListener comboListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                listener.stateChanged(new ChangeEvent(component));
            }
        };
        ((JComboBox) component).addActionListener(comboListener);
        Disposer.register(parentDisposable, new Disposable() {

            @Override
            public void dispose() {
                ((JComboBox) component).removeActionListener(comboListener);
            }
        });
    } else if (component instanceof JCheckBox) {
        final ActionListener checkBoxListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                listener.stateChanged(new ChangeEvent(component));
            }
        };
        ((JCheckBox) component).addActionListener(checkBoxListener);
        Disposer.register(parentDisposable, new Disposable() {

            @Override
            public void dispose() {
                ((JCheckBox) component).removeActionListener(checkBoxListener);
            }
        });
    } else {
        throw new UnsupportedOperationException("editor control of type " + component.getClass().getName() + " is not supported");
    }
}
Also used : Disposable(com.intellij.openapi.Disposable) ActionEvent(java.awt.event.ActionEvent) DocumentAdapter(com.intellij.ui.DocumentAdapter) DocumentEvent(javax.swing.event.DocumentEvent) Document(javax.swing.text.Document) ChangeEvent(javax.swing.event.ChangeEvent) ActionListener(java.awt.event.ActionListener)

Example 49 with Document

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

the class HtmlPanel method getSelectedText.

@Override
public String getSelectedText() {
    Document doc = getDocument();
    int start = getSelectionStart();
    int end = getSelectionEnd();
    try {
        Position p0 = doc.createPosition(start);
        Position p1 = doc.createPosition(end);
        StringWriter sw = new StringWriter(p1.getOffset() - p0.getOffset());
        getEditorKit().write(sw, doc, p0.getOffset(), p1.getOffset() - p0.getOffset());
        return StringUtil.removeHtmlTags(sw.toString());
    } catch (BadLocationException | IOException ignored) {
    }
    return super.getSelectedText();
}
Also used : StringWriter(java.io.StringWriter) Position(javax.swing.text.Position) IOException(java.io.IOException) Document(javax.swing.text.Document) BadLocationException(javax.swing.text.BadLocationException)

Example 50 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)

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