Example usage for javax.swing.text StyleContext StyleContext

List of usage examples for javax.swing.text StyleContext StyleContext

Introduction

In this page you can find the example usage for javax.swing.text StyleContext StyleContext.

Prototype

public StyleContext() 

Source Link

Document

Creates a new StyleContext object.

Usage

From source file:StylesExample8.java

public static void main(String[] args) {
    try {/*from   w  w w .  j  av a  2  s  .  c o m*/
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception evt) {
    }

    JFrame f = new JFrame("Styles Example 8");

    // Create the StyleContext, the document and the pane
    final StyleContext sc = new StyleContext();
    final DefaultStyledDocument doc = new DefaultStyledDocument(sc);
    final JTextPane pane = new JTextPane(doc);

    // Build the styles
    createDocumentStyles(sc);

    try {
        // Add the text and apply the styles
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                // Add the text
                addText(pane, sc, sc.getStyle(mainStyleName), content);
            }
        });
    } catch (Exception e) {
        System.out.println("Exception when constructing document: " + e);
        System.exit(1);
    }

    f.getContentPane().add(new JScrollPane(pane));
    f.setSize(500, 300);
    f.setVisible(true);
}

From source file:TextPaneElements.java

public static void main(String[] args) {
    try {/*  www .  ja v  a  2  s  .  c  o m*/
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception evt) {
    }

    JFrame f = new JFrame("Text Pane Elements");

    // Create the StyleContext, the document and the pane
    final StyleContext sc = new StyleContext();
    final DefaultStyledDocument doc = new DefaultStyledDocument(sc);
    final JTextPane pane = new JTextPane(doc);

    // Build the styles
    createDocumentStyles(sc);

    try {
        // Add the text and apply the styles
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                // Add the text
                addText(pane, sc, sc.getStyle(mainStyleName), content);

                // Dump the element structure
                ((AbstractDocument) pane.getDocument()).dump(System.out);
            }
        });
    } catch (Exception e) {
        System.out.println("Exception when constructing document: " + e);
        System.exit(1);
    }

    f.getContentPane().add(new JScrollPane(pane));
    f.setSize(500, 300);
    f.setVisible(true);
}

From source file:ExtendedParagraphExample.java

public static void main(String[] args) {
    try {/*from   w  ww  .j  a va2  s. co m*/
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception evt) {
    }

    JFrame f = new JFrame("Extended Paragraph Example");

    // Create the StyleContext, the document and the pane
    final StyleContext sc = new StyleContext();
    final DefaultStyledDocument doc = new DefaultStyledDocument(sc);
    final JTextPane pane = new JTextPane(doc);
    pane.setEditorKit(new ExtendedStyledEditorKit());

    try {
        // Add the text and apply the styles
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                // Build the styles
                createDocumentStyles(sc);

                // Add the text
                addText(pane, sc, sc.getStyle(mainStyleName), content);
            }
        });
    } catch (Exception e) {
        System.out.println("Exception when constructing document: " + e);
        System.exit(1);
    }

    f.getContentPane().add(new JScrollPane(pane));
    f.setSize(400, 300);
    f.setVisible(true);
}

From source file:com.croer.javaorange.diviner.SimpleOrangeTextPane.java

public SimpleOrangeTextPane() {
    CONFIGURATION = Configuration.getCONFIGURATION();

    //Create the style array to show the colors 
    List<Object> colorList = CONFIGURATION.getList("ColorWord");
    styles = new Style[colorList.size()];
    StyleContext sc = new StyleContext();
    for (int i = 0; i < colorList.size(); i++) {
        styles[i] = sc.addStyle((String) colorList.get(i), sc.getStyle(StyleContext.DEFAULT_STYLE));
        StyleConstants.setForeground(styles[i], ColorUtils.getColorByName((String) colorList.get(i)));
        StyleConstants.setBold(styles[i], true);
    }/*from w w w .j ava  2  s  . c o  m*/

    //Deactive key bindings 
    List<Object> navigationList = CONFIGURATION.getList("PageNavigation");
    for (Object object : navigationList) {
        getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(object.toString()), "none");
    }

    //Get the document for adding a document listener
    dsd = (DefaultStyledDocument) getDocument();
    dsd.addDocumentListener(new DocumentListenerTextPane());

    //...and setting a document filter
    documentFilter = new MyDocumentFilter();
    dsd.setDocumentFilter(documentFilter);
}

