Search in sources :

Example 6 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 7 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 8 with Highlighter

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

the class CustomScanDialog method getRemoveCustomButton.

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

            @Override
            public void actionPerformed(java.awt.event.ActionEvent e) {
                // Remove any selected injection points
                int userDefStart = getRequestField().getSelectionStart();
                if (userDefStart >= 0) {
                    int userDefEnd = getRequestField().getSelectionEnd();
                    Highlighter hltr = getRequestField().getHighlighter();
                    Highlight[] hls = hltr.getHighlights();
                    if (hls != null && hls.length > 0) {
                        for (Highlight hl : hls) {
                            if (selectionIncludesHighlight(userDefStart, userDefEnd, hl)) {
                                hltr.removeHighlight(hl);
                                injectionPointModel.removeElement(hl);
                            }
                        }
                    }
                    // Unselect the text
                    getRequestField().setSelectionStart(userDefEnd);
                    getRequestField().setSelectionEnd(userDefEnd);
                    getRequestField().getCaret().setVisible(true);
                }
            }
        });
    }
    return removeCustomButton;
}
Also used : ActionListener(java.awt.event.ActionListener) Highlight(javax.swing.text.Highlighter.Highlight) JButton(javax.swing.JButton) ActionEvent(java.awt.event.ActionEvent) DefaultHighlighter(javax.swing.text.DefaultHighlighter) Highlighter(javax.swing.text.Highlighter)

Example 9 with Highlighter

use of javax.swing.text.Highlighter 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)

Example 10 with Highlighter

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

the class HttpPanelTextArea method removeAllHighlights.

private void removeAllHighlights() {
    Highlighter hilite = this.getHighlighter();
    hilite.removeAllHighlights();
}
Also used : DefaultHighlighter(javax.swing.text.DefaultHighlighter) Highlighter(javax.swing.text.Highlighter)

Aggregations

DefaultHighlighter (javax.swing.text.DefaultHighlighter)14 Highlighter (javax.swing.text.Highlighter)14 BadLocationException (javax.swing.text.BadLocationException)7 HighlightPainter (javax.swing.text.Highlighter.HighlightPainter)5 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 IOException (java.io.IOException)2 Matcher (java.util.regex.Matcher)2 JButton (javax.swing.JButton)2 Highlight (javax.swing.text.Highlighter.Highlight)2 LayerPainter (javax.swing.text.LayeredHighlighter.LayerPainter)1