001 /*
002 * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 package javax.swing.text.html;
026
027 import javax.swing.text.*;
028 import java.io.Serializable;
029 import java.util.*;
030
031 /**
032 * An implementation of <code>AttributeSet</code> that can multiplex
033 * across a set of <code>AttributeSet</code>s.
034 *
035 * @version 1.11 05/05/07
036 */
037 class MuxingAttributeSet implements AttributeSet, Serializable {
038 /**
039 * Creates a <code>MuxingAttributeSet</code> with the passed in
040 * attributes.
041 */
042 public MuxingAttributeSet(AttributeSet[] attrs) {
043 this .attrs = attrs;
044 }
045
046 /**
047 * Creates an empty <code>MuxingAttributeSet</code>. This is intended for
048 * use by subclasses only, and it is also intended that subclasses will
049 * set the constituent <code>AttributeSet</code>s before invoking any
050 * of the <code>AttributeSet</code> methods.
051 */
052 protected MuxingAttributeSet() {
053 }
054
055 /**
056 * Directly sets the <code>AttributeSet</code>s that comprise this
057 * <code>MuxingAttributeSet</code>.
058 */
059 protected synchronized void setAttributes(AttributeSet[] attrs) {
060 this .attrs = attrs;
061 }
062
063 /**
064 * Returns the <code>AttributeSet</code>s multiplexing too. When the
065 * <code>AttributeSet</code>s need to be referenced, this should be called.
066 */
067 protected synchronized AttributeSet[] getAttributes() {
068 return attrs;
069 }
070
071 /**
072 * Inserts <code>as</code> at <code>index</code>. This assumes
073 * the value of <code>index</code> is between 0 and attrs.length,
074 * inclusive.
075 */
076 protected synchronized void insertAttributeSetAt(AttributeSet as,
077 int index) {
078 int numAttrs = attrs.length;
079 AttributeSet newAttrs[] = new AttributeSet[numAttrs + 1];
080 if (index < numAttrs) {
081 if (index > 0) {
082 System.arraycopy(attrs, 0, newAttrs, 0, index);
083 System.arraycopy(attrs, index, newAttrs, index + 1,
084 numAttrs - index);
085 } else {
086 System.arraycopy(attrs, 0, newAttrs, 1, numAttrs);
087 }
088 } else {
089 System.arraycopy(attrs, 0, newAttrs, 0, numAttrs);
090 }
091 newAttrs[index] = as;
092 attrs = newAttrs;
093 }
094
095 /**
096 * Removes the AttributeSet at <code>index</code>. This assumes
097 * the value of <code>index</code> is greater than or equal to 0,
098 * and less than attrs.length.
099 */
100 protected synchronized void removeAttributeSetAt(int index) {
101 int numAttrs = attrs.length;
102 AttributeSet[] newAttrs = new AttributeSet[numAttrs - 1];
103 if (numAttrs > 0) {
104 if (index == 0) {
105 // FIRST
106 System.arraycopy(attrs, 1, newAttrs, 0, numAttrs - 1);
107 } else if (index < (numAttrs - 1)) {
108 // MIDDLE
109 System.arraycopy(attrs, 0, newAttrs, 0, index);
110 System.arraycopy(attrs, index + 1, newAttrs, index,
111 numAttrs - index - 1);
112 } else {
113 // END
114 System.arraycopy(attrs, 0, newAttrs, 0, numAttrs - 1);
115 }
116 }
117 attrs = newAttrs;
118 }
119
120 // --- AttributeSet methods ----------------------------
121
122 /**
123 * Gets the number of attributes that are defined.
124 *
125 * @return the number of attributes
126 * @see AttributeSet#getAttributeCount
127 */
128 public int getAttributeCount() {
129 AttributeSet[] as = getAttributes();
130 int n = 0;
131 for (int i = 0; i < as.length; i++) {
132 n += as[i].getAttributeCount();
133 }
134 return n;
135 }
136
137 /**
138 * Checks whether a given attribute is defined.
139 * This will convert the key over to CSS if the
140 * key is a StyleConstants key that has a CSS
141 * mapping.
142 *
143 * @param key the attribute key
144 * @return true if the attribute is defined
145 * @see AttributeSet#isDefined
146 */
147 public boolean isDefined(Object key) {
148 AttributeSet[] as = getAttributes();
149 for (int i = 0; i < as.length; i++) {
150 if (as[i].isDefined(key)) {
151 return true;
152 }
153 }
154 return false;
155 }
156
157 /**
158 * Checks whether two attribute sets are equal.
159 *
160 * @param attr the attribute set to check against
161 * @return true if the same
162 * @see AttributeSet#isEqual
163 */
164 public boolean isEqual(AttributeSet attr) {
165 return ((getAttributeCount() == attr.getAttributeCount()) && containsAttributes(attr));
166 }
167
168 /**
169 * Copies a set of attributes.
170 *
171 * @return the copy
172 * @see AttributeSet#copyAttributes
173 */
174 public AttributeSet copyAttributes() {
175 AttributeSet[] as = getAttributes();
176 MutableAttributeSet a = new SimpleAttributeSet();
177 int n = 0;
178 for (int i = as.length - 1; i >= 0; i--) {
179 a.addAttributes(as[i]);
180 }
181 return a;
182 }
183
184 /**
185 * Gets the value of an attribute. If the requested
186 * attribute is a StyleConstants attribute that has
187 * a CSS mapping, the request will be converted.
188 *
189 * @param key the attribute name
190 * @return the attribute value
191 * @see AttributeSet#getAttribute
192 */
193 public Object getAttribute(Object key) {
194 AttributeSet[] as = getAttributes();
195 int n = as.length;
196 for (int i = 0; i < n; i++) {
197 Object o = as[i].getAttribute(key);
198 if (o != null) {
199 return o;
200 }
201 }
202 return null;
203 }
204
205 /**
206 * Gets the names of all attributes.
207 *
208 * @return the attribute names
209 * @see AttributeSet#getAttributeNames
210 */
211 public Enumeration getAttributeNames() {
212 return new MuxingAttributeNameEnumeration();
213 }
214
215 /**
216 * Checks whether a given attribute name/value is defined.
217 *
218 * @param name the attribute name
219 * @param value the attribute value
220 * @return true if the name/value is defined
221 * @see AttributeSet#containsAttribute
222 */
223 public boolean containsAttribute(Object name, Object value) {
224 return value.equals(getAttribute(name));
225 }
226
227 /**
228 * Checks whether the attribute set contains all of
229 * the given attributes.
230 *
231 * @param attrs the attributes to check
232 * @return true if the element contains all the attributes
233 * @see AttributeSet#containsAttributes
234 */
235 public boolean containsAttributes(AttributeSet attrs) {
236 boolean result = true;
237
238 Enumeration names = attrs.getAttributeNames();
239 while (result && names.hasMoreElements()) {
240 Object name = names.nextElement();
241 result = attrs.getAttribute(name)
242 .equals(getAttribute(name));
243 }
244
245 return result;
246 }
247
248 /**
249 * Returns null, subclasses may wish to do something more
250 * intelligent with this.
251 */
252 public AttributeSet getResolveParent() {
253 return null;
254 }
255
256 /**
257 * The <code>AttributeSet</code>s that make up the resulting
258 * <code>AttributeSet</code>.
259 */
260 private AttributeSet[] attrs;
261
262 /**
263 * An Enumeration of the Attribute names in a MuxingAttributeSet.
264 * This may return the same name more than once.
265 */
266 private class MuxingAttributeNameEnumeration implements Enumeration {
267
268 MuxingAttributeNameEnumeration() {
269 updateEnum();
270 }
271
272 public boolean hasMoreElements() {
273 if (currentEnum == null) {
274 return false;
275 }
276 return currentEnum.hasMoreElements();
277 }
278
279 public Object nextElement() {
280 if (currentEnum == null) {
281 throw new NoSuchElementException("No more names");
282 }
283 Object retObject = currentEnum.nextElement();
284 if (!currentEnum.hasMoreElements()) {
285 updateEnum();
286 }
287 return retObject;
288 }
289
290 void updateEnum() {
291 AttributeSet[] as = getAttributes();
292 currentEnum = null;
293 while (currentEnum == null && attrIndex < as.length) {
294 currentEnum = as[attrIndex++].getAttributeNames();
295 if (!currentEnum.hasMoreElements()) {
296 currentEnum = null;
297 }
298 }
299 }
300
301 /** Index into attrs the current Enumeration came from. */
302 private int attrIndex;
303 /** Enumeration from attrs. */
304 private Enumeration currentEnum;
305 }
306 }
|