Search in sources :

Example 51 with StyledDocument

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;
}
Also used : Matcher(java.util.regex.Matcher) StyledDocument(javax.swing.text.StyledDocument) Style(javax.swing.text.Style) BadLocationException(javax.swing.text.BadLocationException) Highlighter(javax.swing.text.Highlighter)

Example 52 with StyledDocument

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;
    }
}
Also used : StyledDocument(javax.swing.text.StyledDocument) BadLocationException(javax.swing.text.BadLocationException)

Example 53 with StyledDocument

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);
}
Also used : StyledDocument(javax.swing.text.StyledDocument) Style(javax.swing.text.Style)

Aggregations

StyledDocument (javax.swing.text.StyledDocument)53 BadLocationException (javax.swing.text.BadLocationException)24 Style (javax.swing.text.Style)16 SimpleAttributeSet (javax.swing.text.SimpleAttributeSet)11 Point (java.awt.Point)8 JLabel (javax.swing.JLabel)4 JTextPane (javax.swing.JTextPane)4 DefaultStyledDocument (javax.swing.text.DefaultStyledDocument)4 Font (java.awt.Font)3 ArrayList (java.util.ArrayList)3 Matcher (java.util.regex.Matcher)3 ImageIcon (javax.swing.ImageIcon)3 JPanel (javax.swing.JPanel)3 PersistentArrayMap (clojure.lang.PersistentArrayMap)2 BorderLayout (java.awt.BorderLayout)2 Dimension (java.awt.Dimension)2 Rectangle (java.awt.Rectangle)2 IOException (java.io.IOException)2 Pattern (java.util.regex.Pattern)2 Element (javax.swing.text.Element)2