Search in sources :

Example 36 with Highlighter

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

the class SystemErrConsole 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 : IOException(java.io.IOException) DefaultHighlighter(javax.swing.text.DefaultHighlighter) Highlighter(javax.swing.text.Highlighter)

Example 37 with Highlighter

use of javax.swing.text.Highlighter in project IBMiProgTool by vzupka.

the class DisplayFile method changeHighlight.

/**
 * Find all matches and highlight it YELLOW (highlightPainter),
 * then hihglight current match ORANGE for the text area.
 */
protected void changeHighlight() {
    Highlighter highlighter = textArea.getHighlighter();
    highlighter.removeAllHighlights();
    findField.setBackground(Color.WHITE);
    try {
        Pattern pattern = getPattern();
        if (pattern == null) {
            return;
        }
        if (Objects.nonNull(pattern)) {
            startOffsets = new ArrayList<>();
            endOffsets = new ArrayList<>();
            highlightMap.clear();
            Matcher matcher = pattern.matcher(textArea.getText(0, textArea.getText().length()));
            int pos = 0;
            int start = 0;
            int end = 0;
            while (matcher.find(pos)) {
                start = matcher.start();
                end = matcher.end();
                highlighter.addHighlight(start, end, highlightPainter);
                startOffsets.add(start);
                endOffsets.add(end);
                highlightMap.put(start, end);
                pos = end;
            }
            System.out.println("start: '" + start + "'");
        }
        JLabel label = layerUI.hint;
        Highlighter.Highlight[] array = highlighter.getHighlights();
        // number of highlighted intervals found.
        int hits = array.length;
        if (hits > 0) {
            // If at least one interval was found.
            if (direction.equals("forward")) {
                // Forward direction
                // Get next interval start - greater or equal
                startOffset = highlightMap.ceilingKey(curPos);
                if (startOffset == null) {
                    // First interval
                    startOffset = highlightMap.ceilingKey(0);
                }
                // This interval end
                endOffset = highlightMap.get(startOffset);
                // Sequence number of the interval
                sequence = startOffsets.indexOf(startOffset);
                Highlighter.Highlight hh = highlighter.getHighlights()[sequence];
                highlighter.removeHighlight(hh);
                highlighter.addHighlight(hh.getStartOffset(), hh.getEndOffset(), currentPainter);
                textArea.setCaretPosition(startOffset);
                curPos = startOffset;
            } else {
                // Backward direction
                startOffset = highlightMap.floorKey(curPos);
                if (startOffset == null) {
                    // Last interval
                    startOffset = highlightMap.lastKey();
                }
                endOffset = highlightMap.get(startOffset);
                sequence = startOffsets.indexOf(startOffset);
                Highlighter.Highlight hh = highlighter.getHighlights()[sequence];
                highlighter.removeHighlight(hh);
                highlighter.addHighlight(hh.getStartOffset(), hh.getEndOffset(), currentPainter);
                textArea.setCaretPosition(startOffset);
                curPos = startOffset;
            }
        }
        if (hits > 0) {
            label.setText(String.format("%02d / %02d%n", sequence + 1, hits));
        } else {
            label.setText("");
        }
    } catch (BadLocationException ex) {
        ex.printStackTrace();
    }
    findField.repaint();
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) JLabel(javax.swing.JLabel) Point(java.awt.Point) BadLocationException(javax.swing.text.BadLocationException) DefaultHighlighter(javax.swing.text.DefaultHighlighter) Highlighter(javax.swing.text.Highlighter)

Example 38 with Highlighter

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

the class AutoSpellChecker method checkElement.

/**
 * Check the spelling of the text of an element.
 *
 * @param element
 *            the to checking Element
 */
private void checkElement(final javax.swing.text.Element element) {
    try {
        if (!EventQueue.isDispatchThread()) {
            try {
                EventQueue.invokeAndWait(new Runnable() {

                    public void run() {
                        checkElement(element);
                        return;
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        final int i = element.getStartOffset();
        final int l = ((AbstractDocument) jText.getDocument()).getLength();
        final int j = Math.min(element.getEndOffset(), l);
        if (i >= j) {
            return;
        }
        // prevent a NPE if the dictionary is currently not loaded.
        final Dictionary dic = dictionary;
        final Locale loc = locale;
        if (dic == null || loc == null) {
            return;
        }
        final Tokenizer tok = new Tokenizer(jText, dic, loc, i, j, options);
        String word;
        final Highlighter highlighter = jText.getHighlighter();
        while ((word = tok.nextInvalidWord()) != null) {
            final int wordOffset = tok.getWordOffset();
            highlighter.addHighlight(wordOffset, wordOffset + word.length(), painter);
        }
    } catch (final BadLocationException e) {
        e.printStackTrace();
    }
}
Also used : Locale(java.util.Locale) AbstractDocument(javax.swing.text.AbstractDocument) BadLocationException(javax.swing.text.BadLocationException) BadLocationException(javax.swing.text.BadLocationException) Highlighter(javax.swing.text.Highlighter)

Example 39 with Highlighter

use of javax.swing.text.Highlighter in project pdfbox by apache.

the class Searcher method changeHighlighter.

private void changeHighlighter(int index, Highlighter.HighlightPainter newPainter) {
    Highlighter highlighter = textComponent.getHighlighter();
    Highlighter.Highlight highLight = highlights.get(index);
    highlighter.removeHighlight(highLight);
    try {
        highlighter.addHighlight(highLight.getStartOffset(), highLight.getEndOffset(), newPainter);
        highlights.set(index, highlighter.getHighlights()[highlighter.getHighlights().length - 1]);
    } catch (BadLocationException e) {
        LOG.error(e.getMessage(), e);
    }
}
Also used : BadLocationException(javax.swing.text.BadLocationException) DefaultHighlighter(javax.swing.text.DefaultHighlighter) Highlighter(javax.swing.text.Highlighter)

Example 40 with Highlighter

use of javax.swing.text.Highlighter in project CCDD by nasa.

the class CcddMacroHandler method highlightMacro.

/**
 ********************************************************************************************
 * Highlight any macros in the the specified text component
 *
 * @param component
 *            reference to the table cell renderer component
 *
 * @param text
 *            cell value
 *
 * @param hightlightColor
 *            color used for highlighting the macro name
 ********************************************************************************************
 */
protected void highlightMacro(Component component, String text, Color hightlightColor) {
    // Get a reference to the highlighter
    Highlighter highlighter = ((JTextComponent) component).getHighlighter();
    // Create a highlighter painter
    DefaultHighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(hightlightColor);
    // Remove any existing highlighting
    highlighter.removeAllHighlights();
    // Step through each macro location
    for (MacroLocation location : getMacroLocation(text)) {
        try {
            // Highlight the macro name in the text
            highlighter.addHighlight(location.getStart(), location.getStart() + location.getMacroName().length(), painter);
        } catch (BadLocationException ble) {
        // Ignore highlighting failure
        }
    }
}
Also used : JTextComponent(javax.swing.text.JTextComponent) DefaultHighlightPainter(javax.swing.text.DefaultHighlighter.DefaultHighlightPainter) BadLocationException(javax.swing.text.BadLocationException) 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