Search in sources :

Example 61 with Highlighter

use of javax.swing.text.Highlighter in project omero-insight by ome.

the class TextFormatter method removeHighlights.

/**
 * Remove the highlights from the TextComponent.
 * @param textComp see above.
 */
public static void removeHighlights(JTextComponent textComp) {
    Highlighter hilite = textComp.getHighlighter();
    Highlighter.Highlight[] hilites = hilite.getHighlights();
    for (int i = 0; i < hilites.length; i++) if (hilites[i].getPainter() instanceof FormatHighlighter)
        hilite.removeHighlight(hilites[i]);
}
Also used : Highlighter(javax.swing.text.Highlighter)

Example 62 with Highlighter

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

the class CodeUI method updateBreakpoints.

public void updateBreakpoints() {
    Highlighter hl = codeEditor.getHighlighter();
    for (Object breakpointsLineTag : breakpointsLineTags) {
        hl.removeHighlight(breakpointsLineTag);
    }
    breakpointsLineTags.clear();
    for (Watchpoint w : mote.getBreakpoints()) {
        if (!w.getCodeFile().equals(displayedFile)) {
            continue;
        }
        final int start = codeEditorLines.get(w.getLineNumber());
        int end = codeEditorLines.get(w.getLineNumber() + 1);
        try {
            breakpointsLineTags.add(hl.addHighlight(start, end, BREAKPOINTS_MARKER));
        } catch (BadLocationException e1) {
        }
    }
}
Also used : Watchpoint(org.contikios.cooja.Watchpoint) 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) Highlighter(javax.swing.text.Highlighter)

Example 63 with Highlighter

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

the class MainPanel method setHighlight.

public static void setHighlight(JTextComponent tc, String pattern) {
    removeWordHighlights(tc);
    try {
        Highlighter highlighter = tc.getHighlighter();
        Document doc = tc.getDocument();
        String text = doc.getText(0, doc.getLength());
        Matcher matcher = Pattern.compile(pattern).matcher(text);
        int pos = 0;
        while (matcher.find(pos) && !matcher.group().isEmpty()) {
            pos = matcher.end();
            highlighter.addHighlight(matcher.start(), pos, HIGHLIGHT);
        }
    } catch (BadLocationException | PatternSyntaxException ex) {
        UIManager.getLookAndFeel().provideErrorFeedback(tc);
    }
}
Also used : Matcher(java.util.regex.Matcher) Document(javax.swing.text.Document) BadLocationException(javax.swing.text.BadLocationException) Highlighter(javax.swing.text.Highlighter) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 64 with Highlighter

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

the class MetalHighlightScrollBarUI method setHighlight.

public static void setHighlight(JTextComponent jtc, String pattern) {
    Highlighter highlighter = jtc.getHighlighter();
    highlighter.removeAllHighlights();
    Document doc = jtc.getDocument();
    try {
        String text = doc.getText(0, doc.getLength());
        Matcher matcher = Pattern.compile(pattern).matcher(text);
        int pos = 0;
        while (matcher.find(pos) && !matcher.group().isEmpty()) {
            int start = matcher.start();
            int end = matcher.end();
            highlighter.addHighlight(start, end, HIGHLIGHT);
            pos = end;
        }
    } catch (BadLocationException | PatternSyntaxException ex) {
        UIManager.getLookAndFeel().provideErrorFeedback(jtc);
    }
}
Also used : Matcher(java.util.regex.Matcher) Document(javax.swing.text.Document) BadLocationException(javax.swing.text.BadLocationException) Highlighter(javax.swing.text.Highlighter) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 65 with Highlighter

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

the class MetalHighlightScrollBarUI method paintTrack.

@Override
protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) {
    super.paintTrack(g, c, trackBounds);
    Rectangle rect = textArea.getBounds();
    double sy = trackBounds.getHeight() / rect.getHeight();
    AffineTransform at = AffineTransform.getScaleInstance(1d, sy);
    Highlighter highlighter = textArea.getHighlighter();
    g.setColor(Color.YELLOW);
    try {
        for (Highlighter.Highlight hh : highlighter.getHighlights()) {
            Rectangle r = textArea.modelToView(hh.getStartOffset());
            // Java 9: Rectangle r = textArea.modelToView2D(hh.getStartOffset()).getBounds();
            Rectangle s = at.createTransformedShape(r).getBounds();
            // Math.max(2, s.height - 2);
            int h = 2;
            g.fillRect(trackBounds.x, trackBounds.y + s.y, trackBounds.width, h);
        }
    } catch (BadLocationException ex) {
        // should never happen
        RuntimeException wrap = new StringIndexOutOfBoundsException(ex.offsetRequested());
        wrap.initCause(ex);
        throw wrap;
    }
}
Also used : AffineTransform(java.awt.geom.AffineTransform) BadLocationException(javax.swing.text.BadLocationException) 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