From source file:ome.formats.importer.gui.GuiCommonElements.java

/**
 * Add a text pane to the parent container
 * //from  ww w .  j a  va  2  s.  com
 * @param container - parent container
 * @param text - text for new Text Pane
 * @param placement - TableLayout placement within the parent container
 * @param debug - turn on/off red debug borders
 * @return new JTextPane
 */
public static synchronized JTextPane addTextPane(Container container, String text, String placement,
        boolean debug) {
    StyleContext context = new StyleContext();
    StyledDocument document = new DefaultStyledDocument(context);

    Style style = context.getStyle(StyleContext.DEFAULT_STYLE);
    StyleConstants.setAlignment(style, StyleConstants.ALIGN_LEFT);

    try {
        document.insertString(document.getLength(), text, null);
    } catch (BadLocationException e) {
        log.error("BadLocationException inserting text to document.");
    }

    JTextPane textPane = new JTextPane(document);
    textPane.setOpaque(false);
    textPane.setEditable(false);
    textPane.setFocusable(false);
    container.add(textPane, placement);

    if (debug == true)
        textPane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.red),
                textPane.getBorder()));

    return textPane;
}

From source file:org.openmicroscopy.shoola.util.ui.UIUtilities.java

/**
 * Formats the text and displays it in a {@link JTextPane}.
 * /*from w ww .j a  va2s . c  o  m*/
 * @param text          The text to display.
 * @param foreground   The foreground color.
 * @return See above.
 */
public static JTextPane buildTextPane(String text, Color foreground) {
    if (text == null)
        text = "";
    StyleContext context = new StyleContext();
    StyledDocument document = new DefaultStyledDocument(context);

    Style style = context.getStyle(StyleContext.DEFAULT_STYLE);
    StyleConstants.setAlignment(style, StyleConstants.ALIGN_LEFT);
    if (foreground != null)
        StyleConstants.setForeground(style, foreground);
    try {
        document.insertString(document.getLength(), text, style);
    } catch (BadLocationException e) {
    }

    JTextPane textPane = new JTextPane(document);
    textPane.setOpaque(false);
    textPane.setEditable(false);
    textPane.setFocusable(false);
    return textPane;
}

From source file:org.openmicroscopy.shoola.util.ui.UIUtilities.java

/** Builds the UI component displaying the exception.*/
public static JTextPane buildExceptionArea() {
    StyleContext context = new StyleContext();
    StyledDocument document = new DefaultStyledDocument(context);

    JTextPane textPane = new JTextPane(document);
    textPane.setOpaque(false);/* www.j  av  a 2 s. c o  m*/
    textPane.setEditable(false);

    // Create one of each type of tab stop
    List<TabStop> list = new ArrayList<TabStop>();

    // Create a left-aligned tab stop at 100 pixels from the left margin
    float pos = 15;
    int align = TabStop.ALIGN_LEFT;
    int leader = TabStop.LEAD_NONE;
    TabStop tstop = new TabStop(pos, align, leader);
    list.add(tstop);

    // Create a right-aligned tab stop at 200 pixels from the left margin
    pos = 15;
    align = TabStop.ALIGN_RIGHT;
    leader = TabStop.LEAD_NONE;
    tstop = new TabStop(pos, align, leader);
    list.add(tstop);

    // Create a center-aligned tab stop at 300 pixels from the left margin
    pos = 15;
    align = TabStop.ALIGN_CENTER;
    leader = TabStop.LEAD_NONE;
    tstop = new TabStop(pos, align, leader);
    list.add(tstop);

    // Create a decimal-aligned tab stop at 400 pixels from the left margin
    pos = 15;
    align = TabStop.ALIGN_DECIMAL;
    leader = TabStop.LEAD_NONE;
    tstop = new TabStop(pos, align, leader);
    list.add(tstop);

    // Create a tab set from the tab stops
    TabSet tabs = new TabSet(list.toArray(new TabStop[0]));

    // Add the tab set to the logical style;
    // the logical style is inherited by all paragraphs
    Style style = textPane.getLogicalStyle();
    StyleConstants.setTabSet(style, tabs);
    textPane.setLogicalStyle(style);
    Style debugStyle = document.addStyle("StyleName", null);
    StyleConstants.setForeground(debugStyle, Color.BLACK);
    StyleConstants.setFontFamily(debugStyle, "SansSerif");
    StyleConstants.setFontSize(debugStyle, 12);
    StyleConstants.setBold(debugStyle, false);
    return textPane;
}

