Search in sources :

Example 56 with StyledDocument

use of javax.swing.text.StyledDocument in project opt4j by felixreimann.

the class Opt4JAbout method startup.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.opt4j.config.gui.Startupable#startup()
	 */
@Override
public void startup() {
    Container content = this;
    content.setLayout(new BorderLayout());
    JLabel logoLabel = new JLabel(Icons.getIcon("img/top_logo.png"));
    logoLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    JPanel logo = new JPanel(new BorderLayout());
    logo.setBackground(Color.WHITE);
    logo.add(logoLabel);
    content.add(logo, BorderLayout.PAGE_START);
    JTextPane license = new JTextPane();
    license.setEditable(false);
    final JScrollPane licenseScroll = new JScrollPane(license);
    licenseScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    StyledDocument doc = license.getStyledDocument();
    Style regular = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
    try {
        doc.insertString(doc.getLength(), LICENSE_TEXT, regular);
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
    license.setPreferredSize(new Dimension(360, 100));
    content.add(licenseScroll, BorderLayout.CENTER);
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            licenseScroll.getVerticalScrollBar().setValue(0);
        }
    });
    JPanel footer = new JPanel(new BorderLayout());
    footer.setBackground(Color.WHITE);
    // Add Copyright & Credits
    String copyright = "<html>Build " + Opt4J.getDateISO() + " <br /> Version " + Opt4J.getVersion() + "   \u00a9 Opt4J.org 2007</html>";
    JLabel copyrightLabel = new JLabel(copyright);
    copyrightLabel.setHorizontalAlignment(SwingConstants.CENTER);
    copyrightLabel.setVerticalAlignment(SwingConstants.BOTTOM);
    copyrightLabel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
    footer.add(copyrightLabel, BorderLayout.EAST);
    String credits = "<html><p>Credits:<br />";
    for (String author : AUTHORS) {
        credits += author + "<br/>";
    }
    credits += "</p></html>";
    JLabel creditsLabel = new JLabel(credits);
    creditsLabel.setHorizontalAlignment(SwingConstants.CENTER);
    creditsLabel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
    footer.add(creditsLabel, BorderLayout.WEST);
    content.add(footer, BorderLayout.PAGE_END);
}
Also used : JScrollPane(javax.swing.JScrollPane) JPanel(javax.swing.JPanel) StyledDocument(javax.swing.text.StyledDocument) JLabel(javax.swing.JLabel) Dimension(java.awt.Dimension) JTextPane(javax.swing.JTextPane) Container(java.awt.Container) BorderLayout(java.awt.BorderLayout) Style(javax.swing.text.Style) BadLocationException(javax.swing.text.BadLocationException)

Example 57 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)

Example 58 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 59 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 60 with StyledDocument

use of javax.swing.text.StyledDocument in project litiengine by gurkenlabs.

the class LogHandler method flush.

@Override
public void flush() {
    StyledDocument doc = textPane.getStyledDocument();
    try {
        doc.remove(0, doc.getLength());
    } catch (BadLocationException e) {
    // if an exception occurs while logging, just ignore it
    }
    textPane.setCaretPosition(doc.getLength());
}
Also used : StyledDocument(javax.swing.text.StyledDocument) BadLocationException(javax.swing.text.BadLocationException)

Aggregations

StyledDocument (javax.swing.text.StyledDocument)63 BadLocationException (javax.swing.text.BadLocationException)29 Style (javax.swing.text.Style)18 SimpleAttributeSet (javax.swing.text.SimpleAttributeSet)12 Point (java.awt.Point)7 JTextPane (javax.swing.JTextPane)7 DefaultStyledDocument (javax.swing.text.DefaultStyledDocument)7 ArrayList (java.util.ArrayList)4 JLabel (javax.swing.JLabel)4 Test (org.junit.jupiter.api.Test)4 Font (java.awt.Font)3 LogRecord (java.util.logging.LogRecord)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