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