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();
}
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();
}
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;
}
Aggregations