|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object javax.swing.text.StyleContext javax.swing.text.html.StyleSheet
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 the
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 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 rule
body { 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 | |
(package private) static class |
StyleSheet.BackgroundImagePainter
Paints the background image. |
static class |
StyleSheet.BoxPainter
Class to carry out some of the duties of CSS formatting. |
(package private) class |
StyleSheet.CssParser
Default parser for CSS specifications that get loaded into the StyleSheet. |
(package private) class |
StyleSheet.LargeConversionSet
Large set of attributes that does conversion of requests for attributes of type StyleConstants. |
static class |
StyleSheet.ListPainter
Class to carry out some of the duties of CSS list formatting. |
(package private) static class |
StyleSheet.ResolvedStyle
A subclass of MuxingAttributeSet that implements Style. |
private static class |
StyleSheet.SearchBuffer
A temporary class used to hold a Vector, a StringBuffer and a Hashtable. |
(package private) static class |
StyleSheet.SelectorMapping
SelectorMapping contains a specifitiy, as an integer, and an associated Style. |
(package private) class |
StyleSheet.SmallConversionSet
Small set of attributes that does conversion of requests for attributes of type StyleConstants. |
(package private) class |
StyleSheet.ViewAttributeSet
A subclass of MuxingAttributeSet that translates between CSS and HTML and StyleConstants. |
Nested classes inherited from class javax.swing.text.StyleContext |
StyleContext.NamedStyle, StyleContext.SmallAttributeSet |
Field Summary | |
private URL |
base
Where the style sheet was found. |
private CSS |
css
|
(package private) static int |
DEFAULT_FONT_SIZE
|
private Vector |
linkedStyleSheets
Vector of StyleSheets that the rules are to reference. |
(package private) static Border |
noBorder
|
private Hashtable |
resolvedStyles
Maps from selector (as a string) to Style that includes all relevant styles. |
private StyleSheet.SelectorMapping |
selectorMapping
An inverted graph of the selectors. |
Fields inherited from class javax.swing.text.StyleContext |
DEFAULT_STYLE |
Constructor Summary | |
StyleSheet()
Construct a StyleSheet |
Method Summary | |
private String |
_cleanSelectorString(String selector)
Returns a new String that contains only one space between non white space characters. |
AttributeSet |
addAttribute(AttributeSet old,
Object key,
Object value)
Adds an attribute to the given set, and returns the new representative 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. |
(package private) void |
addRule(String[] selector,
AttributeSet declaration,
boolean isLinked)
Adds a rule into the StyleSheet. |
private void |
addSortedStyle(StyleSheet.SelectorMapping mapping,
Vector elements)
Adds mapping to elements . |
void |
addStyleSheet(StyleSheet ss)
Adds the rules from the StyleSheet ss to those of
the receiver. |
(package private) String |
cleanSelectorString(String selector)
Returns a string that only has one space between simple selectors, which may be the passed in String. |
(package private) 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 a)
Creates a large set of attributes that should trade off space for time. |
private Style |
createResolvedStyle(String selector)
Creates and returns a Style containing all the rules that match selector . |
private Style |
createResolvedStyle(String selector,
String[] tags,
String[] ids,
String[] classes)
Creates and returns a Style containing all the rules that match selector . |
private Style |
createResolvedStyle(String selector,
Vector elements,
HTML.Tag t)
Creates and returns a Style containing all the rules that matches selector . |
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. |
(package private) ImageIcon |
getBackgroundImage(AttributeSet attr)
Returns the ImageIcon to draw in the background for attr . |
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)
|
private Style |
getLinkedStyle(Style localStyle)
Returns the style that linked attributes should be added to. |
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. |
private Style |
getResolvedStyle(String selector)
Returns the resolved style for selector . |
private Style |
getResolvedStyle(String selector,
Vector elements,
HTML.Tag t)
Returns the resolved style for selector . |
private StyleSheet.SelectorMapping |
getRootSelectorMapping()
Returns the root selector mapping that all selectors are relative to. |
Style |
getRule(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. |
(package private) String[] |
getSimpleSelectors(String selector)
Returns the simple selectors that comprise selector. |
(package private) static int |
getSpecificity(String selector)
Returns the specificity of the passed in String. |
private void |
getStyles(StyleSheet.SelectorMapping parentMapping,
Vector styles,
String[] tags,
String[] ids,
String[] classes,
int index,
int numElements,
Hashtable alreadyChecked)
Adds parentMapping to styles , and
recursively calls this method if parentMapping has
any child mappings for any of the Elements in elements . |
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 . |
private void |
linkStyleSheetAt(StyleSheet ss,
int index)
Updates the attributes of the rules to reference any related rules in ss . |
void |
loadRules(Reader in,
URL ref)
Loads a set of rules that have been specified in terms of CSS1 grammar. |
private void |
refreshResolvedRules(String selectorName,
String[] selector,
Style newStyle,
int specificity)
Should be invoked when a new rule is added that did not previously exist. |
AttributeSet |
removeAttribute(AttributeSet old,
Object key)
Removes an attribute from the set. |
AttributeSet |
removeAttributes(AttributeSet old,
AttributeSet attrs)
Removes a set of attributes. |
AttributeSet |
removeAttributes(AttributeSet old,
Enumeration names)
Removes a set of attributes for the element. |
private AttributeSet |
removeHTMLTags(AttributeSet old,
AttributeSet attr)
For any StyleConstants key in attr that has an associated HTML.Tag, it is removed from old. |
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. |
private void |
unlinkStyleSheet(StyleSheet ss,
int index)
Removes references to the rules in ss .
|
Methods inherited from class javax.swing.text.StyleContext |
addChangeListener, addStyle, getChangeListeners, getCompressionThreshold, getDefaultStyleContext, getEmptySet, getFont, getFontMetrics, getStaticAttribute, getStaticAttributeKey, getStyle, getStyleNames, readAttributes, readAttributeSet, reclaim, registerStaticAttributeKey, removeChangeListener, toString, writeAttributes, writeAttributeSet |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
static final Border noBorder
static final int DEFAULT_FONT_SIZE
private CSS css
private StyleSheet.SelectorMapping selectorMapping
private Hashtable resolvedStyles
private Vector linkedStyleSheets
private URL base
Constructor Detail |
public StyleSheet()
Method Detail |
public Style getRule(HTML.Tag t, Element e)
t
- the type to translate to visual attributese
- 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
public Style getRule(String selector)
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".
public void addRule(String rule)
public AttributeSet getDeclaration(String decl)
public void loadRules(Reader in, URL ref) throws IOException
in
- the stream to read the CSS grammar fromref
- 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.
IOException
public AttributeSet getViewAttributes(View v)
public void removeStyle(String nm)
removeStyle
in class StyleContext
nm
- the name of the style to removepublic void addStyleSheet(StyleSheet ss)
ss
to those of
the receiver. ss's
rules will override the rules of
any previously added style sheets. An added StyleSheet will never
override the rules of the receiving style sheet.
public void removeStyleSheet(StyleSheet ss)
ss
from those of the receiver.
public StyleSheet[] getStyleSheets()
public void importStyleSheet(URL url)
url
. The resulting rules
are directly added to the receiver. If you do not want the rules
to become part of the receiver, create a new StyleSheet and use
addStyleSheet to link it in.
public void setBase(URL base)
base
.
public URL getBase()
public void addCSSAttribute(MutableAttributeSet attr, CSS.Attribute key, String value)
public boolean addCSSAttributeFromHTML(MutableAttributeSet attr, CSS.Attribute key, String value)
public AttributeSet translateHTMLToCSS(AttributeSet htmlAttrSet)
htmlAttrSet
- AttributeSet containing the HTML attributes.public AttributeSet addAttribute(AttributeSet old, Object key, Object value)
addAttribute
in interface AbstractDocument.AttributeContext
addAttribute
in class StyleContext
old
- the old attribute setkey
- the non-null attribute keyvalue
- the attribute value
MutableAttributeSet.addAttribute(java.lang.Object, java.lang.Object)
public AttributeSet addAttributes(AttributeSet old, AttributeSet attr)
addAttributes
in interface AbstractDocument.AttributeContext
addAttributes
in class StyleContext
old
- the old attribute setattr
- the attributes to add
MutableAttributeSet.addAttribute(java.lang.Object, java.lang.Object)
public AttributeSet removeAttribute(AttributeSet old, Object key)
removeAttribute
in interface AbstractDocument.AttributeContext
removeAttribute
in class StyleContext
old
- the old set of attributeskey
- the non-null attribute name
MutableAttributeSet.removeAttribute(java.lang.Object)
public AttributeSet removeAttributes(AttributeSet old, Enumeration names)
removeAttributes
in interface AbstractDocument.AttributeContext
removeAttributes
in class StyleContext
old
- the old attribute setnames
- the attribute names
MutableAttributeSet.removeAttributes(java.util.Enumeration)
public AttributeSet removeAttributes(AttributeSet old, AttributeSet attrs)
removeAttributes
in interface AbstractDocument.AttributeContext
removeAttributes
in class StyleContext
old
- the old attribute setattrs
- the attributes
MutableAttributeSet.removeAttributes(java.util.Enumeration)
protected StyleContext.SmallAttributeSet createSmallAttributeSet(AttributeSet a)
createSmallAttributeSet
in class StyleContext
a
- The set of attributes to be represented in the
the compact form.protected MutableAttributeSet createLargeAttributeSet(AttributeSet a)
createLargeAttributeSet
in class StyleContext
a
- The set of attributes to be represented in the
the larger form.private AttributeSet removeHTMLTags(AttributeSet old, AttributeSet attr)
AttributeSet convertAttributeSet(AttributeSet a)
public Font getFont(AttributeSet a)
getFont
in class StyleContext
a
- the attribute set
public Color getForeground(AttributeSet a)
getForeground
in class StyleContext
a
- the set of attributes
public Color getBackground(AttributeSet a)
getBackground
in class StyleContext
a
- the set of attributes
public StyleSheet.BoxPainter getBoxPainter(AttributeSet a)
public StyleSheet.ListPainter getListPainter(AttributeSet a)
public void setBaseFontSize(int sz)
public void setBaseFontSize(String size)
public static int getIndexOfSize(float pt)
public float getPointSize(int index)
public float getPointSize(String size)
public Color stringToColor(String string)
ImageIcon getBackgroundImage(AttributeSet attr)
attr
.
void addRule(String[] selector, AttributeSet declaration, boolean isLinked)
selector
- the selector to use for the rule.
This will be a set of simple selectors, and must
be a length of 1 or greater.declaration
- the set of CSS attributes that
make up the rule.private void linkStyleSheetAt(StyleSheet ss, int index)
ss
.
private void unlinkStyleSheet(StyleSheet ss, int index)
ss
.
index
gives the index the StyleSheet was at, that is
how many StyleSheets had been added before it.
String[] getSimpleSelectors(String selector)
String cleanSelectorString(String selector)
private String _cleanSelectorString(String selector)
private StyleSheet.SelectorMapping getRootSelectorMapping()
static int getSpecificity(String selector)
private Style getLinkedStyle(Style localStyle)
private Style getResolvedStyle(String selector, Vector elements, HTML.Tag t)
selector
. This will
create the resolved style, if necessary.
private Style getResolvedStyle(String selector)
selector
. This will
create the resolved style, if necessary.
private void addSortedStyle(StyleSheet.SelectorMapping mapping, Vector elements)
mapping
to elements
. It is added
such that elements
will remain ordered by
specificity.
private void getStyles(StyleSheet.SelectorMapping parentMapping, Vector styles, String[] tags, String[] ids, String[] classes, int index, int numElements, Hashtable alreadyChecked)
parentMapping
to styles
, and
recursively calls this method if parentMapping
has
any child mappings for any of the Elements in elements
.
private Style createResolvedStyle(String selector, String[] tags, String[] ids, String[] classes)
selector
.
private Style createResolvedStyle(String selector, Vector elements, HTML.Tag t)
selector
.
elements
- 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.t
- the Tag to use for
the first Element in elements
private Style createResolvedStyle(String selector)
selector
. It is assumed that each simple selector
in selector
is separated by a space.
private void refreshResolvedRules(String selectorName, String[] selector, Style newStyle, int specificity)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |