Overview | Package | Class | Use | Tree | Deprecated | Index | Help
| Swing 1.1
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## javax.swing.text.html
Class StyleSheet

    java.lang.Object
      |
      +--javax.swing.text.StyleContext
            |
            +--javax.swing.text.html.StyleSheet

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 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);
    }
    }

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.

See Also:
  Serialized Form

Inner 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.

Inner classes inherited from class javax.swing.text.StyleContext
`StyleContext.NamedStyle, StyleContext.SmallAttributeSet`

Fields inherited from class javax.swing.text.StyleContext
`DEFAULT_STYLE`

Constructor Summary
`StyleSheet()` Construct a StyleSheet

Method Summary
`void` | `addRule(java.lang.String rule)` Add a set of rules to the sheet.
`java.awt.Color` | `getBackground(AttributeSet a)` Takes a set of attributes and turn it into a background color specification.
`StyleSheet.BoxPainter` | `getBoxPainter(AttributeSet a)` Fetch the box formatter to use for the given set of css attributes.
`AttributeSet` | `getDeclaration(java.lang.String decl)` Translate a CSS declaration to an AttributeSet that represents the CSS declaration.
`java.awt.Font` | `getFont(AttributeSet a)` Fetch the font to use for the given set of attributes.
`java.awt.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)` Fetch the list formatter to use for the given set of css attributes.
`float` | `getPointSize(int index)` Return the point size, given a size index.
`float` | `getPointSize(java.lang.String size)` Given a string "+2", "-2", "2".
`Style` | `getRule(HTML.Tag t, Element e)` Fetch the style to use to render the given type of html tag.
`Style` | `getRule(java.lang.String selector)` Fetch the rule that best matches the selector given in string form.
`AttributeSet` | `getViewAttributes(View v)` Fetch a set of attributes to use in the view for displaying.
`void` | `loadRules(java.io.Reader in, java.net.URL ref)` Load a set of rules that have been specified in terms of CSS1 grammar.
`void` | `setBaseFontSize(int sz)`
`void` | `setBaseFontSize(java.lang.String size)`
`java.awt.Color` | `stringToColor(java.lang.String str)` Convert a color string "RED" or "#NNNNNN" to a Color.
`AttributeSet` | `translateHTMLToCSS(AttributeSet htmlAttrSet)` Convert a set of html attributes to an equivalent set of css attributes.

Methods inherited from class javax.swing.text.StyleContext
`addAttribute, addAttributes, addChangeListener, addStyle, createLargeAttributeSet, createSmallAttributeSet, getCompressionThreshold, getDefaultStyleContext, getEmptySet, getFont, getFontMetrics, getStaticAttribute, getStaticAttributeKey, getStyle, getStyleNames, readAttributes, readAttributeSet, reclaim, registerStaticAttributeKey, removeAttribute, removeAttributes, removeAttributes, removeChangeListener, removeStyle, toString, writeAttributes, writeAttributeSet`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

Constructor Detail

### StyleSheet

    public StyleSheet()

  Construct a StyleSheet

Method Detail

### getRule

    public Style getRule(HTML.Tag t,
                         Element e)

  Fetch 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.

### getRule

    public Style getRule(java.lang.String selector)

  Fetch the rule that best matches the selector given in string form.

### addRule

    public void addRule(java.lang.String rule)

  Add 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 <style> tag.

### getDeclaration

    public AttributeSet getDeclaration(java.lang.String decl)

  Translate a CSS declaration to an AttributeSet that represents the CSS declaration. Typically this would be called as a result of encountering an HTML style attribute.

### loadRules

    public void loadRules(java.io.Reader in,
                          java.net.URL ref)
                   throws java.io.IOException

  Load a set of rules that have been specified in terms of CSS1 grammar. If there are collisions with existing rules, the newly specified rule will win.
  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.

### getViewAttributes

    public AttributeSet getViewAttributes(View v)

  Fetch a set of attributes to use in the view for displaying. This is basically a set of attributes that can be used for View.getAttributes.

### getFont

    public java.awt.Font getFont(AttributeSet a)

  Fetch the font to use for the given set of attributes.
  Overrides:
    getFont in class StyleContext

### getForeground

    public java.awt.Color getForeground(AttributeSet a)

  Takes a set of attributes and turn it into a foreground color specification. This might be used to specify things like brighter, more hue, etc.
  Parameters:
    `a` - the set of attributes
  Returns:
    the color
  Overrides:
    getForeground in class StyleContext

### getBackground

    public java.awt.Color getBackground(AttributeSet a)

  Takes a set of attributes and turn it into a background color specification. This might be used to specify things like brighter, more hue, etc.
  Parameters:
    `attr` - the set of attributes
  Returns:
    the color
  Overrides:
    getBackground in class StyleContext

### getBoxPainter

    public StyleSheet.BoxPainter getBoxPainter(AttributeSet a)

  Fetch the box formatter to use for the given set of css attributes.

### getListPainter

    public StyleSheet.ListPainter getListPainter(AttributeSet a)

  Fetch the list formatter to use for the given set of css attributes.

### stringToColor

    public java.awt.Color stringToColor(java.lang.String str)

  Convert a color string "RED" or "#NNNNNN" to a Color. Note: This will only convert the HTML3.2 colors strings or string of length 7 otherwise, it will return null.

### setBaseFontSize

    public void setBaseFontSize(int sz)

### setBaseFontSize

    public void setBaseFontSize(java.lang.String size)

### getIndexOfSize

    public static int getIndexOfSize(float pt)

### getPointSize

    public float getPointSize(int index)

  Return the point size, given a size index.

### getPointSize

    public float getPointSize(java.lang.String size)

  Given a string "+2", "-2", "2". returns a point size value

### translateHTMLToCSS

    public AttributeSet translateHTMLToCSS(AttributeSet htmlAttrSet)

  Convert a set of html attributes to an equivalent set of css attributes.
  Parameters:
    `AttributeSet` - containing the HTML attributes.
    `AttributeSet` - containing the corresponding CSS attributes. The AttributeSet will be empty if there are no mapping CSS attributes.

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
| Swing 1.1
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Submit a bug or feature
Java is a trademark or registered trademark of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-1998 Sun Microsystems, Inc. 901 San Antonio Road,
Palo Alto, California, 94303, U.S.A. All Rights Reserved.