Search in sources :

Example 66 with BadLocationException

use of javax.swing.text.BadLocationException 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 67 with BadLocationException

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

the class JBLabel method setCopyable.

/**
   * In 'copyable' mode JBLabel has the same appearance but user can select text with mouse and copy it to clipboard with standard shortcut.
   * By default JBLabel is NOT copyable
   * @return 'this' (the same instance)
   */
public JBLabel setCopyable(boolean copyable) {
    if (copyable ^ myEditorPane != null) {
        if (myEditorPane == null) {
            final JLabel ellipsisLabel = new JBLabel("...");
            myIconLabel = new JLabel(getIcon());
            myEditorPane = new JEditorPane() {

                @Override
                public void paint(Graphics g) {
                    Dimension size = getSize();
                    boolean paintEllipsis = getPreferredSize().width > size.width && !myMultiline;
                    if (!paintEllipsis) {
                        super.paint(g);
                    } else {
                        Dimension ellipsisSize = ellipsisLabel.getPreferredSize();
                        int endOffset = size.width - ellipsisSize.width;
                        try {
                            // do not paint half of the letter
                            endOffset = modelToView(viewToModel(new Point(endOffset, 0)) - 1).x;
                        } catch (BadLocationException ignore) {
                        }
                        Shape oldClip = g.getClip();
                        g.clipRect(0, 0, endOffset, size.height);
                        super.paint(g);
                        g.setClip(oldClip);
                        g.translate(endOffset, 0);
                        ellipsisLabel.setSize(ellipsisSize);
                        ellipsisLabel.paint(g);
                        g.translate(-endOffset, 0);
                    }
                }
            };
            myEditorPane.addFocusListener(new FocusAdapter() {

                @Override
                public void focusLost(FocusEvent e) {
                    if (myEditorPane == null)
                        return;
                    int caretPosition = myEditorPane.getCaretPosition();
                    myEditorPane.setSelectionStart(caretPosition);
                    myEditorPane.setSelectionEnd(caretPosition);
                }
            });
            myEditorPane.setContentType("text/html");
            myEditorPane.setEditable(false);
            myEditorPane.setBackground(UIUtil.TRANSPARENT_COLOR);
            myEditorPane.setOpaque(false);
            myEditorPane.setBorder(null);
            UIUtil.putClientProperty(myEditorPane, UIUtil.NOT_IN_HIERARCHY_COMPONENTS, Collections.singleton(ellipsisLabel));
            myEditorPane.setEditorKit(UIUtil.getHTMLEditorKit());
            updateStyle(myEditorPane);
            myEditorPane.setText(getText());
            checkMultiline();
            myEditorPane.setCaretPosition(0);
            updateLayout();
        } else {
            removeAll();
            myEditorPane = null;
            myIconLabel = null;
        }
    }
    return this;
}
Also used : FocusAdapter(java.awt.event.FocusAdapter) FocusEvent(java.awt.event.FocusEvent) BadLocationException(javax.swing.text.BadLocationException)

Example 68 with BadLocationException

use of javax.swing.text.BadLocationException 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 69 with BadLocationException

use of javax.swing.text.BadLocationException in project zaproxy by zaproxy.

the class CustomScanDialog method getInjectionPointList.

private JList<Highlight> getInjectionPointList() {
    if (injectionPointList == null) {
        injectionPointList = new JList<>(injectionPointModel);
        injectionPointList.setCellRenderer(new ListCellRenderer<Highlight>() {

            @Override
            public Component getListCellRendererComponent(JList<? extends Highlight> list, Highlight hlt, int index, boolean isSelected, boolean cellHasFocus) {
                String str = "";
                try {
                    str = getRequestField().getText(hlt.getStartOffset(), hlt.getEndOffset() - hlt.getStartOffset());
                    if (str.length() > 8) {
                        // just show first 8 chrs (arbitrary limit;)
                        str = str.substring(0, 8) + "..";
                    }
                } catch (BadLocationException e) {
                // Ignore
                }
                return new JLabel("[" + hlt.getStartOffset() + "," + hlt.getEndOffset() + "]: " + str);
            }
        });
    }
    return injectionPointList;
}
Also used : Highlight(javax.swing.text.Highlighter.Highlight) JLabel(javax.swing.JLabel) Component(java.awt.Component) BadLocationException(javax.swing.text.BadLocationException)

Example 70 with BadLocationException

use of javax.swing.text.BadLocationException in project zaproxy by zaproxy.

the class HttpPanelTextArea method highlightEntryParser.

// Parse the TextArea data and search the HighlightEntry strings
// Highlight all found strings
private void highlightEntryParser(HighlightSearchEntry entry) {
    String text;
    int lastPos = 0;
    text = this.getText();
    Highlighter hilite = this.getHighlighter();
    HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(entry.getColor());
    while ((lastPos = text.indexOf(entry.getToken(), lastPos)) > -1) {
        try {
            hilite.addHighlight(lastPos, lastPos + entry.getToken().length(), painter);
            lastPos += entry.getToken().length();
        } catch (BadLocationException e) {
            log.warn("Could not highlight entry", e);
        }
    }
}
Also used : HighlightPainter(javax.swing.text.Highlighter.HighlightPainter) BadLocationException(javax.swing.text.BadLocationException) DefaultHighlighter(javax.swing.text.DefaultHighlighter) Highlighter(javax.swing.text.Highlighter)

Aggregations

BadLocationException (javax.swing.text.BadLocationException)88 Document (javax.swing.text.Document)24 Element (javax.swing.text.Element)13 Point (java.awt.Point)10 IOException (java.io.IOException)9 DefaultHighlighter (javax.swing.text.DefaultHighlighter)8 DefaultStyledDocument (javax.swing.text.DefaultStyledDocument)7 Highlighter (javax.swing.text.Highlighter)7 ArrayList (java.util.ArrayList)6 DocumentEvent (javax.swing.event.DocumentEvent)6 StyledDocument (javax.swing.text.StyledDocument)6 HighlightPainter (javax.swing.text.Highlighter.HighlightPainter)5 Dimension (java.awt.Dimension)4 Rectangle (java.awt.Rectangle)4 ActionEvent (java.awt.event.ActionEvent)4 ActionListener (java.awt.event.ActionListener)4 File (java.io.File)4 Date (java.util.Date)4 Style (javax.swing.text.Style)4 HTMLDocument (javax.swing.text.html.HTMLDocument)4