javax.swing.text.html

Class StyleSheet

Implemented Interfaces:
AbstractDocument.AttributeContext, Serializable

public class StyleSheet
extends StyleContext

This class adds support for defining the visual characteristics of HTML views being rendered. This enables views to be customized by a look-and-feel, mulitple views over the same model can be rendered differently. Each EditorPane has its own StyleSheet, but by default one sheet will be shared by all of the HTMLEditorKit instances. An HTMLDocument can also have a StyleSheet, which holds specific CSS specs. In order for Views to store less state and therefore be more lightweight, the StyleSheet can act as a factory for painters that handle some of the rendering tasks. Since the StyleSheet may be used by views over multiple documents the HTML attributes don't effect the selector being used. The rules are stored as named styles, and other information is stored to translate the context of an element to a rule.
See Also:
Serialized Form

Nested Class Summary

static class
StyleSheet.BoxPainter
This class carries out some of the duties of CSS formatting.
static class
StyleSheet.ListPainter
This class carries out some of the CSS list formatting duties.

Nested classes/interfaces inherited from class javax.swing.text.StyleContext

StyleContext.NamedStyle, StyleContext.SmallAttributeSet

Field Summary

Fields inherited from class javax.swing.text.StyleContext

DEFAULT_STYLE

Constructor Summary

StyleSheet()
Constructs a StyleSheet.

Method Summary

AttributeSet
addAttribute(AttributeSet old, Object key, Object value)
Adds an attribute to the given set and returns a new set.
AttributeSet
addAttributes(AttributeSet old, AttributeSet attr)
Adds a set of attributes to the element.
void
addCSSAttribute(MutableAttributeSet attr, CSS.Attribute key, String value)
Adds a CSS attribute to the given set.
boolean
addCSSAttributeFromHTML(MutableAttributeSet attr, CSS.Attribute key, String value)
Adds a CSS attribute to the given set.
void
addRule(String rule)
Adds a set of rules to the sheet.
void
addStyleSheet(StyleSheet ss)
Adds the rules from ss to those of the receiver. ss's rules will override the old rules.
protected MutableAttributeSet
createLargeAttributeSet(AttributeSet a)
Creates a large set of attributes.
protected StyleContext.SmallAttributeSet
createSmallAttributeSet(AttributeSet a)
Creates a compact set of attributes that might be shared.
Color
getBackground(AttributeSet a)
Takes a set of attributes and turns it into a background color specification.
URL
getBase()
Gets the base url.
StyleSheet.BoxPainter
getBoxPainter(AttributeSet a)
Gets the box formatter to use for the given set of CSS attributes.
AttributeSet
getDeclaration(String decl)
Translates a CSS declaration into an AttributeSet.
Font
getFont(AttributeSet a)
Gets the font to use for the given set.
Color
getForeground(AttributeSet a)
Takes a set of attributes and turns it into a foreground color specification.
static int
getIndexOfSize(float pt)
TODO
StyleSheet.ListPainter
getListPainter(AttributeSet a)
Gets the list formatter to use for the given set of CSS attributes.
float
getPointSize(int index)
Gets the point size, given a size index.
float
getPointSize(String size)
Given the string of the size, returns the point size value.
Style
getRule(String selector)
Gets the rule that best matches the selector. selector is a space separated String of element names.
Style
getRule(HTML.Tag t, Element e)
Gets the style used to render the given tag.
StyleSheet[]
getStyleSheets()
Returns an array of the linked StyleSheets.
AttributeSet
getViewAttributes(View v)
Gets a set of attributes to use in the view.
void
importStyleSheet(URL url)
Imports a style sheet from the url.
void
loadRules(Reader in, URL ref)
Loads a set of rules that have been specified in terms of CSS grammar.
AttributeSet
removeAttribute(AttributeSet old, Object key)
Removes an attribute from the set.
AttributeSet
removeAttributes(AttributeSet old, Enumeration names)
Removes a set of attributes for the element.
AttributeSet
removeAttributes(AttributeSet old, AttributeSet attrs)
Removes an attribute from the set.
void
removeStyle(String nm)
Removes a style previously added.
void
removeStyleSheet(StyleSheet ss)
Removes ss from those of the receiver
void
setBase(URL base)
Sets the base url.
void
setBaseFontSize(int sz)
Sets the base font size between 1 and 7.
void
setBaseFontSize(String size)
Sets the base font size from the String.
Color
stringToColor(String colorName)
Convert the color string represenation into java.awt.Color.
AttributeSet
translateHTMLToCSS(AttributeSet htmlAttrSet)
Converts a set of HTML attributes to an equivalent set of CSS attributes.

Methods inherited from class javax.swing.text.StyleContext

