public class StyleSheet extends StyleContext
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 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.
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 |
|---|---|
static class |
StyleSheet
Class to carry out some of the duties of CSS formatting.
|
static class |
StyleSheet
Class to carry out some of the duties of CSS list formatting.
|
StyleContext.NamedStyle , StyleContext.SmallAttributeSet DEFAULT_STYLE| Constructor and Description |
|---|
StyleSheet()
Construct a StyleSheet
|
| Modifier and Type | Method and Description |
|---|---|
AttributeSet |
addAttribute(AttributeSet
Adds an attribute to the given set, and returns the new representative set.
|
AttributeSet |
addAttributes(AttributeSet
Adds a set of attributes to the element.
|
void |
addCSSAttribute(MutableAttributeSet
Adds a CSS attribute to the given set.
|
boolean |
addCSSAttributeFromHTML(MutableAttributeSet
Adds a CSS attribute to the given set.
|
void |
addRule(String
Adds a set of rules to the sheet.
|
void |
addStyleSheet(StyleSheet
Adds the rules from the StyleSheet
ss to those of the receiver.
|
protected MutableAttributeSet |
createLargeAttributeSet(AttributeSet
Creates a large set of attributes that should trade off space for time.
|
protected StyleContext |
createSmallAttributeSet(AttributeSet
Creates a compact set of attributes that might be shared.
|
Color |
getBackground(AttributeSet
Takes a set of attributes and turn it into a background color specification.
|
URL |
getBase()
Returns the base.
|
StyleSheet |
getBoxPainter(AttributeSet
Fetches the box formatter to use for the given set of CSS attributes.
|
AttributeSet |
getDeclaration(String
Translates a CSS declaration to an AttributeSet that represents the CSS declaration.
|
Font |
getFont(AttributeSet
Fetches the font to use for the given set of attributes.
|
Color |
getForeground(AttributeSet
Takes a set of attributes and turn it into a foreground color specification.
|
static int |
getIndexOfSize(float pt)
|
StyleSheet |
getListPainter(AttributeSet
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
Given a string such as "+2", "-2", or "2", returns a point size value.
|
Style |
getRule(HTML
Fetches the style to use to render the given type of HTML tag.
|
Style |
getRule(String
Fetches the rule that best matches the selector given in string form.
|
StyleSheet |
getStyleSheets()
Returns an array of the linked StyleSheets.
|
AttributeSet |
getViewAttributes(View
Fetches a set of attributes to use in the view for displaying.
|
void |
importStyleSheet(URL
Imports a style sheet from
url.
|
void |
loadRules(Reader
Loads a set of rules that have been specified in terms of CSS1 grammar.
|
AttributeSet |
removeAttribute(AttributeSet
Removes an attribute from the set.
|
AttributeSet |
removeAttributes(AttributeSet
Removes a set of attributes.
|
AttributeSet |
removeAttributes(AttributeSet
Removes a set of attributes for the element.
|
void |
removeStyle(String
Removes a named style previously added to the document.
|
void |
removeStyleSheet(StyleSheet
Removes the StyleSheet
ss from those of the receiver.
|
void |
setBase(URL
Sets the base.
|
void |
setBaseFontSize(int sz)
Sets the base font size, with valid values between 1 and 7.
|
void |
setBaseFontSize(String
Sets the base font size from the passed in String.
|
Color |
stringToColor(String
Converts a color string such as "RED" or "#NNNNNN" to a Color.
|
AttributeSet |
translateHTMLToCSS(AttributeSet
Converts a set of HTML attributes to an equivalent set of CSS attributes.
|
addChangeListener, addStyle, getChangeListeners, getCompressionThreshold, getDefaultStyleContext, getEmptySet, getFont, getFontMetrics, getStaticAttribute, getStaticAttributeKey, getStyle, getStyleNames, readAttributes, readAttributeSet, reclaim, registerStaticAttributeKey, removeChangeListener, toString, writeAttributes, writeAttributeSetpublic StylegetRule(HTML .Tag t, Element e)
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
public StylegetRule(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(Stringrule)
public AttributeSetgetDeclaration(String decl)
public void loadRules(Readerin, URL ref) throws IOException
in - the stream to read the CSS grammar from
ref - 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 AttributeSetgetViewAttributes(View v)
public void removeStyle(Stringnm)
removeStyle in class
StyleContext
nm - the name of the style to remove
public void addStyleSheet(StyleSheetss)
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(StyleSheetss)
ss from those of the receiver.
public StyleSheet[] getStyleSheets()
public void importStyleSheet(URLurl)
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(URLbase)
base.
public URLgetBase()
public void addCSSAttribute(MutableAttributeSetattr, CSS .Attribute key, String value)
public boolean addCSSAttributeFromHTML(MutableAttributeSetattr, CSS .Attribute key, String value)
public AttributeSettranslateHTMLToCSS(AttributeSet htmlAttrSet)
htmlAttrSet - AttributeSet containing the HTML attributes.
public AttributeSetaddAttribute(AttributeSet old, Object key, Object value)
addAttribute in interface
AbstractDocument.AttributeContext
addAttribute in class
StyleContext
old - the old attribute set
key - the non-null attribute key
value - the attribute value
MutableAttributeSet.addAttribute(java.lang.Object, java.lang.Object)
public AttributeSetaddAttributes(AttributeSet old, AttributeSet attr)
addAttributes in interface
AbstractDocument.AttributeContext
addAttributes in class
StyleContext
old - the old attribute set
attr - the attributes to add
MutableAttributeSet.addAttribute(java.lang.Object, java.lang.Object)
public AttributeSetremoveAttribute(AttributeSet old, Object key)
removeAttribute in interface
AbstractDocument.AttributeContext
removeAttribute in class
StyleContext
old - the old set of attributes
key - the non-null attribute name
MutableAttributeSet.removeAttribute(java.lang.Object)
public AttributeSetremoveAttributes(AttributeSet old, Enumeration <?> names)
removeAttributes in interface
AbstractDocument.AttributeContext
removeAttributes in class
StyleContext
old - the old attribute set
names - the attribute names
MutableAttributeSet.removeAttributes(java.util.Enumeration<?>)
public AttributeSetremoveAttributes(AttributeSet old, AttributeSet attrs)
removeAttributes in interface
AbstractDocument.AttributeContext
removeAttributes in class
StyleContext
old - the old attribute set
attrs - 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 MutableAttributeSetcreateLargeAttributeSet(AttributeSet a)
createLargeAttributeSet in class
StyleContext
a - The set of attributes to be represented in the the larger form.
public FontgetFont(AttributeSet a)
getFont in class
StyleContext
a - the attribute set
public ColorgetForeground(AttributeSet a)
getForeground in class
StyleContext
a - the set of attributes
public ColorgetBackground(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(Stringsize)
public static int getIndexOfSize(float pt)
public float getPointSize(int index)
public float getPointSize(Stringsize)