Java Swing Text Style overrideStyle(Style originalStyle, Style newStyle)

Here you can find the source of overrideStyle(Style originalStyle, Style newStyle)

Description

override Style

License

Open Source License

Declaration

public static void overrideStyle(Style originalStyle, 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);
        }/* w  w w .j  a  v  a  2s.  c  o  m*/

        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. getIntegerValue(Element element, Object constant)
  2. getNextVisualPosition(final View v, final int pos, final Position.Bias b0, final int direction, final Position.Bias[] biasRet)
  3. hasDifferentCharacterAttributes(AttributeSet style, AttributeSet base)
  4. isCentered(AttributeSet attributes)
  5. isLTR(final int level)
  6. resolveAttributes(AttributeSet style)
  7. unRTF(String s)