use of javax.swing.text.Highlighter in project java-swing-tips by aterai.
the class HighlightTableCellRenderer method getTableCellRendererComponent.
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
String txt = Objects.toString(value, "");
Highlighter highlighter = getHighlighter();
highlighter.removeAllHighlights();
setText(txt);
setBackground(isSelected ? SELECTION_BGC : Color.WHITE);
if (Objects.nonNull(pattern) && !pattern.isEmpty() && !Objects.equals(pattern, prev)) {
Matcher matcher = Pattern.compile(pattern).matcher(txt);
HighlightPainter highlightPainter = new DefaultHighlightPainter(Color.YELLOW);
int pos = 0;
while (matcher.find(pos) && !matcher.group().isEmpty()) {
int start = matcher.start();
int end = matcher.end();
try {
highlighter.addHighlight(start, end, highlightPainter);
} catch (BadLocationException | PatternSyntaxException ex) {
UIManager.getLookAndFeel().provideErrorFeedback(this);
}
pos = end;
}
}
return this;
}
use of javax.swing.text.Highlighter in project java-swing-tips by aterai.
the class ComboKeyHandler method makeComboBox.
private static JComboBox<String> makeComboBox(String... model) {
HighlightPainter highlightPainter = new DefaultHighlightPainter(Color.YELLOW);
return new JComboBox<String>(model) {
@Override
public void updateUI() {
super.updateUI();
JTextField field = new JTextField(" ");
field.setOpaque(true);
field.setBorder(BorderFactory.createEmptyBorder());
ListCellRenderer<? super String> renderer = getRenderer();
setRenderer((list, value, index, isSelected, cellHasFocus) -> {
String pattern = ((JTextField) getEditor().getEditorComponent()).getText();
if (index >= 0 && !pattern.isEmpty()) {
Highlighter highlighter = field.getHighlighter();
highlighter.removeAllHighlights();
String txt = Objects.toString(value, "");
field.setText(txt);
addHighlight(highlighter, Pattern.compile(pattern).matcher(txt));
field.setBackground(isSelected ? new Color(0xAA_64_AA_FF, true) : Color.WHITE);
return field;
}
return renderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
});
}
private void addHighlight(Highlighter highlighter, Matcher matcher) {
int pos = 0;
try {
while (matcher.find(pos) && !matcher.group().isEmpty()) {
int start = matcher.start();
int end = matcher.end();
highlighter.addHighlight(start, end, highlightPainter);
pos = end;
}
} catch (BadLocationException ex) {
// should never happen
RuntimeException wrap = new StringIndexOutOfBoundsException(ex.offsetRequested());
wrap.initCause(ex);
throw wrap;
}
}
};
}
use of javax.swing.text.Highlighter in project java-swing-tips by aterai.
the class MainPanel method addHighlight.
public void addHighlight(Element element, boolean isBlock) {
Highlighter highlighter = editorPane.getHighlighter();
int start = element.getStartOffset();
int lf = isBlock ? 1 : 0;
// lf???, setDrawsLayeredHighlights(false) bug???
int end = element.getEndOffset() - lf;
try {
highlighter.addHighlight(start, end, HIGHLIGHT);
} catch (BadLocationException ex) {
// should never happen
RuntimeException wrap = new StringIndexOutOfBoundsException(ex.offsetRequested());
wrap.initCause(ex);
throw wrap;
}
}
use of javax.swing.text.Highlighter in project cooja by contiki-ng.
the class CodeUI method displayLine.
/**
* Mark given line number in shown source code.
* Should be called from AWT thread.
*
* @param lineNumber Line number
*/
private void displayLine(int lineNumber, boolean markCurrent) {
try {
if (markCurrent) {
/* remove previous highlight */
Highlighter hl = codeEditor.getHighlighter();
hl.changeHighlight(currentLineTag, 0, 0);
}
if (lineNumber >= 0) {
final int start = codeEditorLines.get(lineNumber);
int end = codeEditorLines.get(lineNumber + 1);
if (markCurrent) {
/* highlight code */
Highlighter hl = codeEditor.getHighlighter();
hl.changeHighlight(currentLineTag, start, end);
}
/* ensure visible */
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
Rectangle r = codeEditor.modelToView(start);
if (r != null) {
codeEditor.scrollRectToVisible(codeEditor.modelToView(start));
}
} catch (BadLocationException e) {
}
}
});
}
} catch (Exception e) {
logger.warn("Error when highlighting current line: " + e.getMessage(), e);
}
}
use of javax.swing.text.Highlighter in project bytecode-viewer by Konloch.
the class JTextAreaUtils method highlight.
public static void highlight(JTextArea textArea, String pattern, boolean caseSensitiveSearch) {
if (pattern.isEmpty()) {
textArea.getHighlighter().removeAllHighlights();
return;
}
try {
Highlighter highlighter = textArea.getHighlighter();
highlighter.removeAllHighlights();
Document doc = textArea.getDocument();
String text = doc.getText(0, doc.getLength());
int pos = 0;
if (!caseSensitiveSearch) {
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
highlighter.addHighlight(pos, pos + pattern.length(), painter);
pos += pattern.length();
}
} catch (Exception e) {
BytecodeViewer.handleException(e);
}
}
Aggregations