From source file:pl.otros.logview.gui.message.pattern.PropertyPatternMessageColorizer.java

@Override
public Collection<MessageFragmentStyle> colorize(String message) throws BadLocationException {
    Collection<MessageFragmentStyle> list = new ArrayList<MessageFragmentStyle>();
    StyleContext styleContext = new StyleContext();
    for (int i = 0; i <= groupCount; i++) {
        if (StyleProperties.isStyleForGroupDeclared(i, configuration)) {
            style = StyleProperties.getStyle(styleContext, configuration, "propStyle" + getName(), i);
            list.addAll(MessageColorizerUtils.colorizeRegex(style, message, pattern, i));
        }/* w w w  . ja  v  a 2s . co m*/
    }

    return list;
}

From source file:pl.otros.logview.gui.message.SearchResultColorizer.java

@Override
public Collection<MessageFragmentStyle> colorize(String textToColorize) throws BadLocationException {
    Collection<MessageFragmentStyle> list = new ArrayList<MessageFragmentStyle>();
    if (StringUtils.isEmpty(searchString)) {
        return list;
    }/*from   www .  j av  a 2  s  .co  m*/
    StyleContext sc = new StyleContext();
    Style searchStyle = sc.addStyle("searchResult", sc.getStyle(StyleContext.DEFAULT_STYLE));
    StyleConstants.setBackground(searchStyle, color);
    if (searchMode.equals(SearchMode.STRING_CONTAINS)) {
        list.addAll(colorizeString(textToColorize, searchStyle, searchString));
    } else if (searchMode.equals(SearchMode.REGEX)) {
        list.addAll(MessageColorizerUtils.colorizeRegex(searchStyle, textToColorize,
                Pattern.compile(searchString, Pattern.CASE_INSENSITIVE), 0));
    }
    for (MessageFragmentStyle style : list) {
        style.setSearchResult(true);
    }
    return list;
}

From source file:pl.otros.logview.gui.message.update.LogDataFormatter.java

public LogDataFormatter(LogData logData, //
        DateFormat dateFormat, //
        MessageUpdateUtils messageUtils, //
        PluginableElementsContainer<MessageColorizer> colorizersContainer, //
        PluginableElementsContainer<MessageFormatter> formattersContainer, //
        CancelStatus cancelStatus, int maximumMessageSize) {
    this.ld = logData;
    this.dateFormat = dateFormat;
    this.messageUtils = messageUtils;
    this.colorizersContainer = colorizersContainer;
    this.formattersContainer = formattersContainer;
    this.cancelStatus = cancelStatus;
    this.maximumMessageSize = maximumMessageSize;

    sc = new StyleContext();
    defaultStyle = sc.getStyle(StyleContext.DEFAULT_STYLE);
    mainStyle = sc.addStyle("MainStyle", defaultStyle);
    StyleConstants.setFontSize(mainStyle, 12);
    StyleConstants.setForeground(mainStyle, Color.BLACK);

    classMethodStyle = sc.addStyle("classMethod", null);
    StyleConstants.setFontFamily(classMethodStyle, "monospaced");
    StyleConstants.setForeground(classMethodStyle, Color.BLUE);
    boldArialStyle = sc.addStyle("note", mainStyle);
    StyleConstants.setFontFamily(boldArialStyle, "arial");
    StyleConstants.setBold(boldArialStyle, true);

    propertyNameStyle = sc.addStyle("propertyValue", classMethodStyle);
    StyleConstants.setForeground(propertyNameStyle, new Color(0, 0, 128));

    propertyValueStyle = sc.addStyle("propertyValue", classMethodStyle);
    StyleConstants.setForeground(propertyNameStyle, new Color(0, 128, 0));

}