OSDN Git Service

Merged gcj-eclipse branch to trunk.
[pf3gnuchains/gcc-fork.git] / libjava / classpath / javax / swing / text / html / MultiAttributeSet.java
1 /* MultiAttributeSet.java -- Multiplexes between a set of AttributeSets
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.Enumeration;
42 import java.util.NoSuchElementException;
43
44 import javax.swing.text.AttributeSet;
45 import javax.swing.text.SimpleAttributeSet;
46
47 /**
48  * An AttributeSet impl that multiplexes between a set of other AttributeSets.
49  *
50  * @author Roman Kennke (kennke@aicas.com)
51  */
52 class MultiAttributeSet
53   implements AttributeSet
54 {
55
56   /**
57    * The Enumeration for the multiplexed names.
58    */
59   private class MultiNameEnumeration
60     implements Enumeration
61   {
62     /**
63      * The index of the current AttributeSet.
64      */
65     private int index;
66
67     /**
68      * The names Enumeration of the current AttributeSet.
69      */
70     private Enumeration current;
71
72     /**
73      * Creates a new instance.
74      */
75     MultiNameEnumeration()
76     {
77       index = 0;
78       current = multi[0].getAttributeNames();
79     }
80
81     public boolean hasMoreElements()
82     {
83       return current.hasMoreElements() || index < multi.length - 1;
84     }
85
86     public Object nextElement()
87     {
88       if (! current.hasMoreElements())
89         {
90           if (index < multi.length - 1)
91             {
92               index++;
93               current = multi[index].getAttributeNames();
94             }
95           else
96             throw new NoSuchElementException();
97         }
98       return current.nextElement();
99     }
100     
101   }
102
103   /**
104    * The AttributeSets to multiplex.
105    */
106   AttributeSet[] multi;
107
108   /**
109    * Provided for subclasses that need to initialize via {@link #init}.
110    */
111   MultiAttributeSet()
112   {
113     // Nothing to do here.
114   }
115
116   /**
117    * Creates a new instance.
118    *
119    * @param m the AttributeSets to multiplex
120    */
121   MultiAttributeSet(AttributeSet[] m)
122   {
123     init(m);
124   }
125
126   /**
127    * Provided for subclasses to initialize the attribute set.
128    *
129    * @param m the attributes to multiplex
130    */
131   void init(AttributeSet[] m)
132   {
133     multi = m;
134   }
135
136   public boolean containsAttribute(Object name, Object value)
137   {
138     boolean ret = false;
139     for (int i = 0; i < multi.length && ret == false; i++)
140       {
141         if (multi[i].containsAttribute(name, value))
142           ret = true;
143       }
144     return ret;
145   }
146
147   public boolean containsAttributes(AttributeSet attributes)
148   {
149     boolean ret = true;
150     Enumeration e = attributes.getAttributeNames();
151     while (ret && e.hasMoreElements())
152       {
153         Object key = e.nextElement();
154         ret = attributes.getAttribute(key).equals(getAttribute(key));
155       }
156     return ret;
157   }
158
159   public AttributeSet copyAttributes()
160   {
161     SimpleAttributeSet copy = new SimpleAttributeSet();
162     for (int i = 0; i < multi.length; i++)
163       {
164         copy.addAttributes(multi[i]);
165       }
166     return copy;
167   }
168
169   public Object getAttribute(Object key)
170   {
171     Object ret = null;
172     for (int i = 0; i < multi.length && ret == null; i++)
173       {
174         ret = multi[i].getAttribute(key);
175       }
176     return ret;
177   }
178
179   public int getAttributeCount()
180   {
181     int n = 0;
182     for (int i = 0; i < multi.length; i++)
183       {
184         n += multi[i].getAttributeCount();
185       }
186     return n;
187   }
188
189   public Enumeration getAttributeNames()
190   {
191     return new MultiNameEnumeration();
192   }
193
194   public AttributeSet getResolveParent()
195   {
196     return null;
197   }
198
199   public boolean isDefined(Object attrName)
200   {
201     boolean ret = false;
202     for (int i = 0; i < multi.length && ! ret; i++)
203       ret = multi[i].isDefined(attrName);
204     return ret;
205   }
206
207   public boolean isEqual(AttributeSet attr)
208   {
209     return getAttributeCount() == attr.getAttributeCount()
210            && containsAttributes(attr);
211   }
212
213 }