852 lines · 23.6 KB · Java
RawDownload
/* * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.  Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */package javax.swing.text; import java.awt.Color;import java.awt.Component;import java.awt.Toolkit;import javax.swing.Icon; /** * <p> * A collection of <em>well known</em> or common attribute keys * and methods to apply to an AttributeSet or MutableAttributeSet * to get/set the properties in a typesafe manner. * <p> * The paragraph attributes form the definition of a paragraph to be rendered. * All sizes are specified in points (such as found in postscript), a * device independent measure. * </p> * <p align=center><img src="doc-files/paragraph.gif" * alt="Diagram shows SpaceAbove, FirstLineIndent, LeftIndent, RightIndent, *      and SpaceBelow a paragraph."></p> * <p> * * @author  Timothy Prinzing */public class StyleConstants {     /**     * Name of elements used to represent components.     */    public static final String ComponentElementName = "component";     /**     * Name of elements used to represent icons.     */    public static final String IconElementName = "icon";     /**     * Attribute name used to name the collection of     * attributes.     */    public static final Object NameAttribute = new StyleConstants("name");     /**     * Attribute name used to identifiy the resolving parent     * set of attributes, if one is defined.     */    public static final Object ResolveAttribute = new StyleConstants("resolver");     /**     * Attribute used to identify the model for embedded     * objects that have a model view separation.     */    public static final Object ModelAttribute = new StyleConstants("model");     /**     * Returns the string representation.     *     * @return the string     */    public String toString() {        return representation;    }     // ---- character constants -----------------------------------     /**     * Bidirectional level of a character as assigned by the Unicode bidi     * algorithm.     */    public static final Object BidiLevel = new CharacterConstants("bidiLevel");     /**     * Name of the font family.     */    public static final Object FontFamily = new FontConstants("family");     /**     * Name of the font family.     *     * @since 1.5     */    public static final Object Family = FontFamily;     /**     * Name of the font size.     */    public static final Object FontSize = new FontConstants("size");     /**     * Name of the font size.     *     * @since 1.5     */    public static final Object Size = FontSize;     /**     * Name of the bold attribute.     */    public static final Object Bold = new FontConstants("bold");     /**     * Name of the italic attribute.     */    public static final Object Italic = new FontConstants("italic");     /**     * Name of the underline attribute.     */    public static final Object Underline = new CharacterConstants("underline");     /**     * Name of the Strikethrough attribute.     */    public static final Object StrikeThrough = new CharacterConstants("strikethrough");     /**     * Name of the Superscript attribute.     */    public static final Object Superscript = new CharacterConstants("superscript");     /**     * Name of the Subscript attribute.     */    public static final Object Subscript = new CharacterConstants("subscript");     /**     * Name of the foreground color attribute.     */    public static final Object Foreground = new ColorConstants("foreground");     /**     * Name of the background color attribute.     */    public static final Object Background = new ColorConstants("background");     /**     * Name of the component attribute.     */    public static final Object ComponentAttribute = new CharacterConstants("component");     /**     * Name of the icon attribute.     */    public static final Object IconAttribute = new CharacterConstants("icon");     /**     * Name of the input method composed text attribute. The value of     * this attribute is an instance of AttributedString which represents     * the composed text.     */    public static final Object ComposedTextAttribute = new StyleConstants("composed text");     /**     * The amount of space to indent the first     * line of the paragraph.  This value may be negative     * to offset in the reverse direction.  The type     * is Float and specifies the size of the space     * in points.     */    public static final Object FirstLineIndent = new ParagraphConstants("FirstLineIndent");     /**     * The amount to indent the left side     * of the paragraph.     * Type is float and specifies the size in points.     */    public static final Object LeftIndent = new ParagraphConstants("LeftIndent");     /**     * The amount to indent the right side     * of the paragraph.     * Type is float and specifies the size in points.     */    public static final Object RightIndent = new ParagraphConstants("RightIndent");     /**     * The amount of space between lines     * of the paragraph.     * Type is float and specifies the size as a factor of the line height     */    public static final Object LineSpacing = new ParagraphConstants("LineSpacing");     /**     * The amount of space above the paragraph.     * Type is float and specifies the size in points.     */    public static final Object SpaceAbove = new ParagraphConstants("SpaceAbove");     /**     * The amount of space below the paragraph.     * Type is float and specifies the size in points.     */    public static final Object SpaceBelow = new ParagraphConstants("SpaceBelow");     /**     * Alignment for the paragraph.  The type is     * Integer.  Valid values are:     * <ul>     * <li>ALIGN_LEFT     * <li>ALIGN_RIGHT     * <li>ALIGN_CENTER     * <li>ALIGN_JUSTIFED     * </ul>     *     */    public static final Object Alignment = new ParagraphConstants("Alignment");     /**     * TabSet for the paragraph, type is a TabSet containing     * TabStops.     */    public static final Object TabSet = new ParagraphConstants("TabSet");     /**     * Orientation for a paragraph.     */    public static final Object Orientation = new ParagraphConstants("Orientation");    /**     * A possible value for paragraph alignment.  This     * specifies that the text is aligned to the left     * indent and extra whitespace should be placed on     * the right.     */    public static final int ALIGN_LEFT = 0;     /**     * A possible value for paragraph alignment.  This     * specifies that the text is aligned to the center     * and extra whitespace should be placed equally on     * the left and right.     */    public static final int ALIGN_CENTER = 1;     /**     * A possible value for paragraph alignment.  This     * specifies that the text is aligned to the right     * indent and extra whitespace should be placed on     * the left.     */    public static final int ALIGN_RIGHT = 2;     /**     * A possible value for paragraph alignment.  This     * specifies that extra whitespace should be spread     * out through the rows of the paragraph with the     * text lined up with the left and right indent     * except on the last line which should be aligned     * to the left.     */    public static final int ALIGN_JUSTIFIED = 3;     // --- character attribute accessors ---------------------------     /**     * Gets the BidiLevel setting.     *     * @param a the attribute set     * @return the value     */    public static int getBidiLevel(AttributeSet a) {        Integer o = (Integer) a.getAttribute(BidiLevel);        if (o != null) {            return o.intValue();        }        return 0;  // Level 0 is base level (non-embedded) left-to-right    }     /**     * Sets the BidiLevel.     *     * @param a the attribute set     * @param o the bidi level value     */    public static void setBidiLevel(MutableAttributeSet a, int o) {        a.addAttribute(BidiLevel, Integer.valueOf(o));    }     /**     * Gets the component setting from the attribute list.     *     * @param a the attribute set     * @return the component, null if none     */    public static Component getComponent(AttributeSet a) {        return (Component) a.getAttribute(ComponentAttribute);    }     /**     * Sets the component attribute.     *     * @param a the attribute set     * @param c the component     */    public static void setComponent(MutableAttributeSet a, Component c) {        a.addAttribute(AbstractDocument.ElementNameAttribute, ComponentElementName);        a.addAttribute(ComponentAttribute, c);    }     /**     * Gets the icon setting from the attribute list.     *     * @param a the attribute set     * @return the icon, null if none     */    public static Icon getIcon(AttributeSet a) {        return (Icon) a.getAttribute(IconAttribute);    }     /**     * Sets the icon attribute.     *     * @param a the attribute set     * @param c the icon     */    public static void setIcon(MutableAttributeSet a, Icon c) {        a.addAttribute(AbstractDocument.ElementNameAttribute, IconElementName);        a.addAttribute(IconAttribute, c);    }     /**     * Gets the font family setting from the attribute list.     *     * @param a the attribute set     * @return the font family, "Monospaced" as the default     */    public static String getFontFamily(AttributeSet a) {        String family = (String) a.getAttribute(FontFamily);        if (family == null) {            family = "Monospaced";        }        return family;    }     /**     * Sets the font attribute.     *     * @param a the attribute set     * @param fam the font     */    public static void setFontFamily(MutableAttributeSet a, String fam) {        a.addAttribute(FontFamily, fam);    }     /**     * Gets the font size setting from the attribute list.     *     * @param a the attribute set     * @return the font size, 12 as the default     */    public static int getFontSize(AttributeSet a) {        Integer size = (Integer) a.getAttribute(FontSize);        if (size != null) {            return size.intValue();        }        return 12;    }     /**     * Sets the font size attribute.     *     * @param a the attribute set     * @param s the font size     */    public static void setFontSize(MutableAttributeSet a, int s) {        a.addAttribute(FontSize, Integer.valueOf(s));    }     /**     * Checks whether the bold attribute is set.     *     * @param a the attribute set     * @return true if set else false     */    public static boolean isBold(AttributeSet a) {        Boolean bold = (Boolean) a.getAttribute(Bold);        if (bold != null) {            return bold.booleanValue();        }        return false;    }     /**     * Sets the bold attribute.     *     * @param a the attribute set     * @param b specifies true/false for setting the attribute     */    public static void setBold(MutableAttributeSet a, boolean b) {        a.addAttribute(Bold, Boolean.valueOf(b));    }     /**     * Checks whether the italic attribute is set.     *     * @param a the attribute set     * @return true if set else false     */    public static boolean isItalic(AttributeSet a) {        Boolean italic = (Boolean) a.getAttribute(Italic);        if (italic != null) {            return italic.booleanValue();        }        return false;    }     /**     * Sets the italic attribute.     *     * @param a the attribute set     * @param b specifies true/false for setting the attribute     */    public static void setItalic(MutableAttributeSet a, boolean b) {        a.addAttribute(Italic, Boolean.valueOf(b));    }     /**     * Checks whether the underline attribute is set.     *     * @param a the attribute set     * @return true if set else false     */    public static boolean isUnderline(AttributeSet a) {        Boolean underline = (Boolean) a.getAttribute(Underline);        if (underline != null) {            return underline.booleanValue();        }        return false;    }     /**     * Checks whether the strikethrough attribute is set.     *     * @param a the attribute set     * @return true if set else false     */    public static boolean isStrikeThrough(AttributeSet a) {        Boolean strike = (Boolean) a.getAttribute(StrikeThrough);        if (strike != null) {            return strike.booleanValue();        }        return false;    }      /**     * Checks whether the superscript attribute is set.     *     * @param a the attribute set     * @return true if set else false     */    public static boolean isSuperscript(AttributeSet a) {        Boolean superscript = (Boolean) a.getAttribute(Superscript);        if (superscript != null) {            return superscript.booleanValue();        }        return false;    }      /**     * Checks whether the subscript attribute is set.     *     * @param a the attribute set     * @return true if set else false     */    public static boolean isSubscript(AttributeSet a) {        Boolean subscript = (Boolean) a.getAttribute(Subscript);        if (subscript != null) {            return subscript.booleanValue();        }        return false;    }      /**     * Sets the underline attribute.

Lines 1–500 of 852.