Search in sources :

Example 16 with Highlighter

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

the class HighlightTableCellRenderer method getTableCellRendererComponent.

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    String txt = Objects.toString(value, "");
    Highlighter highlighter = getHighlighter();
    highlighter.removeAllHighlights();
    setText(txt);
    setBackground(isSelected ? SELECTION_BGC : Color.WHITE);
    if (Objects.nonNull(pattern) && !pattern.isEmpty() && !Objects.equals(pattern, prev)) {
        Matcher matcher = Pattern.compile(pattern).matcher(txt);
        HighlightPainter highlightPainter = new DefaultHighlightPainter(Color.YELLOW);
        int pos = 0;
        while (matcher.find(pos) && !matcher.group().isEmpty()) {
            int start = matcher.start();
            int end = matcher.end();
            try {
                highlighter.addHighlight(start, end, highlightPainter);
            } catch (BadLocationException | PatternSyntaxException ex) {
                UIManager.getLookAndFeel().provideErrorFeedback(this);
            }
            pos = end;
        }
    }
    return this;
}
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) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 17 with Highlighter

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

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

the class MainPanel method addHighlight.

public void addHighlight(Element element, boolean isBlock) {
    Highlighter highlighter = editorPane.getHighlighter();
    int start = element.getStartOffset();
    int lf = isBlock ? 1 : 0;
    // lf???, setDrawsLayeredHighlights(false) bug???
    int end = element.getEndOffset() - lf;
    try {
        highlighter.addHighlight(start, end, HIGHLIGHT);
    } catch (BadLocationException ex) {
        // should never happen
        RuntimeException wrap = new StringIndexOutOfBoundsException(ex.offsetRequested());
        wrap.initCause(ex);
        throw wrap;
    }
}
Also used : BadLocationException(javax.swing.text.BadLocationException) DefaultHighlighter(javax.swing.text.DefaultHighlighter) Highlighter(javax.swing.text.Highlighter)

Example 19 with Highlighter

use of javax.swing.text.Highlighter in project cooja by contiki-ng.

the class CodeUI method displayLine.

/**
 * Mark given line number in shown source code.
 * Should be called from AWT thread.
 *
 * @param lineNumber Line number
 */
private void displayLine(int lineNumber, boolean markCurrent) {
    try {
        if (markCurrent) {
            /* remove previous highlight */
            Highlighter hl = codeEditor.getHighlighter();
            hl.changeHighlight(currentLineTag, 0, 0);
        }
        if (lineNumber >= 0) {
            final int start = codeEditorLines.get(lineNumber);
            int end = codeEditorLines.get(lineNumber + 1);
            if (markCurrent) {
                /* highlight code */
                Highlighter hl = codeEditor.getHighlighter();
                hl.changeHighlight(currentLineTag, start, end);
            }
            /* ensure visible */
            SwingUtilities.invokeLater(new Runnable() {

                public void run() {
                    try {
                        Rectangle r = codeEditor.modelToView(start);
                        if (r != null) {
                            codeEditor.scrollRectToVisible(codeEditor.modelToView(start));
                        }
                    } catch (BadLocationException e) {
                    }
                }
            });
        }
    } catch (Exception e) {
        logger.warn("Error when highlighting current line: " + e.getMessage(), e);
    }
}
Also used : Rectangle(java.awt.Rectangle) Point(java.awt.Point) JSyntaxRemoveBreakpoint(org.contikios.cooja.util.JSyntaxRemoveBreakpoint) Watchpoint(org.contikios.cooja.Watchpoint) JSyntaxAddBreakpoint(org.contikios.cooja.util.JSyntaxAddBreakpoint) BadLocationException(javax.swing.text.BadLocationException) BadLocationException(javax.swing.text.BadLocationException) Highlighter(javax.swing.text.Highlighter)

Example 20 with Highlighter

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

the class JTextAreaUtils method highlight.

public static void highlight(JTextArea textArea, String pattern, boolean caseSensitiveSearch) {
    if (pattern.isEmpty()) {
        textArea.getHighlighter().removeAllHighlights();
        return;
    }
    try {
        Highlighter highlighter = textArea.getHighlighter();
        highlighter.removeAllHighlights();
        Document doc = textArea.getDocument();
        String text = doc.getText(0, doc.getLength());
        int pos = 0;
        if (!caseSensitiveSearch) {
            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
            highlighter.addHighlight(pos, pos + pattern.length(), painter);
            pos += pattern.length();
        }
    } catch (Exception e) {
        BytecodeViewer.handleException(e);
    }
}
Also used : Document(javax.swing.text.Document) DefaultHighlighter(javax.swing.text.DefaultHighlighter) Highlighter(javax.swing.text.Highlighter)

Aggregations

Highlighter (javax.swing.text.Highlighter)68 BadLocationException (javax.swing.text.BadLocationException)40 DefaultHighlighter (javax.swing.text.DefaultHighlighter)37 Matcher (java.util.regex.Matcher)12 HighlightPainter (javax.swing.text.Highlighter.HighlightPainter)10 JTextArea (javax.swing.JTextArea)9 Document (javax.swing.text.Document)9 Point (java.awt.Point)6 File (java.io.File)6 PatternSyntaxException (java.util.regex.PatternSyntaxException)5 DefaultHighlightPainter (javax.swing.text.DefaultHighlighter.DefaultHighlightPainter)5 Editor (omega.ui.component.Editor)5 StringTokenizer (java.util.StringTokenizer)4 JTextComponent (javax.swing.text.JTextComponent)4 Highlight (omega.instant.support.Highlight)4 IOException (java.io.IOException)3 ImageIcon (javax.swing.ImageIcon)3 AbstractErrorHighlighter (omega.instant.support.AbstractErrorHighlighter)3 JavaSyntaxParserGutterIconInfo (omega.instant.support.java.parser.JavaSyntaxParserGutterIconInfo)3 SquiggleUnderlineHighlightPainter (org.fife.ui.rsyntaxtextarea.SquiggleUnderlineHighlightPainter)3