Search in sources :

Example 1 with Highlighter

use of javax.swing.text.Highlighter in project jabref by JabRef.

the class JEditorPaneWithHighlighting method highlightPattern.

public void highlightPattern(Optional<Pattern> highlightPattern) {
    Highlighter highlighter = getHighlighter();
    highlighter.removeAllHighlights();
    if ((highlightPattern == null) || !highlightPattern.isPresent()) {
        return;
    }
    String text = getDocumentText();
    Matcher matcher = highlightPattern.get().matcher(text);
    LayerPainter painter = DefaultHighlighter.DefaultPainter;
    while (matcher.find()) {
        try {
            highlighter.addHighlight(matcher.start(), matcher.end(), painter);
        } catch (BadLocationException ble) {
            // should not occur if matcher works right
            LOGGER.warn("Highlighting not possible, bad location", ble);
        }
    }
}
Also used : Matcher(java.util.regex.Matcher) BadLocationException(javax.swing.text.BadLocationException) DefaultHighlighter(javax.swing.text.DefaultHighlighter) Highlighter(javax.swing.text.Highlighter) LayerPainter(javax.swing.text.LayeredHighlighter.LayerPainter)

Example 2 with Highlighter

use of javax.swing.text.Highlighter in project jabref by JabRef.

the class JTextAreaWithHighlighting method highLight.

/**
     * Highlight words in the Textarea
     *
     * @param words to highlight
     */
private void highLight() {
    // highlight all characters that appear in charsToHighlight
    Highlighter highlighter = getHighlighter();
    highlighter.removeAllHighlights();
    if ((highlightPattern == null) || !highlightPattern.isPresent()) {
        return;
    }
    String content = getText();
    if (content.isEmpty()) {
        return;
    }
    highlightPattern.ifPresent(pattern -> {
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            try {
                highlighter.addHighlight(matcher.start(), matcher.end(), DefaultHighlighter.DefaultPainter);
            } catch (BadLocationException ble) {
                LOGGER.warn("Highlighting not possible, bad location", ble);
            }
        }
    });
}
Also used : Matcher(java.util.regex.Matcher) BadLocationException(javax.swing.text.BadLocationException) DefaultHighlighter(javax.swing.text.DefaultHighlighter) Highlighter(javax.swing.text.Highlighter)

Example 3 with Highlighter

use of javax.swing.text.Highlighter in project bytecode-viewer by Konloch.

the class ClassViewer method highlight.

public void highlight(int pane, JTextComponent textComp, String pattern) {
    if (pattern.isEmpty()) {
        textComp.getHighlighter().removeAllHighlights();
        return;
    }
    try {
        Highlighter hilite = textComp.getHighlighter();
        hilite.removeAllHighlights();
        javax.swing.text.Document doc = textComp.getDocument();
        String text = doc.getText(0, doc.getLength());
        int pos = 0;
        if ((pane == 0 && !exacts.get(0).isSelected()) || pane == 1 && !exacts.get(1).isSelected() || pane == 2 && !exacts.get(2).isSelected()) {
            pattern = pattern.toLowerCase();
            text = text.toLowerCase();
        }
        // Search for pattern
        while ((pos = text.indexOf(pattern, pos)) >= 0) {
            // Create highlighter using private painter and apply around
            // pattern
            hilite.addHighlight(pos, pos + pattern.length(), painter);
            pos += pattern.length();
        }
    } catch (Exception e) {
        new the.bytecode.club.bytecodeviewer.api.ExceptionUI(e);
    }
}
Also used : DefaultHighlighter(javax.swing.text.DefaultHighlighter) Highlighter(javax.swing.text.Highlighter)

Example 4 with Highlighter

use of javax.swing.text.Highlighter in project bytecode-viewer by Konloch.

the class PluginConsole method highlight.

public void highlight(JTextComponent textComp, String pattern) {
    if (pattern.isEmpty()) {
        textComp.getHighlighter().removeAllHighlights();
        return;
    }
    try {
        Highlighter hilite = textComp.getHighlighter();
        hilite.removeAllHighlights();
        javax.swing.text.Document doc = textComp.getDocument();
        String text = doc.getText(0, doc.getLength());
        int pos = 0;
        if (!check.isSelected()) {
            pattern = pattern.toLowerCase();
            text = text.toLowerCase();
        }
        // Search for pattern
        while ((pos = text.indexOf(pattern, pos)) >= 0) {
            // Create highlighter using private painter and apply around
            // pattern
            hilite.addHighlight(pos, pos + pattern.length(), painter);
            pos += pattern.length();
        }
    } catch (Exception e) {
        new the.bytecode.club.bytecodeviewer.api.ExceptionUI(e);
    }
}
Also used : DefaultHighlighter(javax.swing.text.DefaultHighlighter) Highlighter(javax.swing.text.Highlighter)

Example 5 with Highlighter

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

the class CustomScanDialog method getAddCustomButton.

private JButton getAddCustomButton() {
    if (addCustomButton == null) {
        addCustomButton = new JButton(Constant.messages.getString("ascan.custom.button.pt.add"));
        addCustomButton.setEnabled(false);
        addCustomButton.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(java.awt.event.ActionEvent e) {
                // Add the selected injection point
                int userDefStart = getRequestField().getSelectionStart();
                if (userDefStart >= 0) {
                    int userDefEnd = getRequestField().getSelectionEnd();
                    Highlighter hl = getRequestField().getHighlighter();
                    HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.RED);
                    try {
                        Highlight hlt = (Highlight) hl.addHighlight(userDefStart, userDefEnd, painter);
                        injectionPointModel.addElement(hlt);
                        // Unselect the text
                        getRequestField().setSelectionStart(userDefEnd);
                        getRequestField().setSelectionEnd(userDefEnd);
                        getRequestField().getCaret().setVisible(true);
                    } catch (BadLocationException e1) {
                        logger.error(e1.getMessage(), e1);
                    }
                }
            }
        });
    }
    return addCustomButton;
}
Also used : ActionListener(java.awt.event.ActionListener) Highlight(javax.swing.text.Highlighter.Highlight) HighlightPainter(javax.swing.text.Highlighter.HighlightPainter) JButton(javax.swing.JButton) DefaultHighlighter(javax.swing.text.DefaultHighlighter) ActionEvent(java.awt.event.ActionEvent) BadLocationException(javax.swing.text.BadLocationException) DefaultHighlighter(javax.swing.text.DefaultHighlighter) Highlighter(javax.swing.text.Highlighter)

Aggregations

Highlighter (javax.swing.text.Highlighter)32 BadLocationException (javax.swing.text.BadLocationException)22 DefaultHighlighter (javax.swing.text.DefaultHighlighter)20 Matcher (java.util.regex.Matcher)10 HighlightPainter (javax.swing.text.Highlighter.HighlightPainter)6 PatternSyntaxException (java.util.regex.PatternSyntaxException)5 Document (javax.swing.text.Document)5 Point (java.awt.Point)2 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 AffineTransform (java.awt.geom.AffineTransform)2 IOException (java.io.IOException)2 JButton (javax.swing.JButton)2 DefaultHighlightPainter (javax.swing.text.DefaultHighlighter.DefaultHighlightPainter)2 Highlight (javax.swing.text.Highlighter.Highlight)2 Locale (java.util.Locale)1 Pattern (java.util.regex.Pattern)1 JLabel (javax.swing.JLabel)1 AbstractDocument (javax.swing.text.AbstractDocument)1 JTextComponent (javax.swing.text.JTextComponent)1