addAttribute, addAttributes, addChangeListener, addStyle, createLargeAttributeSet, createSmallAttributeSet, getBackground, getChangeListeners, getCompressionThreshold, getDefaultStyleContext, getEmptySet, getFont, getFont, getFontMetrics, getForeground, getStaticAttribute, getStaticAttributeKey, getStyle, getStyleNames, readAttributeSet, readAttributes, reclaim, registerStaticAttributeKey, removeAttribute, removeAttributes, removeAttributes, removeChangeListener, removeStyle, toString, writeAttributeSet, writeAttributes

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

StyleSheet

public StyleSheet()
Constructs a StyleSheet.

Method Details

addAttribute

public AttributeSet addAttribute(AttributeSet old,
                                 Object key,
                                 Object value)
Adds an attribute to the given set and returns a new set. This is implemented to convert StyleConstants attributes to CSS before forwarding them to the superclass. The StyleConstants attribute do not have corresponding CSS entry, the attribute is stored (but will likely not be used).
Specified by:
addAttribute in interface AbstractDocument.AttributeContext
Overrides:
addAttribute in interface StyleContext
Parameters:
old - - the old set
key - - the non-null attribute key
value - - the attribute value
Returns:
the updated set

addAttributes

public AttributeSet addAttributes(AttributeSet old,
                                  AttributeSet attr)
Adds a set of attributes to the element. If any of these attributes are StyleConstants, they will be converted to CSS before forwarding to the superclass.
Specified by:
addAttributes in interface AbstractDocument.AttributeContext
Overrides:
addAttributes in interface StyleContext
Parameters:
old - - the old set
attr - - the attributes to add
Returns:
the updated attribute set

addCSSAttribute

public void addCSSAttribute(MutableAttributeSet attr,
                            CSS.Attribute key,
                            String value)
Adds a CSS attribute to the given set.
Parameters:
attr - - the attribute set
key - - the attribute to add
value - - the value of the key

addCSSAttributeFromHTML

public boolean addCSSAttributeFromHTML(MutableAttributeSet attr,
                                       CSS.Attribute key,
                                       String value)
Adds a CSS attribute to the given set. This method parses the value argument from HTML based on key. Returns true if it finds a valid value for the given key, and false otherwise.
Parameters:
attr - - the attribute set
key - - the attribute to add
value - - the value of the key
Returns:
true if a valid value was found.

addRule

public void addRule(String rule)
Adds a set of rules to the sheet. The rules are expected to be in valid CSS format. This is called as a result of parsing a
Parameters:
rule - - the rule to add to the sheet

addStyleSheet

public void addStyleSheet(StyleSheet ss)
Adds the rules from ss to those of the receiver. ss's rules will override the old rules. An added StyleSheet will never override the rules of the receiving style sheet.
Parameters:
ss - - the new StyleSheet.

createLargeAttributeSet

protected MutableAttributeSet createLargeAttributeSet(AttributeSet a)
Creates a large set of attributes. This set is not shared. This is a hook for subclasses that want to change the behaviour of the larger attribute storage format.
Overrides:
createLargeAttributeSet in interface StyleContext
Parameters:
a - - the set of attributes to be represented in the larger form.
Returns:
the large set of attributes.

createSmallAttributeSet

protected StyleContext.SmallAttributeSet createSmallAttributeSet(AttributeSet a)
Creates a compact set of attributes that might be shared. This is a hook for subclasses that want to change the behaviour of SmallAttributeSet.
Overrides:
createSmallAttributeSet in interface StyleContext
Parameters:
a - - the set of attributes to be represented in the compact form.
Returns:
the set of attributes created

getBackground

public Color getBackground(AttributeSet a)
Takes a set of attributes and turns it into a background color specification. This is used to specify things like, brigher, more hue etc.
Overrides:
getBackground in interface StyleContext
Parameters:
a - - the set to get the background color for
Returns:
the background color for the set

getBase

public URL getBase()
Gets the base url.
Returns:
- the base

getBoxPainter

public StyleSheet.BoxPainter getBoxPainter(AttributeSet a)
Gets the box formatter to use for the given set of CSS attributes.
Parameters:
a - - the given set
Returns:
the box formatter

getDeclaration

public AttributeSet getDeclaration(String decl)
Translates a CSS declaration into an AttributeSet. This is called as a result of encountering an HTML style attribute.
Parameters:
decl - - the declaration to get
Returns:
the AttributeSet representing the declaration

getFont

public Font getFont(AttributeSet a)
Gets the font to use for the given set.
Overrides:
getFont in interface StyleContext
Parameters:
a - - the set to get the font for.
Returns:
the font for the set

getForeground

public Color getForeground(AttributeSet a)
Takes a set of attributes and turns it into a foreground color specification. This is used to specify things like, brigher, more hue etc.
Overrides:
getForeground in interface StyleContext
Parameters:
a - - the set to get the foreground color for
Returns:
the foreground color for the set

getIndexOfSize

public static int getIndexOfSize(float pt)
TODO
Parameters:
pt - - TODO
Returns:
TODO

getListPainter

