001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Alexey A. Ivanov
019: * @version $Revision$
020: */package javax.swing.text;
021:
022: import java.io.IOException;
023: import java.io.ObjectInputStream;
024: import java.io.ObjectOutputStream;
025: import java.io.Serializable;
026: import java.util.Enumeration;
027: import java.util.Hashtable;
028:
029: public class SimpleAttributeSet implements Cloneable,
030: MutableAttributeSet, Serializable {
031:
032: public static final AttributeSet EMPTY = EmptyAttributeSet.EMPTY;
033:
034: /**
035: * This is an instance of hashtable. It contains all attributes.
036: * It is declared transient for correct serialization of AttributeSet.
037: */
038: private transient Hashtable hashtable = new Hashtable();
039:
040: public SimpleAttributeSet() {
041: }
042:
043: public SimpleAttributeSet(final AttributeSet source) {
044: this ();
045: addAttributes(source);
046: }
047:
048: public void addAttribute(final Object name, final Object value) {
049: hashtable.put(name, value);
050: }
051:
052: public void addAttributes(final AttributeSet attributes) {
053: for (Enumeration e = attributes.getAttributeNames(); e
054: .hasMoreElements();) {
055:
056: Object name = e.nextElement();
057: addAttribute(name, attributes.getAttribute(name));
058: }
059: }
060:
061: public Object clone() {
062: return new SimpleAttributeSet(this );
063: }
064:
065: public boolean containsAttribute(final Object name,
066: final Object value) {
067: Object obtValue = getAttribute(name);
068: return (obtValue == null) ? false : obtValue.equals(value);
069: }
070:
071: public boolean containsAttributes(final AttributeSet attributes) {
072: for (Enumeration e = attributes.getAttributeNames(); e
073: .hasMoreElements();) {
074:
075: Object name = e.nextElement();
076: if (!containsAttribute(name, attributes.getAttribute(name))) {
077: return false;
078: }
079: }
080: return true;
081: }
082:
083: public AttributeSet copyAttributes() {
084: return new SimpleAttributeSet(this );
085: }
086:
087: public boolean equals(final Object obj) {
088: if (obj instanceof AttributeSet) {
089: return isEqual((AttributeSet) obj);
090: }
091:
092: return false;
093: }
094:
095: public Object getAttribute(final Object name) {
096: Object value = hashtable.get(name);
097: if (value == null) {
098: value = hashtable.get(AttributeSet.ResolveAttribute);
099: if (value instanceof AttributeSet) {
100: value = ((AttributeSet) value).getAttribute(name);
101: }
102: }
103: return value;
104: }
105:
106: public int getAttributeCount() {
107: return hashtable.size();
108: }
109:
110: public Enumeration<?> getAttributeNames() {
111: return hashtable.keys();
112: }
113:
114: public AttributeSet getResolveParent() {
115: return (AttributeSet) hashtable
116: .get(AttributeSet.ResolveAttribute);
117: }
118:
119: public int hashCode() {
120: //Hash code of an empty instance must be equal to EMPTY.hashCode()
121: if (hashtable.size() == 0) {
122: return EMPTY.hashCode();
123: }
124: return hashtable.hashCode();
125: }
126:
127: public boolean isDefined(final Object attrName) {
128: return hashtable.containsKey(attrName);
129: }
130:
131: public boolean isEmpty() {
132: return hashtable.isEmpty();
133: }
134:
135: public boolean isEqual(final AttributeSet attr) {
136: if (getAttributeCount() != attr.getAttributeCount()) {
137: return false;
138: }
139:
140: for (Enumeration e = hashtable.keys(); e.hasMoreElements();) {
141: Object name = e.nextElement();
142: if (!attr.containsAttribute(name, hashtable.get(name))) {
143: return false;
144: }
145: }
146:
147: return true;
148: }
149:
150: public void removeAttribute(final Object name) {
151: hashtable.remove(name);
152: }
153:
154: public void removeAttributes(final AttributeSet attributes) {
155: Enumeration e = attributes.getAttributeNames();
156: while (e.hasMoreElements()) {
157: Object name = e.nextElement();
158: Object this Value = hashtable.get(name);
159: if (this Value != null) {
160: if (this Value.equals(attributes.getAttribute(name))) {
161: removeAttribute(name);
162: }
163: }
164: }
165: }
166:
167: public void removeAttributes(final Enumeration<?> names) {
168: while (names.hasMoreElements()) {
169: removeAttribute(names.nextElement());
170: }
171: }
172:
173: public void setResolveParent(final AttributeSet parent) {
174: addAttribute(AttributeSet.ResolveAttribute, parent);
175: }
176:
177: /*
178: * The format of the string is based on 1.5 release behavior
179: * which can be revealed using the following code:
180: *
181: * SimpleAttributeSet obj = new SimpleAttributeSet();
182: * obj.addAttribute("attribute", "value");
183: * System.out.println(obj.toString());
184: */
185: public String toString() {
186: String str = new String();
187: for (Enumeration e = hashtable.keys(); e.hasMoreElements();) {
188: Object name = e.nextElement();
189: str += name + "=";
190: Object value = hashtable.get(name);
191: if (value instanceof AttributeSet) {
192: str += "**Attribute Set**" + " ";
193: } else {
194: str += value + " ";
195: }
196: }
197: return str;
198: }
199:
200: /**
201: * Removes all the attributes from the set. Internally clears the attribute
202: * storage <code>hashtable</code>.
203: */
204: final void removeAll() {
205: hashtable.clear();
206: }
207:
208: private void readObject(final ObjectInputStream in)
209: throws IOException, ClassNotFoundException {
210:
211: in.defaultReadObject();
212: hashtable = new Hashtable();
213: StyleContext.readAttributeSet(in, this );
214: }
215:
216: private void writeObject(final ObjectOutputStream out)
217: throws IOException {
218: out.defaultWriteObject();
219: StyleContext.writeAttributeSet(out, this);
220: }
221: }
|