01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: /**
18: * @author Alexey A. Ivanov
19: * @version $Revision$
20: */package javax.swing.text.html;
21:
22: import javax.swing.text.AttributeSet;
23: import javax.swing.text.Element;
24: import javax.swing.text.View;
25: import javax.swing.text.html.CSS.RelativeValueResolver;
26:
27: class ViewAttributeSet extends CompositeAttributeSet {
28: final View view;
29:
30: ViewAttributeSet(final StyleSheet ss, final View view) {
31: super (calculateElementAttr(ss, view), calculateCSSRules(ss,
32: view));
33: this .view = view;
34: }
35:
36: public AttributeSet getResolveParent() {
37: final View parent = view.getParent();
38: return parent != null ? parent.getAttributes() : null;
39: }
40:
41: public Object getAttribute(final Object key) {
42: if (key == ResolveAttribute) {
43: return getResolveParent();
44: }
45:
46: Object result = getElementAttr().getAttribute(key);
47: if (result != null) {
48: return result;
49: }
50:
51: if (getCssRules().getAttributeCount() > 0) {
52: result = ((CascadedStyle) getCssRules()).getAttribute(key,
53: view.getElement());
54: if (result != null) {
55: return result;
56: }
57: }
58:
59: final AttributeSet resolver = getResolveParent();
60: if (resolver == null) {
61: return null;
62: }
63:
64: if (key instanceof CSS.Attribute) {
65: CSS.Attribute cssKey = (CSS.Attribute) key;
66: if (cssKey.isInherited()) {
67: result = resolver.getAttribute(key);
68: if (result instanceof RelativeValueResolver) {
69: return ((RelativeValueResolver) result)
70: .getComputedValue(view.getParent());
71: }
72: return result;
73: }
74: return null;
75: }
76: return resolver.getAttribute(key);
77: }
78:
79: private static AttributeSet calculateElementAttr(
80: final StyleSheet ss, final View view) {
81: final Element element = view.getElement();
82: return ss.translateHTMLToCSS(element.getAttributes());
83: }
84:
85: private static AttributeSet calculateCSSRules(final StyleSheet ss,
86: final View view) {
87: HTML.Tag tag = SelectorMatcher.getTag(view.getElement());
88: return tag != null ? ss.getRule(tag, view.getElement()) : ss
89: .getEmptySet();
90: }
91:
92: private AttributeSet getElementAttr() {
93: return getPrimarySet();
94: }
95:
96: private AttributeSet getCssRules() {
97: return getSecondarySet();
98: }
99: }
|