libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / javax / swing / text / html / ViewAttributeSet.java
blob8838646d5f5f29fe737345f585f49b165d147fa4
1 /* ViewAttributeSet.java -- The AttributeSet used by HTML views
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
39 package javax.swing.text.html;
41 import java.util.ArrayList;
42 import java.util.Enumeration;
44 import javax.swing.text.AttributeSet;
45 import javax.swing.text.Element;
46 import javax.swing.text.StyleConstants;
47 import javax.swing.text.View;
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.
58 * In addition to that, it resolves attributes to the parent views, if
59 * a CSS attribute is requested that is inheritable.
61 * @author Roman Kennke (kennke@aicas.com)
63 class ViewAttributeSet
64 extends MultiAttributeSet
67 /**
68 * The view for which we are the AttributeSet.
70 private View view;
72 /**
73 * The stylesheet to use.
75 private StyleSheet styleSheet;
77 /**
78 * Creates a new instance.
80 * @param v the view for which to do the AttributeSet
82 ViewAttributeSet(View v, StyleSheet ss)
84 styleSheet = ss;
85 view = v;
86 ArrayList<AttributeSet> atts = new ArrayList<AttributeSet>();
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);
94 if (el.isLeaf())
96 Enumeration<?> n = elAtts.getAttributeNames();
97 while (n.hasMoreElements())
99 Object key = n.nextElement();
100 if (key instanceof HTML.Tag)
102 AttributeSet rule = styleSheet.getRule((HTML.Tag) key, el);
103 if (rule != null)
104 atts.add(rule);
108 else
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);
117 AttributeSet[] atts1 = new AttributeSet[atts.size()];
118 atts1 = atts.toArray(atts1);
119 init(atts1);
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.
127 public Object getAttribute(Object key)
129 Object val = super.getAttribute(key);
130 if (val == null)
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)
136 CSS.Attribute cssKey = (CSS.Attribute) key;
137 if (cssKey.isInherited())
139 AttributeSet resolveParent = getResolveParent();
140 if (resolveParent != null)
141 val = resolveParent.getAttribute(cssKey);
145 return val;
149 * Returns the resolve parent of this AttributeSet. This is the AttributeSet
150 * returned by the parent view if available.
152 public AttributeSet getResolveParent()
154 AttributeSet parent = null;
155 if (view != null)
157 View parentView = view.getParent();
158 if (parentView != null)
159 parent = parentView.getAttributes();
161 return parent;