public StyleSheet.ListPainter getListPainter(AttributeSet a)
Gets the list formatter to use for the given set of CSS attributes.
Parameters:
a - - the given set
Returns:
the list formatter

getPointSize

public float getPointSize(int index)
Gets the point size, given a size index.
Parameters:
index - - the size index
Returns:
the point size.

getPointSize

public float getPointSize(String size)
Given the string of the size, returns the point size value.
Parameters:
size - - the string representation of the size.
Returns:
- the point size value.

getRule

public Style getRule(String selector)
Gets the rule that best matches the selector. selector is a space separated String of element names. The attributes of the returned Style will change as rules are added and removed.
Parameters:
selector - - the element names separated by spaces
Returns:
the set of CSS attributes to use to render

getRule

public Style getRule(HTML.Tag t,
                     Element e)
Gets the style used to render the given tag. The element represents the tag and can be used to determine the nesting, where the attributes will differ if there is nesting inside of elements.
Parameters:
t - - the tag to translate to visual attributes
e - - the element representing the tag
Returns:
the set of CSS attributes to use to render the tag.

getStyleSheets

public StyleSheet[] getStyleSheets()
Returns an array of the linked StyleSheets. May return null.
Returns:
- An array of the linked StyleSheets.

getViewAttributes

public AttributeSet getViewAttributes(View v)
Gets a set of attributes to use in the view. This is a set of attributes that can be used for View.getAttributes
Parameters:
v - - the view to get the set for
Returns:
the AttributeSet to use in the view.

importStyleSheet

public void importStyleSheet(URL url)
Imports a style sheet from the url. The rules are directly added to the receiver. This is usually called when a
Parameters:
url - the URL to import the StyleSheet from

loadRules

public void loadRules(Reader in,
                      URL ref)
            throws IOException
Loads a set of rules that have been specified in terms of CSS grammar. If there are any conflicts with existing rules, the new rule is added.
Parameters:
in - - the stream to read the CSS grammar from.
ref - - the reference URL. It is the location of the stream, it may be null. All relative URLs specified in the stream will be based upon this parameter.
Throws:
IOException - - For any IO error while reading

removeAttribute

public AttributeSet removeAttribute(AttributeSet old,
                                    Object key)
Removes an attribute from the set. If the attribute is a StyleConstants, it will be converted to CSS before forwarding to the superclass.
Specified by:
removeAttribute in interface AbstractDocument.AttributeContext
Overrides:
removeAttribute in interface StyleContext
Parameters:
old - - the old set
key - - the non-null attribute key
Returns:
the updated set

removeAttributes

public AttributeSet removeAttributes(AttributeSet old,
                                     Enumeration names)
Removes a set of attributes for the element. If any of the attributes is a StyleConstants, they will be converted to CSS before forwarding to the superclass.
Specified by:
removeAttributes in interface AbstractDocument.AttributeContext
Overrides:
removeAttributes in interface StyleContext
Parameters:
old - - the old attribute set
names - - the attribute names
Returns:
the update attribute set

removeAttributes

public AttributeSet removeAttributes(AttributeSet old,
                                     AttributeSet attrs)
Removes an attribute from the set. If any of the attributes are StyleConstants, they will be converted to CSS before forwarding to the superclass.
Specified by:
removeAttributes in interface AbstractDocument.AttributeContext
Overrides:
removeAttributes in interface StyleContext
Parameters:
old - - the old set
attrs - - the attributes to remove
Returns:
the updated set

removeStyle

public void removeStyle(String nm)
Removes a style previously added.
Overrides:
removeStyle in interface StyleContext
Parameters:
nm - - the name of the style to remove

removeStyleSheet

public void removeStyleSheet(StyleSheet ss)
Removes ss from those of the receiver
Parameters:
ss - - the StyleSheet to remove.

setBase

public void setBase(URL base)
Sets the base url. All import statements that are relative, will be relative to base.
Parameters:
base - - the base URL.

setBaseFontSize

public void setBaseFontSize(int sz)
Sets the base font size between 1 and 7.
Parameters:
sz - - the new font size for the base.

setBaseFontSize

public void setBaseFontSize(String size)
Sets the base font size from the String. It can either identify a specific font size (between 1 and 7) or identify a relative font size such as +1 or -2.
Parameters:
size - - the new font size as a String.

stringToColor

public Color stringToColor(String colorName)
Convert the color string represenation into java.awt.Color. The valid values are like "aqua" , "#00FFFF" or "rgb(1,6,44)".
Parameters:
colorName - the color to convert.
Returns:
the matching java.awt.color

translateHTMLToCSS

public AttributeSet translateHTMLToCSS(AttributeSet htmlAttrSet)
Converts a set of HTML attributes to an equivalent set of CSS attributes.
Parameters:
htmlAttrSet - - the set containing the HTML attributes.
Returns:
the set of CSS attributes

StyleSheet.java -- Copyright (C) 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath 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 for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.