Search in sources :

Example 1 with HighlightPainter

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

Example 2 with HighlightPainter

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

the class HttpPanelSyntaxHighlightTextArea method highlight.

protected void highlight(int start, int end) {
    Highlighter hilite = this.getHighlighter();
    HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(DisplayUtils.getHighlightColor());
    try {
        removeAllHighlights();
        hilite.addHighlight(start, end, painter);
        this.setCaretPosition(start);
    } catch (BadLocationException e) {
        log.error(e.getMessage(), 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 3 with HighlightPainter

use of javax.swing.text.Highlighter.HighlightPainter in project java-swing-tips by aterai.

the class ComboKeyHandler method makeComboBox.

private static JComboBox<String> makeComboBox(String... model) {
    HighlightPainter highlightPainter = new DefaultHighlightPainter(Color.YELLOW);
    return new JComboBox<String>(model) {

        @Override
        public void updateUI() {
            super.updateUI();
            JTextField field = new JTextField(" ");
            field.setOpaque(true);
            field.setBorder(BorderFactory.createEmptyBorder());
            ListCellRenderer<? super String> renderer = getRenderer();
            setRenderer((list, value, index, isSelected, cellHasFocus) -> {
                String pattern = ((JTextField) getEditor().getEditorComponent()).getText();
                if (index >= 0 && !pattern.isEmpty()) {
                    Highlighter highlighter = field.getHighlighter();
                    highlighter.removeAllHighlights();
                    String txt = Objects.toString(value, "");
                    field.setText(txt);
                    addHighlight(highlighter, Pattern.compile(pattern).matcher(txt));
                    field.setBackground(isSelected ? new Color(0xAA_64_AA_FF, true) : Color.WHITE);
                    return field;
                }
                return renderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            });
        }

        private void addHighlight(Highlighter highlighter, Matcher matcher) {
            int pos = 0;
            try {
                while (matcher.find(pos) && !matcher.group().isEmpty()) {
                    int start = matcher.start();
                    int end = matcher.end();
                    highlighter.addHighlight(start, end, highlightPainter);
                    pos = end;
                }
            } catch (BadLocationException ex) {
                // should never happen
                RuntimeException wrap = new StringIndexOutOfBoundsException(ex.offsetRequested());
                wrap.initCause(ex);
                throw wrap;
            }
        }
    };
}
Also used : Matcher(java.util.regex.Matcher) DefaultHighlightPainter(javax.swing.text.DefaultHighlighter.DefaultHighlightPainter) HighlightPainter(javax.swing.text.Highlighter.HighlightPainter) DefaultHighlightPainter(javax.swing.text.DefaultHighlighter.DefaultHighlightPainter) BadLocationException(javax.swing.text.BadLocationException) Highlighter(javax.swing.text.Highlighter)

Example 4 with HighlightPainter

use of javax.swing.text.Highlighter.HighlightPainter in project omegat by omegat-org.

the class ProtectedPartsMarker method getMarksForEntry.

@Override
public List<Mark> getMarksForEntry(SourceTextEntry ste, String sourceText, String translationText, boolean isActive) throws Exception {
    HighlightPainter painter;
    AttributeSet attrs;
    if (((EditorController) Core.getEditor()).getOrientation() == Document3.ORIENTATION.ALL_LTR) {
        attrs = ATTRIBUTES_LTR;
        painter = null;
    } else {
        attrs = null;
        painter = PAINTER_RTL;
    }
    if (ste.getProtectedParts().length == 0) {
        return null;
    }
    if (sourceText == null && translationText == null) {
        return null;
    }
    List<Mark> r = new ArrayList<Mark>();
    // find protected parts
    for (ProtectedPart pp : ste.getProtectedParts()) {
        if (sourceText != null) {
            int pos = -1;
            while ((pos = sourceText.indexOf(pp.getTextInSourceSegment(), pos + 1)) >= 0) {
                Mark m = new Mark(Mark.ENTRY_PART.SOURCE, pos, pos + pp.getTextInSourceSegment().length());
                m.painter = painter;
                m.attributes = attrs;
                m.toolTipText = escapeHtml(pp.getDetailsFromSourceFile());
                r.add(m);
            }
        }
        if (translationText != null) {
            int pos = -1;
            while ((pos = translationText.indexOf(pp.getTextInSourceSegment(), pos + 1)) >= 0) {
                Mark m = new Mark(Mark.ENTRY_PART.TRANSLATION, pos, pos + pp.getTextInSourceSegment().length());
                m.painter = painter;
                m.attributes = attrs;
                m.toolTipText = escapeHtml(pp.getDetailsFromSourceFile());
                r.add(m);
            }
        }
    }
    return r;
}
Also used : ProtectedPart(org.omegat.core.data.ProtectedPart) AttributeSet(javax.swing.text.AttributeSet) HighlightPainter(javax.swing.text.Highlighter.HighlightPainter) ArrayList(java.util.ArrayList)

Example 5 with HighlightPainter

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

the class HttpPanelSyntaxHighlightTextArea 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

HighlightPainter (javax.swing.text.Highlighter.HighlightPainter)7 BadLocationException (javax.swing.text.BadLocationException)6 Highlighter (javax.swing.text.Highlighter)6 DefaultHighlighter (javax.swing.text.DefaultHighlighter)5 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 JButton (javax.swing.JButton)1 AttributeSet (javax.swing.text.AttributeSet)1 DefaultHighlightPainter (javax.swing.text.DefaultHighlighter.DefaultHighlightPainter)1 Highlight (javax.swing.text.Highlighter.Highlight)1 ProtectedPart (org.omegat.core.data.ProtectedPart)1