use of javax.swing.text.Highlighter in project bytecode-viewer by Konloch.
the class SystemErrConsole method highlight.
public void highlight(JTextComponent textComp, String pattern) {
if (pattern.isEmpty()) {
textComp.getHighlighter().removeAllHighlights();
return;
}
try {
Highlighter hilite = textComp.getHighlighter();
hilite.removeAllHighlights();
javax.swing.text.Document doc = textComp.getDocument();
String text = doc.getText(0, doc.getLength());
int pos = 0;
if (!check.isSelected()) {
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
hilite.addHighlight(pos, pos + pattern.length(), painter);
pos += pattern.length();
}
} catch (Exception e) {
new the.bytecode.club.bytecodeviewer.api.ExceptionUI(e);
}
}
use of javax.swing.text.Highlighter in project IBMiProgTool by vzupka.
the class DisplayFile method changeHighlight.
/**
* Find all matches and highlight it YELLOW (highlightPainter),
* then hihglight current match ORANGE for the text area.
*/
protected void changeHighlight() {
Highlighter highlighter = textArea.getHighlighter();
highlighter.removeAllHighlights();
findField.setBackground(Color.WHITE);
try {
Pattern pattern = getPattern();
if (pattern == null) {
return;
}
if (Objects.nonNull(pattern)) {
startOffsets = new ArrayList<>();
endOffsets = new ArrayList<>();
highlightMap.clear();
Matcher matcher = pattern.matcher(textArea.getText(0, textArea.getText().length()));
int pos = 0;
int start = 0;
int end = 0;
while (matcher.find(pos)) {
start = matcher.start();
end = matcher.end();
highlighter.addHighlight(start, end, highlightPainter);
startOffsets.add(start);
endOffsets.add(end);
highlightMap.put(start, end);
pos = end;
}
System.out.println("start: '" + start + "'");
}
JLabel label = layerUI.hint;
Highlighter.Highlight[] array = highlighter.getHighlights();
// number of highlighted intervals found.
int hits = array.length;
if (hits > 0) {
// If at least one interval was found.
if (direction.equals("forward")) {
// Forward direction
// Get next interval start - greater or equal
startOffset = highlightMap.ceilingKey(curPos);
if (startOffset == null) {
// First interval
startOffset = highlightMap.ceilingKey(0);
}
// This interval end
endOffset = highlightMap.get(startOffset);
// Sequence number of the interval
sequence = startOffsets.indexOf(startOffset);
Highlighter.Highlight hh = highlighter.getHighlights()[sequence];
highlighter.removeHighlight(hh);
highlighter.addHighlight(hh.getStartOffset(), hh.getEndOffset(), currentPainter);
textArea.setCaretPosition(startOffset);
curPos = startOffset;
} else {
// Backward direction
startOffset = highlightMap.floorKey(curPos);
if (startOffset == null) {
// Last interval
startOffset = highlightMap.lastKey();
}
endOffset = highlightMap.get(startOffset);
sequence = startOffsets.indexOf(startOffset);
Highlighter.Highlight hh = highlighter.getHighlights()[sequence];
highlighter.removeHighlight(hh);
highlighter.addHighlight(hh.getStartOffset(), hh.getEndOffset(), currentPainter);
textArea.setCaretPosition(startOffset);
curPos = startOffset;
}
}
if (hits > 0) {
label.setText(String.format("%02d / %02d%n", sequence + 1, hits));
} else {
label.setText("");
}
} catch (BadLocationException ex) {
ex.printStackTrace();
}
findField.repaint();
}
use of javax.swing.text.Highlighter in project freeplane by freeplane.
the class AutoSpellChecker method checkElement.
/**
* Check the spelling of the text of an element.
*
* @param element
* the to checking Element
*/
private void checkElement(final javax.swing.text.Element element) {
try {
if (!EventQueue.isDispatchThread()) {
try {
EventQueue.invokeAndWait(new Runnable() {
public void run() {
checkElement(element);
return;
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
final int i = element.getStartOffset();
final int l = ((AbstractDocument) jText.getDocument()).getLength();
final int j = Math.min(element.getEndOffset(), l);
if (i >= j) {
return;
}
// prevent a NPE if the dictionary is currently not loaded.
final Dictionary dic = dictionary;
final Locale loc = locale;
if (dic == null || loc == null) {
return;
}
final Tokenizer tok = new Tokenizer(jText, dic, loc, i, j, options);
String word;
final Highlighter highlighter = jText.getHighlighter();
while ((word = tok.nextInvalidWord()) != null) {
final int wordOffset = tok.getWordOffset();
highlighter.addHighlight(wordOffset, wordOffset + word.length(), painter);
}
} catch (final BadLocationException e) {
e.printStackTrace();
}
}
use of javax.swing.text.Highlighter in project pdfbox by apache.
the class Searcher method changeHighlighter.
private void changeHighlighter(int index, Highlighter.HighlightPainter newPainter) {
Highlighter highlighter = textComponent.getHighlighter();
Highlighter.Highlight highLight = highlights.get(index);
highlighter.removeHighlight(highLight);
try {
highlighter.addHighlight(highLight.getStartOffset(), highLight.getEndOffset(), newPainter);
highlights.set(index, highlighter.getHighlights()[highlighter.getHighlights().length - 1]);
} catch (BadLocationException e) {
LOG.error(e.getMessage(), e);
}
}
use of javax.swing.text.Highlighter in project CCDD by nasa.
the class CcddMacroHandler method highlightMacro.
/**
********************************************************************************************
* Highlight any macros in the the specified text component
*
* @param component
* reference to the table cell renderer component
*
* @param text
* cell value
*
* @param hightlightColor
* color used for highlighting the macro name
********************************************************************************************
*/
protected void highlightMacro(Component component, String text, Color hightlightColor) {
// Get a reference to the highlighter
Highlighter highlighter = ((JTextComponent) component).getHighlighter();
// Create a highlighter painter
DefaultHighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(hightlightColor);
// Remove any existing highlighting
highlighter.removeAllHighlights();
// Step through each macro location
for (MacroLocation location : getMacroLocation(text)) {
try {
// Highlight the macro name in the text
highlighter.addHighlight(location.getStart(), location.getStart() + location.getMacroName().length(), painter);
} catch (BadLocationException ble) {
// Ignore highlighting failure
}
}
}
Aggregations