Search in sources :

Example 16 with Document

use of javax.swing.text.Document in project Botnak by Gocnak.

the class ListenerURL method mouseReleased.

@Override
public void mouseReleased(MouseEvent e) {
    JTextPane editor = (JTextPane) e.getSource();
    Point pt = new Point(e.getX(), e.getY());
    int pos = editor.viewToModel(pt);
    if (pos >= 0) {
        Document doc = editor.getDocument();
        if (doc instanceof DefaultStyledDocument) {
            DefaultStyledDocument hdoc = (DefaultStyledDocument) doc;
            Element el = hdoc.getCharacterElement(pos);
            AttributeSet a = el.getAttributes();
            String href = (String) a.getAttribute(HTML.Attribute.HREF);
            if (href != null) {
                Utils.openWebPage(href);
            }
        }
    }
}
Also used : AttributeSet(javax.swing.text.AttributeSet) Element(javax.swing.text.Element) DefaultStyledDocument(javax.swing.text.DefaultStyledDocument) Document(javax.swing.text.Document) DefaultStyledDocument(javax.swing.text.DefaultStyledDocument)

Example 17 with Document

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

the class ActionTracer method afterActionPerformed.

@Override
public void afterActionPerformed(AnAction action, DataContext dataContext, AnActionEvent event) {
    StringBuilder out = new StringBuilder(String.format("%1$tF %1$tT,%1$tL ", System.currentTimeMillis()));
    final ActionManager actionManager = ActionManager.getInstance();
    final String id = actionManager.getId(action);
    out.append("id=").append(id);
    if (id != null) {
        out.append("; shortcuts:");
        final Shortcut[] shortcuts = KeymapManager.getInstance().getActiveKeymap().getShortcuts(id);
        for (int i = 0; i < shortcuts.length; i++) {
            Shortcut shortcut = shortcuts[i];
            out.append(shortcut);
            if (i < shortcuts.length - 1) {
                out.append(",");
            }
        }
    }
    out.append("; class: ").append(action.getClass().getName());
    out.append("\n");
    final Document doc = myText.getDocument();
    try {
        doc.insertString(doc.getLength(), out.toString(), null);
        SwingUtilities.invokeLater(() -> {
            final int y = (int) myText.getBounds().getMaxY();
            myText.scrollRectToVisible(new Rectangle(0, y, myText.getBounds().width, 0));
        });
    } catch (BadLocationException e) {
        LOG.error(e);
    }
}
Also used : Document(javax.swing.text.Document) BadLocationException(javax.swing.text.BadLocationException)

Example 18 with Document

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

the class SingleIntegerFieldOptionsPanel method setupIntegerFieldTrackingValue.

/**
     * Sets integer number format to JFormattedTextField instance,
     * sets value of JFormattedTextField instance to object's field value,
     * synchronizes object's field value with the value of JFormattedTextField instance.
     *
     * @param textField  JFormattedTextField instance
     * @param owner      an object whose field is synchronized with {@code textField}
     * @param property   object's field name for synchronization
     */
public static void setupIntegerFieldTrackingValue(final JFormattedTextField textField, final InspectionProfileEntry owner, final String property) {
    NumberFormat formatter = NumberFormat.getIntegerInstance();
    formatter.setParseIntegerOnly(true);
    textField.setFormatterFactory(new DefaultFormatterFactory(new NumberFormatter(formatter)));
    textField.setValue(getPropertyValue(owner, property));
    final Document document = textField.getDocument();
    document.addDocumentListener(new DocumentAdapter() {

        @Override
        public void textChanged(DocumentEvent e) {
            try {
                textField.commitEdit();
                setPropertyValue(owner, property, ((Number) textField.getValue()).intValue());
            } catch (ParseException e1) {
            // No luck this time
            }
        }
    });
}
Also used : DocumentAdapter(com.intellij.ui.DocumentAdapter) DefaultFormatterFactory(javax.swing.text.DefaultFormatterFactory) ParseException(java.text.ParseException) Document(javax.swing.text.Document) DocumentEvent(javax.swing.event.DocumentEvent) NumberFormat(java.text.NumberFormat) NumberFormatter(javax.swing.text.NumberFormatter)

Example 19 with Document

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

the class FileTextFieldImpl method replacePathComponent.

/**
   * Replace the path component under the caret with the file selected from the completion list.
   *
   * @param file     the selected file.
   * @param caretPos
   * @param start    the start offset of the path component under the caret.
   * @param end      the end offset of the path component under the caret.
   * @throws BadLocationException
   */
