toString method is required
to return a string representation of CSS value.
The primary entry point for HTML View implementations
to get their attributes is the
getViewAttributes
method. This should be implemented to establish the
desired policy used to associate attributes with the view.
Each HTMLEditorKit (i.e. and therefore each associated
JEditorPane) can have its own StyleSheet, but by default one
sheet will be shared by all of the HTMLEditorKit instances.
HTMLDocument instance can also have a StyleSheet, which
holds the document-specific CSS specifications.
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. This allows implementations to determine what they want to cache and have the sharing potentially at the level that a selector is common to multiple views. Since the StyleSheet may be used by views over multiple documents and typically the HTML attributes don't effect the selector being used, the potential for sharing is significant.
The rules are stored as named styles, and other information is stored to translate the context of an element to a rule quickly. The following code fragment will display the named styles, and therefore the CSS rules contained.
import java.util.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
public class ShowStyles {
public static void main(String[] args) {
HTMLEditorKit kit = new HTMLEditorKit();
HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
StyleSheet styles = doc.getStyleSheet();
Enumeration rules = styles.getStyleNames();
while (rules.hasMoreElements()) {
String name = (String) rules.nextElement();
Style rule = styles.getStyle(name);
System.out.println(rule.toString());
}
System.exit(0);
}
}
The semantics for when a CSS style should override visual attributes
defined by an element are not well defined. For example, the html
<body bgcolor=red> makes the body have a red
background. But if the html file also contains the CSS rule
body { background: blue } it becomes less clear as to
what color the background of the body should be. The current
implementation gives visual attributes defined in the element the
highest precedence, that is they are always checked before any styles.
Therefore, in the previous example the background would have a
red color as the body element defines the background color to be red.
As already mentioned this supports CSS. We don't support the full CSS
spec. Refer to the javadoc of the CSS class to see what properties
we support. The two major CSS parsing related
concepts we do not currently
support are pseudo selectors, such as A:link { color: red },
and the important modifier.
Implementation Note
This implementation is currently incomplete. It can be replaced with alternative implementations that are complete. Future versions of this class will provide better CSS support.
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static class | StyleSheet.
Paints the background image. |
| public static class | StyleSheet.
Class to carry out some of the duties of CSS formatting. |
| pack-priv class | StyleSheet.
Default parser for CSS specifications that get loaded into the StyleSheet. |
| pack-priv class | StyleSheet.
Large set of attributes that does conversion of requests for attributes of type StyleConstants. |
| public static class | StyleSheet.
Class to carry out some of the duties of CSS list formatting. |
| pack-priv static class | StyleSheet.
A subclass of MuxingAttributeSet that implements Style. |
| private static class | StyleSheet.
A temporary class used to hold a Vector, a StringBuffer and a Hashtable. |
| pack-priv static class | StyleSheet.
SelectorMapping contains a specifitiy, as an integer, and an associated Style. |
| pack-priv class | StyleSheet.
Small set of attributes that does conversion of requests for attributes of type StyleConstants. |
| pack-priv class | StyleSheet.
A subclass of MuxingAttributeSet that translates between CSS and HTML and StyleConstants. |
| Modifier and Type | Field and Description |
|---|---|
| private URL | base
Where the style sheet was found. |
| private CSS | |
| private transient Object | |
| private Vector | linkedStyleSheets
Vector of StyleSheets that the rules are to reference. |
| private Hashtable | resolvedStyles
Maps from selector (as a string) to Style that includes all relevant styles. |
| private StyleSheet. | selectorMapping
An inverted graph of the selectors. |
| private int[] | |
| pack-priv static final int[] | sizeMapDefault
The HTML/CSS size model has seven slots that one can assign sizes to. |
| private boolean |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| private String | _cleanSelectorString(String selector)
Returns a new String that contains only one space between non white space characters. |
| public AttributeSet | Returns: the updated attribute setthe old attribute set old, Object the non-null attribute key key, Object the attribute value value)Overrides javax. Implements javax. Adds an attribute to the given set, and returns the new representative set. |
| public AttributeSet | Returns: the updated attribute setthe old attribute set old, AttributeSet the attributes to add attr)Overrides javax. Implements javax. Adds a set of attributes to the element. |
| public void | addCSSAttribute(MutableAttributeSet
a set of attributes attr, CSS.a CSS property key,an HTML attribute value value)Adds a CSS attribute to the given set. |
| public boolean | Returns: true if an HTML attribute value can be converted
to a CSS attribute, false otherwise.a set of attributes attr, CSS.a CSS property key,an HTML attribute value value)Adds a CSS attribute to the given set. |
| public void | |
| pack-priv void | addRule(String[]
the selector to use for the rule.
This will be a set of simple selectors, and must
be a length of 1 or greater. selector, AttributeSet the set of CSS attributes that
make up the rule. declaration, boolean isLinked)Adds a rule into the StyleSheet. |
| private void | addSortedStyle(StyleSheet.
Adds |
| public void | addStyleSheet(StyleSheet
a StyleSheet ss)Adds the rules from the StyleSheet |
| pack-priv String | cleanSelectorString(String selector)
Returns a string that only has one space between simple selectors, which may be the passed in String. |
| pack-priv AttributeSet | convertAttributeSet(AttributeSet a)
Converts a set of attributes (if necessary) so that any attributes that were specified as StyleConstants attributes and have a CSS mapping, will be converted to CSS attributes. |
| protected MutableAttributeSet | createLargeAttributeSet(AttributeSet
The set of attributes to be represented in
the larger form. a)Overrides javax. Creates a large set of attributes that should trade off space for time. |
| private synchronized Style | createResolvedStyle(String selector, String[] tags, String[] ids, String[] classes)
Creates and returns a Style containing all the rules that match
|
| private Style | createResolvedStyle(String selector, Vector<Element>
a Vector of all the Elements
the style is being asked for. The
first Element is the deepest Element, with the last Element
representing the root. elements, HTML.the Tag to use for
the first Element in telementsCreates and returns a Style containing all the rules that
matches |
| private Style | createResolvedStyle(String selector)
Creates and returns a Style containing all the rules that match
|
| protected StyleContext. | createSmallAttributeSet(AttributeSet
The set of attributes to be represented in
the compact form. a)Overrides javax. Creates a compact set of attributes that might be shared. |
| private Object | |
| public Color | Returns: the colorthe set of attributes a)Overrides javax. Takes a set of attributes and turn it into a background color specification. |
| pack-priv ImageIcon | |
| public URL | |
| public StyleSheet. | Returns: the box formatter.a set of CSS attributes a)Fetches the box formatter to use for the given set of CSS attributes. |
| public AttributeSet | Returns: a set of attributes that represents the CSS declaration.a CSS declaration decl)Translates a CSS declaration to an AttributeSet that represents the CSS declaration. |
| public Font | getFont(AttributeSet a)
Overrides javax. Fetches the font to use for the given set of attributes. |
| public Color | Returns: the colorthe set of attributes a)Overrides javax. Takes a set of attributes and turn it into a foreground color specification. |
| public static int | Returns: the index of HTML/CSS size model.a size of point pt)Returns the index of HTML/CSS size model. |
| private Style | |
| public StyleSheet. | Returns: the list formatter.a set of CSS attributes a)Fetches the list formatter to use for the given set of CSS attributes. |
| public float | Returns: the point size value.a size index index)Returns the point size, given a size index. |
| public float | Returns: the point size value.a CSS string describing font size size)Given a string such as "+2", "-2", or "2", returns a point size value. |
| private synchronized Style | |
| private synchronized Style | |
| private StyleSheet. | |
| public Style | Returns: the set of CSS attributes to use to render the tagthe type to translate to visual attributes t,the element representing the tag; the element
can be used to determine the nesting for situations where
the attributes will differ if nested inside of other
elements e)Fetches the style to use to render the given type of HTML tag. |
| public Style | |
| pack-priv String[] | |
| pack-priv int[] | |
| pack-priv static int | |
| private synchronized void | getStyles(StyleSheet.
Adds |
| public StyleSheet[] | |
| public AttributeSet | Returns: the of attributesa view v)Fetches a set of attributes to use in the view for displaying. |
| public void | |
| pack-priv boolean | |
| private synchronized void | linkStyleSheetAt(StyleSheet ss, int index)
Updates the attributes of the rules to reference any related
rules in |
| public void | loadRules(Reader
the stream to read the CSS grammar from in, URL the reference URL. This value represents the
location of the stream and may be null. All relative
URLs specified in the stream will be based upon this
parameter. ref)Loads a set of rules that have been specified in terms of CSS1 grammar. |
| pack-priv void | |
| private synchronized void | refreshResolvedRules(String selectorName, String[] selector, Style newStyle, int specificity)
Should be invoked when a new rule is added that did not previously exist. |
| public AttributeSet | Returns: the updated attribute setthe old set of attributes old, Object the non-null attribute name key)Overrides javax. Implements javax. Removes an attribute from the set. |
| public AttributeSet | Returns: the updated attribute setthe old attribute set old, Enumeration<?> the attribute names names)Overrides javax. Implements javax. Removes a set of attributes for the element. |
| public AttributeSet | Returns: the updated attribute setthe old attribute set old, AttributeSet the attributes attrs)Overrides javax. Implements javax. Removes a set of attributes. |
| private AttributeSet | removeHTMLTags(AttributeSet old, AttributeSet attr)
For any StyleConstants key in attr that has an associated HTML.Tag, it is removed from old. |
| public void | removeStyle(String
the name of the style to remove nm)Overrides javax. Removes a named style previously added to the document. |
| public void | |
| public void | |
| public void | |
| public void | |
| public Color | Returns: the colorcolor string such as "RED" or "#NNNNNN" string)Converts a color string such as "RED" or "#NNNNNN" to a Color. |
| public AttributeSet | Returns: the set of CSS attributes.AttributeSet containing the HTML attributes. htmlAttrSet)Converts a set of HTML attributes to an equivalent set of CSS attributes. |
| private synchronized void |