use of javax.swing.text.StyledDocument 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;
}
use of javax.swing.text.StyledDocument in project java-swing-tips by aterai.
the class MainPanel method append.
// private static final String SEPARATOR = "\n";
// private void append_(String str, boolean flg) {
// MutableAttributeSet sas = null;
// if (!flg) {
// // sas = new SimpleAttributeSet(jtp.getCharacterAttributes());
// sas = new SimpleAttributeSet();
// StyleConstants.setForeground(sas, Color.RED);
// // StyleConstants.setBold(sas, true);
// // StyleConstants.setFontFamily(sas, Font.MONOSPACED);
// // StyleConstants.setFontSize(sas, 32);
// // StyleConstants.setForeground(sas, Color.GREEN);
// }
// try {
// Document doc = jtp.getDocument();
// doc.insertString(doc.getLength(), str + SEPARATOR, sas);
// jtp.setCaretPosition(doc.getLength());
// } catch (BadLocationException ex) {
// throw new RuntimeException(ex); // should never happen
// }
// }
private void append(String str, boolean flg) {
String style = flg ? StyleContext.DEFAULT_STYLE : "error";
StyledDocument doc = jtp.getStyledDocument();
try {
doc.insertString(doc.getLength(), str + "\n", doc.getStyle(style));
} catch (BadLocationException ex) {
// should never happen
RuntimeException wrap = new StringIndexOutOfBoundsException(ex.offsetRequested());
wrap.initCause(ex);
throw wrap;
}
}
use of javax.swing.text.StyledDocument in project groovy-core by groovy.
the class ConsoleSupport method addStylesToDocument.
protected void addStylesToDocument(JTextPane outputArea) {
StyledDocument doc = outputArea.getStyledDocument();
Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
Style regular = doc.addStyle("regular", def);
StyleConstants.setFontFamily(def, "Monospaced");
promptStyle = doc.addStyle("prompt", regular);
StyleConstants.setForeground(promptStyle, Color.BLUE);
commandStyle = doc.addStyle("command", regular);
StyleConstants.setForeground(commandStyle, Color.MAGENTA);
outputStyle = doc.addStyle("output", regular);
StyleConstants.setBold(outputStyle, true);
}
Aggregations