Method from javax.swing.text.html.StyleSheet Detail: |
public AttributeSet addAttribute(AttributeSet old,
Object key,
Object value) {
if (css == null) {
// supers constructor will call this before returning,
// and we need to make sure CSS is non null.
css = new CSS();
}
if (key instanceof StyleConstants) {
HTML.Tag tag = HTML.getTagForStyleConstantsKey(
(StyleConstants)key);
if (tag != null && old.isDefined(tag)) {
old = removeAttribute(old, tag);
}
Object cssValue = css.styleConstantsValueToCSSValue
((StyleConstants)key, value);
if (cssValue != null) {
Object cssKey = css.styleConstantsKeyToCSSKey
((StyleConstants)key);
if (cssKey != null) {
return super.addAttribute(old, cssKey, cssValue);
}
}
}
return super.addAttribute(old, key, value);
}
Adds an attribute to the given set, and returns
the new representative set. This is reimplemented to
convert StyleConstant attributes to CSS prior to forwarding
to the superclass behavior. The StyleConstants attribute
has no corresponding CSS entry, the StyleConstants attribute
is stored (but will likely be unused). |
public AttributeSet addAttributes(AttributeSet old,
AttributeSet attr) {
if (!(attr instanceof HTMLDocument.TaggedAttributeSet)) {
old = removeHTMLTags(old, attr);
}
return super.addAttributes(old, convertAttributeSet(attr));
}
Adds a set of attributes to the element. If any of these attributes
are StyleConstants attributes, they will be converted to CSS prior
to forwarding to the superclass behavior. |
public void addCSSAttribute(MutableAttributeSet attr,
Attribute key,
String value) {
css.addInternalCSSValue(attr, key, value);
}
Adds a CSS attribute to the given set. |
public boolean addCSSAttributeFromHTML(MutableAttributeSet attr,
Attribute key,
String value) {
Object iValue = css.getCssValue(key, value);
if (iValue != null) {
attr.addAttribute(key, iValue);
return true;
}
return false;
}
Adds a CSS attribute to the given set. |
public void addRule(String rule) {
if (rule != null) {
//tweaks to control display properties
//see BasicEditorPaneUI
final String baseUnitsDisable = "BASE_SIZE_DISABLE";
final String baseUnits = "BASE_SIZE ";
final String w3cLengthUnitsEnable = "W3C_LENGTH_UNITS_ENABLE";
final String w3cLengthUnitsDisable = "W3C_LENGTH_UNITS_DISABLE";
if (rule == baseUnitsDisable) {
sizeMap = sizeMapDefault;
} else if (rule.startsWith(baseUnits)) {
rebaseSizeMap(Integer.
parseInt(rule.substring(baseUnits.length())));
} else if (rule == w3cLengthUnitsEnable) {
w3cLengthUnits = true;
} else if (rule == w3cLengthUnitsDisable) {
w3cLengthUnits = false;
} else {
CssParser parser = new CssParser();
try {
parser.parse(getBase(), new StringReader(rule), false, false);
} catch (IOException ioe) { }
}
}
}
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 <style> tag. |
void addRule(String[] selector,
AttributeSet declaration,
boolean isLinked) {
int n = selector.length;
StringBuilder sb = new StringBuilder();
sb.append(selector[0]);
for (int counter = 1; counter < n; counter++) {
sb.append(' ');
sb.append(selector[counter]);
}
String selectorName = sb.toString();
Style rule = getStyle(selectorName);
if (rule == null) {
// Notice how the rule is first created, and it not part of
// the synchronized block. It is done like this as creating
// a new rule will fire a ChangeEvent. We do not want to be
// holding the lock when calling to other objects, it can
// result in deadlock.
Style altRule = addStyle(selectorName, null);
synchronized(this) {
SelectorMapping mapping = getRootSelectorMapping();
for (int i = n - 1; i >= 0; i--) {
mapping = mapping.getChildSelectorMapping
(selector[i], true);
}
rule = mapping.getStyle();
if (rule == null) {
rule = altRule;
mapping.setStyle(rule);
refreshResolvedRules(selectorName, selector, rule,
mapping.getSpecificity());
}
}
}
if (isLinked) {
rule = getLinkedStyle(rule);
}
rule.addAttributes(declaration);
}
Adds a rule into the StyleSheet. |
public void addStyleSheet(StyleSheet ss) {
synchronized(this) {
if (linkedStyleSheets == null) {
linkedStyleSheets = new Vector< StyleSheet >();
}
if (!linkedStyleSheets.contains(ss)) {
int index = 0;
if (ss instanceof javax.swing.plaf.UIResource
&& linkedStyleSheets.size() > 1) {
index = linkedStyleSheets.size() - 1;
}
linkedStyleSheets.insertElementAt(ss, index);
linkStyleSheetAt(ss, index);
}
}
}
Adds the rules from the StyleSheet 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. |
String cleanSelectorString(String selector) {
boolean lastWasSpace = true;
for (int counter = 0, maxCounter = selector.length();
counter < maxCounter; counter++) {
switch(selector.charAt(counter)) {
case ' ':
if (lastWasSpace) {
return _cleanSelectorString(selector);
}
lastWasSpace = true;
break;
case '\n':
case '\r':
case '\t':
return _cleanSelectorString(selector);
default:
lastWasSpace = false;
}
}
if (lastWasSpace) {
return _cleanSelectorString(selector);
}
// It was fine.
return selector;
}
Returns a string that only has one space between simple selectors,
which may be the passed in String. |
AttributeSet convertAttributeSet(AttributeSet a) {
if ((a instanceof LargeConversionSet) ||
(a instanceof SmallConversionSet)) {
// known to be converted.
return a;
}
// in most cases, there are no StyleConstants attributes
// so we iterate the collection of keys to avoid creating
// a new set.
Enumeration names = a.getAttributeNames();
while (names.hasMoreElements()) {
Object name = names.nextElement();
if (name instanceof StyleConstants) {
// we really need to do a conversion, iterate again
// building a new set.
MutableAttributeSet converted = new LargeConversionSet();
Enumeration keys = a.getAttributeNames();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
Object cssValue = null;
if (key instanceof StyleConstants) {
// convert the StyleConstants attribute if possible
Object cssKey = css.styleConstantsKeyToCSSKey
((StyleConstants)key);
if (cssKey != null) {
Object value = a.getAttribute(key);
cssValue = css.styleConstantsValueToCSSValue
((StyleConstants)key, value);
if (cssValue != null) {
converted.addAttribute(cssKey, cssValue);
}
}
}
if (cssValue == null) {
converted.addAttribute(key, a.getAttribute(key));
}
}
return converted;
}
}
return 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) {
return new LargeConversionSet(a);
}
Creates a large set of attributes that should trade off
space for time. This set will not be shared. This is
a hook for subclasses that want to alter the behavior
of the larger attribute storage format (which is
SimpleAttributeSet by default). This can be reimplemented
to return a MutableAttributeSet that provides some sort of
attribute conversion. |
protected SmallAttributeSet createSmallAttributeSet(AttributeSet a) {
return new SmallConversionSet(a);
}
Creates a compact set of attributes that might be shared.
This is a hook for subclasses that want to alter the
behavior of SmallAttributeSet. This can be reimplemented
to return an AttributeSet that provides some sort of
attribute conversion. |
public Color getBackground(AttributeSet a) {
return css.getColor(a, CSS.Attribute.BACKGROUND_COLOR);
}
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. |
ImageIcon getBackgroundImage(AttributeSet attr) {
Object value = attr.getAttribute(CSS.Attribute.BACKGROUND_IMAGE);
if (value != null) {
return ((CSS.BackgroundImage)value).getImage(getBase());
}
return null;
}
Returns the ImageIcon to draw in the background for
attr . |
public URL getBase() {
return base;
}
|
public BoxPainter getBoxPainter(AttributeSet a) {
return new BoxPainter(a, css, this);
}
Fetches the box formatter to use for the given set
of CSS attributes. |
public AttributeSet getDeclaration(String decl) {
if (decl == null) {
return SimpleAttributeSet.EMPTY;
}
CssParser parser = new CssParser();
return parser.parseDeclaration(decl);
}
Translates 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. |
public Font getFont(AttributeSet a) {
return css.getFont(this, a, 12, this);
}
Fetches the font to use for the given set of attributes. |
public Color getForeground(AttributeSet a) {
Color c = css.getColor(a, CSS.Attribute.COLOR);
if (c == null) {
return Color.black;
}
return c;
}
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. |
public static int getIndexOfSize(float pt) {
return CSS.getIndexOfSize(pt, sizeMapDefault);
}
|
public ListPainter getListPainter(AttributeSet a) {
return new ListPainter(a, this);
}
Fetches the list formatter to use for the given set
of CSS attributes. |
public float getPointSize(int index) {
return css.getPointSize(index, this);
}
Returns the point size, given a size index. |
public float getPointSize(String size) {
return css.getPointSize(size, this);
}
Given a string such as "+2", "-2", or "2",
returns a point size value. |
public Style getRule(String selector) {
selector = cleanSelectorString(selector);
if (selector != null) {
Style style = getResolvedStyle(selector);
return style;
}
return null;
}
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". |
public Style getRule(Tag t,
Element e) {
SearchBuffer sb = SearchBuffer.obtainSearchBuffer();
try {
// Build an array of all the parent elements.
Vector< Element > searchContext = sb.getVector();
for (Element p = e; p != null; p = p.getParentElement()) {
searchContext.addElement(p);
}
// Build a fully qualified selector.
int n = searchContext.size();
StringBuffer cacheLookup = sb.getStringBuffer();
AttributeSet attr;
String eName;
Object name;
// >= 1 as the HTML.Tag for the 0th element is passed in.
for (int counter = n - 1; counter >= 1; counter--) {
e = searchContext.elementAt(counter);
attr = e.getAttributes();
name = attr.getAttribute(StyleConstants.NameAttribute);
eName = name.toString();
cacheLookup.append(eName);
if (attr != null) {
if (attr.isDefined(HTML.Attribute.ID)) {
cacheLookup.append('#');
cacheLookup.append(attr.getAttribute
(HTML.Attribute.ID));
}
else if (attr.isDefined(HTML.Attribute.CLASS)) {
cacheLookup.append('.');
cacheLookup.append(attr.getAttribute
(HTML.Attribute.CLASS));
}
}
cacheLookup.append(' ');
}
cacheLookup.append(t.toString());
e = searchContext.elementAt(0);
attr = e.getAttributes();
if (e.isLeaf()) {
// For leafs, we use the second tier attributes.
Object testAttr = attr.getAttribute(t);
if (testAttr instanceof AttributeSet) {
attr = (AttributeSet)testAttr;
}
else {
attr = null;
}
}
if (attr != null) {
if (attr.isDefined(HTML.Attribute.ID)) {
cacheLookup.append('#');
cacheLookup.append(attr.getAttribute(HTML.Attribute.ID));
}
else if (attr.isDefined(HTML.Attribute.CLASS)) {
cacheLookup.append('.');
cacheLookup.append(attr.getAttribute
(HTML.Attribute.CLASS));
}
}
Style style = getResolvedStyle(cacheLookup.toString(),
searchContext, t);
return style;
}
finally {
SearchBuffer.releaseSearchBuffer(sb);
}
}
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. |
String[] getSimpleSelectors(String selector) {
selector = cleanSelectorString(selector);
SearchBuffer sb = SearchBuffer.obtainSearchBuffer();
Vector< String > selectors = sb.getVector();
int lastIndex = 0;
int length = selector.length();
while (lastIndex != -1) {
int newIndex = selector.indexOf(' ', lastIndex);
if (newIndex != -1) {
selectors.addElement(selector.substring(lastIndex, newIndex));
if (++newIndex == length) {
lastIndex = -1;
}
else {
lastIndex = newIndex;
}
}
else {
selectors.addElement(selector.substring(lastIndex));
lastIndex = -1;
}
}
String[] retValue = new String[selectors.size()];
selectors.copyInto(retValue);
SearchBuffer.releaseSearchBuffer(sb);
return retValue;
}
Returns the simple selectors that comprise selector. |
int[] getSizeMap() {
return sizeMap;
}
|
static int getSpecificity(String selector) {
int specificity = 0;
boolean lastWasSpace = true;
for (int counter = 0, maxCounter = selector.length();
counter < maxCounter; counter++) {
switch(selector.charAt(counter)) {
case '.':
specificity += 100;
break;
case '#':
specificity += 10000;
break;
case ' ':
lastWasSpace = true;
break;
default:
if (lastWasSpace) {
lastWasSpace = false;
specificity += 1;
}
}
}
return specificity;
}
Returns the specificity of the passed in String. It assumes the
passed in string doesn't contain junk, that is each selector is
separated by a space and each selector at most contains one . or one
#. A simple selector has a weight of 1, an id selector has a weight
of 100, and a class selector has a weight of 10000. |
public StyleSheet[] getStyleSheets() {
StyleSheet[] retValue;
synchronized(this) {
if (linkedStyleSheets != null) {
retValue = new StyleSheet[linkedStyleSheets.size()];
linkedStyleSheets.copyInto(retValue);
}
else {
retValue = null;
}
}
return retValue;
}
Returns an array of the linked StyleSheets. Will return null
if there are no linked StyleSheets. |
public AttributeSet getViewAttributes(View v) {
return new ViewAttributeSet(v);
}
Fetches 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. |
public void importStyleSheet(URL url) {
try {
InputStream is;
is = url.openStream();
Reader r = new BufferedReader(new InputStreamReader(is));
CssParser parser = new CssParser();
parser.parse(url, r, false, true);
r.close();
is.close();
} catch (Throwable e) {
// on error we simply have no styles... the html
// will look mighty wrong but still function.
}
}
Imports a style sheet from 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. |
boolean isW3CLengthUnits() {
return w3cLengthUnits;
}
|
public void loadRules(Reader in,
URL ref) throws IOException {
CssParser parser = new CssParser();
parser.parse(ref, in, false, false);
}
Loads 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. |
void rebaseSizeMap(int base) {
final int minimalFontSize = 4;
sizeMap = new int[sizeMapDefault.length];
for (int i = 0; i < sizeMapDefault.length; i++) {
sizeMap[i] = Math.max(base * sizeMapDefault[i] /
sizeMapDefault[CSS.baseFontSizeIndex],
minimalFontSize);
}
}
|
public AttributeSet removeAttribute(AttributeSet old,
Object key) {
if (key instanceof StyleConstants) {
HTML.Tag tag = HTML.getTagForStyleConstantsKey(
(StyleConstants)key);
if (tag != null) {
old = super.removeAttribute(old, tag);
}
Object cssKey = css.styleConstantsKeyToCSSKey((StyleConstants)key);
if (cssKey != null) {
return super.removeAttribute(old, cssKey);
}
}
return super.removeAttribute(old, key);
}
Removes an attribute from the set. If the attribute is a StyleConstants
attribute, the request will be converted to a CSS attribute prior to
forwarding to the superclass behavior. |
public AttributeSet removeAttributes(AttributeSet old,
Enumeration<?> names) {
// PENDING: Should really be doing something similar to
// removeHTMLTags here, but it is rather expensive to have to
// clone names
return super.removeAttributes(old, names);
}
Removes a set of attributes for the element. If any of the attributes
is a StyleConstants attribute, the request will be converted to a CSS
attribute prior to forwarding to the superclass behavior. |
public AttributeSet removeAttributes(AttributeSet old,
AttributeSet attrs) {
if (old != attrs) {
old = removeHTMLTags(old, attrs);
}
return super.removeAttributes(old, convertAttributeSet(attrs));
}
Removes a set of attributes. If any of the attributes
is a StyleConstants attribute, the request will be converted to a CSS
attribute prior to forwarding to the superclass behavior. |
public void removeStyle(String nm) {
Style aStyle = getStyle(nm);
if (aStyle != null) {
String selector = cleanSelectorString(nm);
String[] selectors = getSimpleSelectors(selector);
synchronized(this) {
SelectorMapping mapping = getRootSelectorMapping();
for (int i = selectors.length - 1; i >= 0; i--) {
mapping = mapping.getChildSelectorMapping(selectors[i],
true);
}
Style rule = mapping.getStyle();
if (rule != null) {
mapping.setStyle(null);
if (resolvedStyles.size() > 0) {
Enumeration< ResolvedStyle > values = resolvedStyles.elements();
while (values.hasMoreElements()) {
ResolvedStyle style = values.nextElement();
style.removeStyle(rule);
}
}
}
}
}
super.removeStyle(nm);
}
Removes a named style previously added to the document. |
public void removeStyleSheet(StyleSheet ss) {
synchronized(this) {
if (linkedStyleSheets != null) {
int index = linkedStyleSheets.indexOf(ss);
if (index != -1) {
linkedStyleSheets.removeElementAt(index);
unlinkStyleSheet(ss, index);
if (index == 0 && linkedStyleSheets.size() == 0) {
linkedStyleSheets = null;
}
}
}
}
}
Removes the StyleSheet ss from those of the receiver. |
public void setBase(URL base) {
this.base = base;
}
Sets the base. All import statements that are relative, will be
relative to base . |
public void setBaseFontSize(int sz) {
css.setBaseFontSize(sz);
}
Sets the base font size, with valid values between 1 and 7. |
public void setBaseFontSize(String size) {
css.setBaseFontSize(size);
}
Sets the base font size from the passed in String. The string
can either identify a specific font size, with legal values between
1 and 7, or identifiy a relative font size such as +1 or -2. |
public Color stringToColor(String string) {
return CSS.stringToColor(string);
}
Converts a color string such as "RED" or "#NNNNNN" to a Color.
Note: This will only convert the HTML3.2 color strings
or a string of length 7;
otherwise, it will return null. |
public AttributeSet translateHTMLToCSS(AttributeSet htmlAttrSet) {
AttributeSet cssAttrSet = css.translateHTMLToCSS(htmlAttrSet);
MutableAttributeSet cssStyleSet = addStyle(null, null);
cssStyleSet.addAttributes(cssAttrSet);
return cssStyleSet;
}
Converts a set of HTML attributes to an equivalent
set of CSS attributes. |