Search in sources :

Example 66 with Highlighter

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

the class MetalHighlightScrollBarUI method paintIcon.

@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
    // Rectangle rect = textArea.getBounds();
    // Dimension sbSize = scrollbar.getSize();
    // Insets sbInsets = scrollbar.getInsets();
    // double sy = (sbSize.height - sbInsets.top - sbInsets.bottom) / rect.getHeight();
    int top = scrollbar.getInsets().top;
    BoundedRangeModel range = scrollbar.getModel();
    double sy = range.getExtent() / (double) (range.getMaximum() - range.getMinimum());
    AffineTransform at = AffineTransform.getScaleInstance(1d, sy);
    Highlighter highlighter = textArea.getHighlighter();
    // paint Highlight
    Graphics2D g2 = (Graphics2D) g.create();
    g2.translate(x, y);
    g2.setPaint(Color.RED);
    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;
            g2.fillRect(0, top + s.y, getIconWidth(), h);
        }
    } catch (BadLocationException ex) {
        // should never happen
        RuntimeException wrap = new StringIndexOutOfBoundsException(ex.offsetRequested());
        wrap.initCause(ex);
        throw wrap;
    }
    // paint Thumb
    if (scrollbar.isVisible()) {
        // JViewport vport = Objects.requireNonNull(
        // (JViewport) SwingUtilities.getAncestorOfClass(JViewport.class, textArea));
        // Rectangle thumbRect = vport.getBounds();
        thumbRect.height = range.getExtent();
        // vport.getViewPosition().y;
        thumbRect.y = range.getValue();
        g2.setColor(THUMB_COLOR);
        Rectangle s = at.createTransformedShape(thumbRect).getBounds();
        g2.fillRect(0, top + s.y, getIconWidth(), s.height);
    }
    g2.dispose();
}
Also used : AffineTransform(java.awt.geom.AffineTransform) BadLocationException(javax.swing.text.BadLocationException) Highlighter(javax.swing.text.Highlighter)

Example 67 with Highlighter

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

the class CentredBackgroundBorder method setHighlight.

// https://ateraimemo.com/Swing/Highlighter.html
public static void setHighlight(JTextComponent jtc, String pattern, HighlightPainter painter) {
    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, painter);
            pos = end;
        }
    } catch (BadLocationException | PatternSyntaxException ex) {
        UIManager.getLookAndFeel().provideErrorFeedback(jtc);
    }
    jtc.repaint();
}
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 68 with Highlighter

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

the class PlaceholderLayerUI method changeHighlight.

/* default */
int changeHighlight(int index) {
    field.setBackground(Color.WHITE);
    StyledDocument doc = textPane.getStyledDocument();
    Style s = doc.getStyle("highlight-text-foreground");
    Style def = doc.getStyle(StyleContext.DEFAULT_STYLE);
    // clear the previous highlight:
    Highlighter highlighter = textPane.getHighlighter();
    for (Highlighter.Highlight h : highlighter.getHighlights()) {
        int start = h.getStartOffset();
        int end = h.getEndOffset();
        doc.setCharacterAttributes(start, end - start, def, true);
    }
    highlighter.removeAllHighlights();
    // doc.setCharacterAttributes(0, doc.getLength(), def, true);
    // match highlighting:
    getPattern().ifPresent(pattern -> {
        try {
            Matcher matcher = pattern.matcher(doc.getText(0, doc.getLength()));
            int pos = 0;
            while (matcher.find(pos) && !matcher.group().isEmpty()) {
                int start = matcher.start();
                int end = matcher.end();
                highlighter.addHighlight(start, end, matchedPainter);
                // doc.setCharacterAttributes(start, end - start, red, true);
                pos = end;
            }
        } catch (BadLocationException ex) {
            // should never happen
            RuntimeException wrap = new StringIndexOutOfBoundsException(ex.offsetRequested());
            wrap.initCause(ex);
            throw wrap;
        }
    });
    JLabel label = layerUI.hint;
    Highlighter.Highlight[] array = highlighter.getHighlights();
    int hits = array.length;
    int idx = index;
    if (hits == 0) {
        idx = -1;
        label.setOpaque(true);
    } else {
        idx = (idx + hits) % hits;
        label.setOpaque(false);
        Highlighter.Highlight hh = highlighter.getHighlights()[idx];
        highlighter.removeHighlight(hh);
        int start = hh.getStartOffset();
        int end = hh.getEndOffset();
        try {
            highlighter.addHighlight(start, end, currentPainter);
            doc.setCharacterAttributes(start, end - start, s, true);
            scrollToCenter(textPane, start);
        } catch (BadLocationException ex) {
            // should never happen
            RuntimeException wrap = new StringIndexOutOfBoundsException(ex.offsetRequested());
            wrap.initCause(ex);
            throw wrap;
        }
    }
    label.setText(String.format("%02d / %02d%n", idx + 1, hits));
    field.repaint();
    return idx;
}
Also used : Matcher(java.util.regex.Matcher) StyledDocument(javax.swing.text.StyledDocument) Style(javax.swing.text.Style) 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