1 |
772 |
jeremybenn |
/* ViewAttributeSet.java -- The AttributeSet used by HTML views
|
2 |
|
|
Copyright (C) 2006 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
9 |
|
|
any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
package javax.swing.text.html;
|
40 |
|
|
|
41 |
|
|
import java.util.ArrayList;
|
42 |
|
|
import java.util.Enumeration;
|
43 |
|
|
|
44 |
|
|
import javax.swing.text.AttributeSet;
|
45 |
|
|
import javax.swing.text.Element;
|
46 |
|
|
import javax.swing.text.StyleConstants;
|
47 |
|
|
import javax.swing.text.View;
|
48 |
|
|
|
49 |
|
|
/**
|
50 |
|
|
* An AttributeSet implemenation that is used by the HTML views. This
|
51 |
|
|
* AttributeSet is created by StyleSheet.getViewAttributes() and combines
|
52 |
|
|
* the following attributes:
|
53 |
|
|
* - The original attributes of the View's element.
|
54 |
|
|
* - Any translated (HTML->CSS) attributes, as returned by
|
55 |
|
|
* StyleSheet.translateHTMLToCS().
|
56 |
|
|
* - CSS Styles as resolved by the CSS stylesheet.
|
57 |
|
|
*
|
58 |
|
|
* In addition to that, it resolves attributes to the parent views, if
|
59 |
|
|
* a CSS attribute is requested that is inheritable.
|
60 |
|
|
*
|
61 |
|
|
* @author Roman Kennke (kennke@aicas.com)
|
62 |
|
|
*/
|
63 |
|
|
class ViewAttributeSet
|
64 |
|
|
extends MultiAttributeSet
|
65 |
|
|
{
|
66 |
|
|
|
67 |
|
|
/**
|
68 |
|
|
* The view for which we are the AttributeSet.
|
69 |
|
|
*/
|
70 |
|
|
private View view;
|
71 |
|
|
|
72 |
|
|
/**
|
73 |
|
|
* The stylesheet to use.
|
74 |
|
|
*/
|
75 |
|
|
private StyleSheet styleSheet;
|
76 |
|
|
|
77 |
|
|
/**
|
78 |
|
|
* Creates a new instance.
|
79 |
|
|
*
|
80 |
|
|
* @param v the view for which to do the AttributeSet
|
81 |
|
|
*/
|
82 |
|
|
ViewAttributeSet(View v, StyleSheet ss)
|
83 |
|
|
{
|
84 |
|
|
styleSheet = ss;
|
85 |
|
|
view = v;
|
86 |
|
|
ArrayList<AttributeSet> atts = new ArrayList<AttributeSet>();
|
87 |
|
|
|
88 |
|
|
Element el = v.getElement();
|
89 |
|
|
AttributeSet elAtts = el.getAttributes();
|
90 |
|
|
AttributeSet htmlAtts = styleSheet.translateHTMLToCSS(elAtts);
|
91 |
|
|
if (htmlAtts.getAttributeCount() > 0)
|
92 |
|
|
atts.add(htmlAtts);
|
93 |
|
|
|
94 |
|
|
if (el.isLeaf())
|
95 |
|
|
{
|
96 |
|
|
Enumeration<?> n = elAtts.getAttributeNames();
|
97 |
|
|
while (n.hasMoreElements())
|
98 |
|
|
{
|
99 |
|
|
Object key = n.nextElement();
|
100 |
|
|
if (key instanceof HTML.Tag)
|
101 |
|
|
{
|
102 |
|
|
AttributeSet rule = styleSheet.getRule((HTML.Tag) key, el);
|
103 |
|
|
if (rule != null)
|
104 |
|
|
atts.add(rule);
|
105 |
|
|
}
|
106 |
|
|
}
|
107 |
|
|
}
|
108 |
|
|
else
|
109 |
|
|
{
|
110 |
|
|
HTML.Tag tag =
|
111 |
|
|
(HTML.Tag) elAtts.getAttribute(StyleConstants.NameAttribute);
|
112 |
|
|
AttributeSet rule = styleSheet.getRule(tag, el);
|
113 |
|
|
if (rule != null)
|
114 |
|
|
atts.add(rule);
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
AttributeSet[] atts1 = new AttributeSet[atts.size()];
|
118 |
|
|
atts1 = atts.toArray(atts1);
|
119 |
|
|
init(atts1);
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
/**
|
123 |
|
|
* Fetches the attribute for the specific ckey. If the attribute
|
124 |
|
|
* can't be found and the key is a CSS.Attribute that is inherited,
|
125 |
|
|
* then the attribute is looked up in the resolve parent.
|
126 |
|
|
*/
|
127 |
|
|
public Object getAttribute(Object key)
|
128 |
|
|
{
|
129 |
|
|
Object val = super.getAttribute(key);
|
130 |
|
|
if (val == null)
|
131 |
|
|
{
|
132 |
|
|
// Didn't find value. If the key is a CSS.Attribute, and is
|
133 |
|
|
// inherited, then ask the resolve parent.
|
134 |
|
|
if (key instanceof CSS.Attribute)
|
135 |
|
|
{
|
136 |
|
|
CSS.Attribute cssKey = (CSS.Attribute) key;
|
137 |
|
|
if (cssKey.isInherited())
|
138 |
|
|
{
|
139 |
|
|
AttributeSet resolveParent = getResolveParent();
|
140 |
|
|
if (resolveParent != null)
|
141 |
|
|
val = resolveParent.getAttribute(cssKey);
|
142 |
|
|
}
|
143 |
|
|
}
|
144 |
|
|
}
|
145 |
|
|
return val;
|
146 |
|
|
}
|
147 |
|
|
|
148 |
|
|
/**
|
149 |
|
|
* Returns the resolve parent of this AttributeSet. This is the AttributeSet
|
150 |
|
|
* returned by the parent view if available.
|
151 |
|
|
*/
|
152 |
|
|
public AttributeSet getResolveParent()
|
153 |
|
|
{
|
154 |
|
|
AttributeSet parent = null;
|
155 |
|
|
if (view != null)
|
156 |
|
|
{
|
157 |
|
|
View parentView = view.getParent();
|
158 |
|
|
if (parentView != null)
|
159 |
|
|
parent = parentView.getAttributes();
|
160 |
|
|
}
|
161 |
|
|
return parent;
|
162 |
|
|
}
|
163 |
|
|
}
|