StyleSheet (Java 2 Platform SE 5.0) (original) (raw)
javax.swing.text.html
Class StyleSheet
java.lang.Object
javax.swing.text.StyleContext
javax.swing.text.html.StyleSheet
All Implemented Interfaces:
Serializable, AbstractDocument.AttributeContext
public class StyleSheet
extends StyleContext
Support for defining the visual characteristics of HTML views being rendered. The StyleSheet is used to translate the HTML model into visual characteristics. This enables views to be customized by a look-and-feel, multiple views over the same model can be rendered differently, etc. This can be thought of as a CSS rule repository. The key for CSS attributes is an object of type CSS.Attribute. The type of the value is up to the StyleSheet implementation, but thetoString 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 overide 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 rulebody { background: blue } it becomes less clear as to what color the background of the body should be. The current implemention 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.
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.
| Nested Class Summary | |
|---|---|
| static class | StyleSheet.BoxPainter Class to carry out some of the duties of CSS formatting. |
| static class | StyleSheet.ListPainter Class to carry out some of the duties of CSS list formatting. |
| 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() Construct a StyleSheet |
| Method Summary | |
|---|---|
| AttributeSet | [addAttribute](../../../../javax/swing/text/html/StyleSheet.html#addAttribute%28javax.swing.text.AttributeSet, java.lang.Object, java.lang.Object%29)(AttributeSet old,Object key,Object value) Adds an attribute to the given set, and returns the new representative set. |
| AttributeSet | [addAttributes](../../../../javax/swing/text/html/StyleSheet.html#addAttributes%28javax.swing.text.AttributeSet, javax.swing.text.AttributeSet%29)(AttributeSet old,AttributeSet attr) Adds a set of attributes to the element. |
| void | [addCSSAttribute](../../../../javax/swing/text/html/StyleSheet.html#addCSSAttribute%28javax.swing.text.MutableAttributeSet, javax.swing.text.html.CSS.Attribute, java.lang.String%29)(MutableAttributeSet attr,CSS.Attribute key,String value) Adds a CSS attribute to the given set. |
| boolean | [addCSSAttributeFromHTML](../../../../javax/swing/text/html/StyleSheet.html#addCSSAttributeFromHTML%28javax.swing.text.MutableAttributeSet, javax.swing.text.html.CSS.Attribute, java.lang.String%29)(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 the StyleSheet ss to those of the receiver. |
| protected MutableAttributeSet | createLargeAttributeSet(AttributeSet a) Creates a large set of attributes that should trade off space for time. |
| 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 turn it into a background color specification. |
| URL | getBase() Returns the base. |
| StyleSheet.BoxPainter | getBoxPainter(AttributeSet a) Fetches the box formatter to use for the given set of CSS attributes. |
| AttributeSet | getDeclaration(String decl) Translates a CSS declaration to an AttributeSet that represents the CSS declaration. |
| Font | getFont(AttributeSet a) Fetches the font to use for the given set of attributes. |
| Color | getForeground(AttributeSet a) Takes a set of attributes and turn it into a foreground color specification. |
| static int | getIndexOfSize(float pt) |
| StyleSheet.ListPainter | getListPainter(AttributeSet a) Fetches the list formatter to use for the given set of CSS attributes. |
| float | getPointSize(int index) Returns the point size, given a size index. |
| float | getPointSize(String size) Given a string such as "+2", "-2", or "2", returns a point size value. |
| Style | [getRule](../../../../javax/swing/text/html/StyleSheet.html#getRule%28javax.swing.text.html.HTML.Tag, javax.swing.text.Element%29)(HTML.Tag t,Element e) Fetches the style to use to render the given type of HTML tag. |
| Style | getRule(String selector) Fetches the rule that best matches the selector given in string form. |
| StyleSheet[] | getStyleSheets() Returns an array of the linked StyleSheets. |
| AttributeSet | getViewAttributes(View v) Fetches a set of attributes to use in the view for displaying. |
| void | importStyleSheet(URL url) Imports a style sheet from url. |
| void | [loadRules](../../../../javax/swing/text/html/StyleSheet.html#loadRules%28java.io.Reader, java.net.URL%29)(Reader in,URL ref) Loads a set of rules that have been specified in terms of CSS1 grammar. |
| AttributeSet | [removeAttribute](../../../../javax/swing/text/html/StyleSheet.html#removeAttribute%28javax.swing.text.AttributeSet, java.lang.Object%29)(AttributeSet old,Object key) Removes an attribute from the set. |
| AttributeSet | [removeAttributes](../../../../javax/swing/text/html/StyleSheet.html#removeAttributes%28javax.swing.text.AttributeSet, javax.swing.text.AttributeSet%29)(AttributeSet old,AttributeSet attrs) Removes a set of attributes. |
| AttributeSet | [removeAttributes](../../../../javax/swing/text/html/StyleSheet.html#removeAttributes%28javax.swing.text.AttributeSet, java.util.Enumeration%29)(AttributeSet old,Enumeration<?> names) Removes a set of attributes for the element. |
| void | removeStyle(String nm) Removes a named style previously added to the document. |
| void | removeStyleSheet(StyleSheet ss) Removes the StyleSheet ss from those of the receiver. |
| void | setBase(URL base) Sets the base. |
| void | setBaseFontSize(int sz) Sets the base font size, with valid values between 1 and 7. |
| void | setBaseFontSize(String size) Sets the base font size from the passed in String. |
| Color | stringToColor(String string) Converts a color string such as "RED" or "#NNNNNN" to a 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 |
|---|
| addChangeListener, [addStyle](../../../../javax/swing/text/StyleContext.html#addStyle%28java.lang.String, javax.swing.text.Style%29), getChangeListeners, getCompressionThreshold, getDefaultStyleContext, getEmptySet, [getFont](../../../../javax/swing/text/StyleContext.html#getFont%28java.lang.String, int, int%29), getFontMetrics, getStaticAttribute, getStaticAttributeKey, getStyle, getStyleNames, [readAttributes](../../../../javax/swing/text/StyleContext.html#readAttributes%28java.io.ObjectInputStream, javax.swing.text.MutableAttributeSet%29), [readAttributeSet](../../../../javax/swing/text/StyleContext.html#readAttributeSet%28java.io.ObjectInputStream, javax.swing.text.MutableAttributeSet%29), reclaim, registerStaticAttributeKey, removeChangeListener, toString, [writeAttributes](../../../../javax/swing/text/StyleContext.html#writeAttributes%28java.io.ObjectOutputStream, javax.swing.text.AttributeSet%29), [writeAttributeSet](../../../../javax/swing/text/StyleContext.html#writeAttributeSet%28java.io.ObjectOutputStream, javax.swing.text.AttributeSet%29) |
| Methods inherited from class java.lang.Object |
|---|
| clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, [wait](../../../../java/lang/Object.html#wait%28long, int%29) |
| Constructor Detail |
|---|
StyleSheet
public StyleSheet()
Construct a StyleSheet
| Method Detail |
|---|
getRule
public Style getRule(HTML.Tag t, Element e)
Fetches the style to use to render the given type of HTML tag. The element given is representing the tag and can be used to determine the nesting for situations where the attributes will differ if nesting inside of elements.
Parameters:
t - the type to translate to visual attributes
e - 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
Returns:
the set of CSS attributes to use to render the tag
getRule
public Style getRule(String selector)
Fetches the rule that best matches the selector given in string form. Where selector is a space separated String of the element names. For example, selector might be 'html body tr td''
The attributes of the returned Style will change as rules are added and removed. That is if you to ask for a rule with a selector "table p" and a new rule was added with a selector of "p" the returned Style would include the new attributes from the rule "p".
addRule
public void addRule(String rule)
Adds a set of rules to the sheet. The rules are expected to be in valid CSS format. Typically this would be called as a result of parsing a