private void replacePathComponent(LookupFile file, int caretPos, int start, int end) throws BadLocationException {
    final Document doc = myPathTextField.getDocument();
    myPathTextField.setSelectionStart(0);
    myPathTextField.setSelectionEnd(0);
    final String name = file.getName();
    boolean toRemoveExistingName;
    String prefix = "";
    if (caretPos >= start) {
        prefix = doc.getText(start, caretPos - start);
        if (prefix.length() == 0) {
            prefix = doc.getText(start, end - start);
        }
        if (SystemInfo.isFileSystemCaseSensitive) {
            toRemoveExistingName = name.startsWith(prefix) && prefix.length() > 0;
        } else {
            toRemoveExistingName = StringUtil.toUpperCase(name).startsWith(StringUtil.toUpperCase(prefix)) && prefix.length() > 0;
        }
    } else {
        toRemoveExistingName = true;
    }
    int newPos;
    if (toRemoveExistingName) {
        doc.remove(start, end - start);
        doc.insertString(start, name, doc.getDefaultRootElement().getAttributes());
        newPos = start + name.length();
    } else {
        doc.insertString(caretPos, name, doc.getDefaultRootElement().getAttributes());
        newPos = caretPos + name.length();
    }
    if (file.isDirectory()) {
        if (!myFinder.getSeparator().equals(doc.getText(newPos, 1))) {
            doc.insertString(newPos, myFinder.getSeparator(), doc.getDefaultRootElement().getAttributes());
            newPos++;
        }
    }
    if (newPos < doc.getLength()) {
        if (myFinder.getSeparator().equals(doc.getText(newPos, 1))) {
            newPos++;
        }
    }
    myPathTextField.setCaretPosition(newPos);
}
Also used : Document(javax.swing.text.Document)

Example 20 with Document

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

the class IfCanBeSwitchInspection method createOptionsPanel.

@Override
public JComponent createOptionsPanel() {
    final JPanel panel = new JPanel(new GridBagLayout());
    final JLabel label = new JLabel(InspectionGadgetsBundle.message("if.can.be.switch.minimum.branch.option"));
    final NumberFormat formatter = NumberFormat.getIntegerInstance();
    formatter.setParseIntegerOnly(true);
    final JFormattedTextField valueField = new JFormattedTextField(formatter);
    valueField.setValue(Integer.valueOf(minimumBranches));
    valueField.setColumns(2);
    final Document document = valueField.getDocument();
    document.addDocumentListener(new DocumentAdapter() {

        @Override
        public void textChanged(DocumentEvent e) {
            try {
                valueField.commitEdit();
                minimumBranches = ((Number) valueField.getValue()).intValue();
            } catch (ParseException ignore) {
            // No luck this time
            }
        }
    });
    final GridBagConstraints constraints = new GridBagConstraints();
    constraints.gridx = 0;
    constraints.gridy = 0;
    constraints.insets.bottom = 4;
    constraints.weightx = 0.0;
    constraints.anchor = GridBagConstraints.BASELINE_LEADING;
    constraints.fill = GridBagConstraints.NONE;
    constraints.insets.right = 10;
    panel.add(label, constraints);
    constraints.gridx = 1;
    constraints.gridy = 0;
    constraints.weightx = 1.0;
    constraints.insets.right = 0;
    panel.add(valueField, constraints);
    constraints.gridx = 0;
    constraints.gridy = 1;
    constraints.gridwidth = 2;
    final CheckBox checkBox1 = new CheckBox(InspectionGadgetsBundle.message("if.can.be.switch.int.option"), this, "suggestIntSwitches");
    panel.add(checkBox1, constraints);
    constraints.gridy = 2;
    final CheckBox checkBox2 = new CheckBox(InspectionGadgetsBundle.message("if.can.be.switch.enum.option"), this, "suggestEnumSwitches");
    panel.add(checkBox2, constraints);
    constraints.gridy = 3;
    constraints.weighty = 1.0;
    final CheckBox checkBox3 = new CheckBox(InspectionGadgetsBundle.message("if.can.be.switch.null.safe.option"), this, "onlySuggestNullSafe");
    panel.add(checkBox3, constraints);
    return panel;
}
Also used : DocumentAdapter(com.intellij.ui.DocumentAdapter) Document(javax.swing.text.Document) DocumentEvent(javax.swing.event.DocumentEvent) CheckBox(com.intellij.util.ui.CheckBox) ParseException(java.text.ParseException) NumberFormat(java.text.NumberFormat)

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