Java JTextPane overrideStyle(String styleName, JTextPane textPane, Style newStyle)

Here you can find the source of overrideStyle(String styleName, JTextPane textPane, Style newStyle)

Description

override Style

License

Open Source License

Declaration

public static void overrideStyle(String styleName, JTextPane textPane,
            Style newStyle) 

Method Source Code

//package com.java2s;

import javax.swing.JTextPane;

import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

public class Main {
    protected static StyleContext styleContext = new StyleContext();

    public static void overrideStyle(String styleName,
            StyledDocument document, Style newStyle) {
        Style style = document.getStyle(styleName);
        if (style == null) {
            style = styleContext.addStyle(styleName, null);
            document.addStyle(styleName, style);
        }/*from   w ww.  j  a  va2  s . c om*/

        overrideStyle(style, newStyle);
    }

    public static void overrideStyle(String styleName, JTextPane textPane,
            Style newStyle) {
        Style style = textPane.getStyle(styleName);
        if (style == null) {
            style = styleContext.addStyle(styleName, null);
            textPane.addStyle(styleName, style);
        }

        overrideStyle(style, newStyle);
    }

    public static void overrideStyle(Style originalStyle, Style newStyle) {
        overrideStyleElement(originalStyle, newStyle,
                StyleConstants.FontFamily);
        overrideStyleElement(originalStyle, newStyle, StyleConstants.Family);
        overrideStyleElement(originalStyle, newStyle,
                StyleConstants.FontSize);
        overrideStyleElement(originalStyle, newStyle, StyleConstants.Size);
        overrideStyleElement(originalStyle, newStyle,
                StyleConstants.Foreground);
        overrideStyleElement(originalStyle, newStyle,
                StyleConstants.Background);

        overrideStyleElement(originalStyle, newStyle, StyleConstants.Bold);
        overrideStyleElement(originalStyle, newStyle, StyleConstants.Italic);
        overrideStyleElement(originalStyle, newStyle,
                StyleConstants.StrikeThrough);
        overrideStyleElement(originalStyle, newStyle,
                StyleConstants.Subscript);
        overrideStyleElement(originalStyle, newStyle,
                StyleConstants.Superscript);
        overrideStyleElement(originalStyle, newStyle,
                StyleConstants.Underline);

        overrideStyleElement(originalStyle, newStyle,
                StyleConstants.RightIndent);
        overrideStyleElement(originalStyle, newStyle,
                StyleConstants.LeftIndent);
        overrideStyleElement(originalStyle, newStyle,
                StyleConstants.SpaceAbove);
        overrideStyleElement(originalStyle, newStyle,
                StyleConstants.SpaceBelow);
        overrideStyleElement(originalStyle, newStyle,
                StyleConstants.FirstLineIndent);
        overrideStyleElement(originalStyle, newStyle,
                StyleConstants.LineSpacing);

        overrideStyleElement(originalStyle, newStyle,
                StyleConstants.Alignment);
        overrideStyleElement(originalStyle, newStyle,
                StyleConstants.BidiLevel);
        overrideStyleElement(originalStyle, newStyle,
                StyleConstants.Orientation);
        overrideStyleElement(originalStyle, newStyle, StyleConstants.TabSet);
    }

    public static boolean overrideStyleElement(Style originalStyle,
            Style newStyle, Object key) {
        //        System.out.println( "DocumentUtils: setting " + key + " to " + newStyle.getAttribute( key ) );

        originalStyle.removeAttribute(key);

        Object attribute = newStyle.getAttribute(key);
        if (attribute != null) {
            originalStyle.addAttribute(key, attribute);
            return true;
        }

        return false;
    }
}

Related

  1. createTextPaneScrollPane(JTextPane textPane)
  2. getFontFamily(JTextPane textPane)
  3. getForegroundColor(JTextPane textPane)
  4. insertStyledString(JTextPane textPane, String text, SimpleAttributeSet attrSet, Color background, Color foreground, String fontFamily, int fontSize, boolean bold, boolean italic, boolean underline, boolean strikeThrough, Boolean superscript)
  5. isSelectionBold(JTextPane textPane)
  6. saveFile(JTextPane txt)
  7. scrollTo(JTextPane textPane, int position)
  8. setDefaultTextPaneProperties(JTextPane textPane)
  9. setJTextPaneFont(JTextPane jtp, Font font, Color c)