Class StyleSheet
- All Implemented Interfaces:
-
Serializable
,AbstractDocument.AttributeContext
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.
- 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.
Nested Class Summary
Modifier and Type | Class | Description |
---|---|---|
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 declared in class javax.swing.text.StyleContext
StyleContext.NamedStyle, StyleContext.SmallAttributeSet
Field Summary
Fields declared in class javax.swing.text.StyleContext
DEFAULT_STYLE
Constructor Summary
Constructor | Description |
---|---|
StyleSheet() |
Construct a StyleSheet |
Method Summary
Modifier and Type | Method | Description |
---|---|---|
AttributeSet |
addAttribute |
Adds an attribute to the given set, and returns the new representative set. |
AttributeSet |
addAttributes |
Adds a set of attributes to the element. |
void |
addCSSAttribute |
Adds a CSS attribute to the given set. |
boolean |
addCSSAttributeFromHTML |
Adds a CSS attribute to the given set. |
void |
addRule |
Adds a set of rules to the sheet. |
void |
addStyleSheet |
Adds the rules from the StyleSheet ss to those of the receiver. |
protected MutableAttributeSet |
createLargeAttributeSet |
Creates a large set of attributes that should trade off space for time. |
protected StyleContext.SmallAttributeSet |
createSmallAttributeSet |
Creates a compact set of attributes that might be shared. |
Color |
getBackground |
Takes a set of attributes and turn it into a background color specification. |
URL |
getBase() |
Returns the base. |
StyleSheet.BoxPainter |
getBoxPainter |
Fetches the box formatter to use for the given set of CSS attributes. |
AttributeSet |
getDeclaration |
Translates a CSS declaration to an AttributeSet that represents the CSS declaration. |
Font |
getFont |
Fetches the font to use for the given set of attributes. |
Color |
getForeground |
Takes a set of attributes and turn it into a foreground color specification. |
static int |
getIndexOfSize |
Returns the index of HTML/CSS size model. |
StyleSheet.ListPainter |
getListPainter |
Fetches the list formatter to use for the given set of CSS attributes. |
float |
getPointSize |
Returns the point size, given a size index. |
float |
getPointSize |
Given a string such as "+2", "-2", or "2", returns a point size value. |
Style |
getRule |
Fetches the rule that best matches the selector given in string form. |
Style |
getRule |
Fetches the style to use to render the given type of HTML tag. |
StyleSheet[] |
getStyleSheets() |
Returns an array of the linked StyleSheets. |
AttributeSet |
getViewAttributes |
Fetches a set of attributes to use in the view for displaying. |
void |
importStyleSheet |
Imports a style sheet from url . |
void |
loadRules |
Loads a set of rules that have been specified in terms of CSS1 grammar. |
AttributeSet |
removeAttribute |
Removes an attribute from the set. |
AttributeSet |
removeAttributes |
Removes a set of attributes for the element. |
AttributeSet |
removeAttributes |
Removes a set of attributes. |
void |
removeStyle |
Removes a named style previously added to the document. |
void |
removeStyleSheet |
Removes the StyleSheet ss from those of the receiver. |
void |
setBase |
Sets the base. |
void |
setBaseFontSize |
Sets the base font size, with valid values between 1 and 7. |
void |
setBaseFontSize |
Sets the base font size from the passed in String. |
Color |
stringToColor |
Converts a color string such as "RED" or "#NNNNNN" to a Color. |
AttributeSet |
translateHTMLToCSS |
Converts a set of HTML attributes to an equivalent set of CSS attributes. |
Methods declared in 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
Constructor Details
StyleSheet
public StyleSheet()
Method Details
getRule
public Style getRule(HTML.Tag t, Element e)
- 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)
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".
- Parameters:
-
selector
- a space separated String of the element names. - Returns:
- the rule that best matches the selector.
addRule
public void addRule(String rule)
- Parameters:
-
rule
- a set of rules
getDeclaration
public AttributeSet getDeclaration(String decl)
- Parameters:
-
decl
- a CSS declaration - Returns:
- a set of attributes that represents the CSS declaration.
loadRules
public void loadRules(Reader in, URL ref) throws IOException
- Parameters:
-
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. - Throws:
-
IOException
- if I/O error occured.
getViewAttributes
public AttributeSet getViewAttributes(View v)
- Parameters:
-
v
- a view - Returns:
- the of attributes
removeStyle
public void removeStyle(String nm)
- Overrides:
-
removeStyle
in classStyleContext
- Parameters:
-
nm
- the name of the style to remove
addStyleSheet
public 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.- Parameters:
-
ss
- a StyleSheet - Since:
- 1.3
removeStyleSheet
public void removeStyleSheet(StyleSheet ss)
ss
from those of the receiver.- Parameters:
-
ss
- a StyleSheet - Since:
- 1.3
getStyleSheets
public StyleSheet[] getStyleSheets()
- Returns:
- an array of StyleSheets.
- Since:
- 1.3
importStyleSheet
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.- Parameters:
-
url
- an url - Since:
- 1.3
setBase
public void setBase(URL base)
base
.- Parameters:
-
base
- a base. - Since:
- 1.3
getBase
public URL getBase()
- Returns:
- the base.
- Since:
- 1.3
addCSSAttribute
public void addCSSAttribute(MutableAttributeSet attr, CSS.Attribute key, String value)
- Parameters:
-
attr
- a set of attributes -
key
- a CSS property -
value
- an HTML attribute value - Since:
- 1.3
addCSSAttributeFromHTML
public boolean addCSSAttributeFromHTML(MutableAttributeSet attr, CSS.Attribute key, String value)
- Parameters:
-
attr
- a set of attributes -
key
- a CSS property -
value
- an HTML attribute value - Returns:
-
true
if an HTML attributevalue
can be converted to a CSS attribute, false otherwise. - Since:
- 1.3
translateHTMLToCSS
public AttributeSet translateHTMLToCSS(AttributeSet htmlAttrSet)
- Parameters:
-
htmlAttrSet
- AttributeSet containing the HTML attributes. - Returns:
- the set of CSS attributes.
addAttribute
public AttributeSet addAttribute(AttributeSet old, Object key, Object value)
- Specified by:
-
addAttribute
in interfaceAbstractDocument.AttributeContext
- Overrides:
-
addAttribute
in classStyleContext
- Parameters:
-
old
- the old attribute set -
key
- the non-null attribute key -
value
- the attribute value - Returns:
- the updated attribute set
- See Also:
addAttributes
public AttributeSet addAttributes(AttributeSet old, AttributeSet attr)
- Specified by:
-
addAttributes
in interfaceAbstractDocument.AttributeContext
- Overrides:
-
addAttributes
in classStyleContext
- Parameters:
-
old
- the old attribute set -
attr
- the attributes to add - Returns:
- the updated attribute set
- See Also:
removeAttribute
public AttributeSet removeAttribute(AttributeSet old, Object key)
- Specified by:
-
removeAttribute
in interfaceAbstractDocument.AttributeContext
- Overrides:
-
removeAttribute
in classStyleContext
- Parameters:
-
old
- the old set of attributes -
key
- the non-null attribute name - Returns:
- the updated attribute set
- See Also:
removeAttributes
public AttributeSet removeAttributes(AttributeSet old, Enumeration<?> names)
- Specified by:
-
removeAttributes
in interfaceAbstractDocument.AttributeContext
- Overrides:
-
removeAttributes
in classStyleContext
- Parameters:
-
old
- the old attribute set -
names
- the attribute names - Returns:
- the updated attribute set
- See Also:
removeAttributes
public AttributeSet removeAttributes(AttributeSet old, AttributeSet attrs)
- Specified by:
-
removeAttributes
in interfaceAbstractDocument.AttributeContext
- Overrides:
-
removeAttributes
in classStyleContext
- Parameters:
-
old
- the old attribute set -
attrs
- the attributes - Returns:
- the updated attribute set
- See Also:
createSmallAttributeSet
protected StyleContext.SmallAttributeSet createSmallAttributeSet(AttributeSet a)
- Overrides:
-
createSmallAttributeSet
in classStyleContext
- Parameters:
-
a
- The set of attributes to be represented in the the compact form. - Returns:
- a compact set of attributes that might be shared
createLargeAttributeSet
protected MutableAttributeSet createLargeAttributeSet(AttributeSet a)
- Overrides:
-
createLargeAttributeSet
in classStyleContext
- Parameters:
-
a
- The set of attributes to be represented in the the larger form. - Returns:
- a large set of attributes that should trade off space for time
getFont
public Font getFont(AttributeSet a)
- Overrides:
-
getFont
in classStyleContext
- Parameters:
-
a
- the attribute set - Returns:
- the font
getForeground
public Color getForeground(AttributeSet a)
- Overrides:
-
getForeground
in classStyleContext
- Parameters:
-
a
- the set of attributes - Returns:
- the color
getBackground
public Color getBackground(AttributeSet a)
- Overrides:
-
getBackground
in classStyleContext
- Parameters:
-
a
- the set of attributes - Returns:
- the color
getBoxPainter
public StyleSheet.BoxPainter getBoxPainter(AttributeSet a)
- Parameters:
-
a
- a set of CSS attributes - Returns:
- the box formatter.
getListPainter
public StyleSheet.ListPainter getListPainter(AttributeSet a)
- Parameters:
-
a
- a set of CSS attributes - Returns:
- the list formatter.
setBaseFontSize
public void setBaseFontSize(int sz)
- Parameters:
-
sz
- a font size.
setBaseFontSize
public void setBaseFontSize(String size)
- Parameters:
-
size
- a font size.
getIndexOfSize
public static int getIndexOfSize(float pt)
- Parameters:
-
pt
- a size of point - Returns:
- the index of HTML/CSS size model.
getPointSize
public float getPointSize(int index)
- Parameters:
-
index
- a size index - Returns:
- the point size value.
getPointSize
public float getPointSize(String size)
- Parameters:
-
size
- a CSS string describing font size - Returns:
- the point size value.
stringToColor
public Color stringToColor(String string)
- Parameters:
-
string
- color string such as "RED" or "#NNNNNN" - Returns:
- the color
© 1993, 2021, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/javax/swing/text/html/StyleSheet.html