Search in sources :

Example 21 with Highlighter

use of javax.swing.text.Highlighter in project hextractor by sewave.

the class HexViewer method refreshSelection.

/**
 * Refresh selection.
 */
private void refreshSelection() {
    hexTextArea.setCaretPosition(asciiTextArea.getCaretPosition() * Constants.HEX_VALUE_SIZE);
    Highlighter highlighterHex = hexTextArea.getHighlighter();
    highlighterHex.removeAllHighlights();
    try {
        highlighterHex.addHighlight(hexTextArea.getCaretPosition(), hexTextArea.getCaretPosition() + Constants.HEX_VALUE_SIZE - 1, BLUE_PAINTER);
    } catch (BadLocationException e1) {
    // Do nothing
    }
    Highlighter highlighterAscii = asciiTextArea.getHighlighter();
    highlighterAscii.removeAllHighlights();
    try {
        highlighterAscii.addHighlight(asciiTextArea.getCaretPosition(), asciiTextArea.getCaretPosition() + 1, BLUE_PAINTER);
        for (OffsetEntry entry : offEntries) {
            drawOffsetEntry(entry, highlighterAscii, LGRAY_PAINTER, ORANGE_PAINTER);
        }
        if (currEntry.getStart() > 0 && currEntry.getStart() - offset >= 0) {
            highlighterAscii.addHighlight(currEntry.getStart() - offset, currEntry.getStart() - offset + 1, YELLOW_PAINTER);
        }
        if (currEntry.getEnd() > 0 && currEntry.getEnd() - offset >= 0) {
            highlighterAscii.addHighlight(currEntry.getEnd() - offset, currEntry.getEnd() - offset + 1, ORANGE_PAINTER);
        }
    } catch (BadLocationException e1) {
        Utils.log("Bad location.");
    }
    offsetLabelValue.setText(getOffsetLabelValue());
}
Also used : OffsetEntry(com.wave.hextractor.pojo.OffsetEntry) BadLocationException(javax.swing.text.BadLocationException) DefaultHighlighter(javax.swing.text.DefaultHighlighter) Highlighter(javax.swing.text.Highlighter)

Example 22 with Highlighter

use of javax.swing.text.Highlighter in project netbeans-rcp-lite by outersky.

the class ColorHighlighter method highlight.

public int highlight(String word) {
    Highlighter highlighter = comp.getHighlighter();
    // remove old highlight before applying new one
    for (Highlighter.Highlight h : highlighter.getHighlights()) {
        if (h.getPainter() instanceof ColorHighlightPainter) {
            highlighter.removeHighlight(h);
        }
    }
    if (word == null || word.equals("")) {
        return -1;
    }
    // search for the word, case insentitive
    String content = null;
    try {
        Document d = comp.getDocument();
        content = d.getText(0, d.getLength()).toLowerCase();
    } catch (BadLocationException e) {
        return -1;
    }
    word = word.toLowerCase();
    int lastIndex = 0;
    int firstOffset = -1;
    int wordSize = word.length();
    while ((lastIndex = content.indexOf(word, lastIndex)) != -1) {
        int endIndex = lastIndex + wordSize;
        try {
            highlighter.addHighlight(lastIndex, endIndex, painter);
        } catch (BadLocationException ex) {
        // ignore
        }
        if (firstOffset == -1) {
            firstOffset = lastIndex;
        }
        lastIndex = endIndex;
    }
    return firstOffset;
}
Also used : Document(javax.swing.text.Document) BadLocationException(javax.swing.text.BadLocationException) DefaultHighlighter(javax.swing.text.DefaultHighlighter) Highlighter(javax.swing.text.Highlighter)

Example 23 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 24 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 25 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)

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