001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.activities;
011:
012: import java.util.ArrayList;
013: import java.util.HashSet;
014: import java.util.List;
015: import java.util.Set;
016:
017: import org.eclipse.ui.activities.CategoryEvent;
018: import org.eclipse.ui.activities.ICategory;
019: import org.eclipse.ui.activities.ICategoryActivityBinding;
020: import org.eclipse.ui.activities.ICategoryListener;
021: import org.eclipse.ui.activities.NotDefinedException;
022: import org.eclipse.ui.internal.util.Util;
023:
024: final class Category implements ICategory {
025: private final static int HASH_FACTOR = 89;
026:
027: private final static int HASH_INITIAL = Category.class.getName()
028: .hashCode();
029:
030: private final static Set strongReferences = new HashSet();
031:
032: private Set categoryActivityBindings;
033:
034: private transient ICategoryActivityBinding[] categoryActivityBindingsAsArray;
035:
036: private List categoryListeners;
037:
038: private boolean defined;
039:
040: private transient int hashCode = HASH_INITIAL;
041:
042: private String id;
043:
044: private String name;
045:
046: private transient String string;
047:
048: private String description;
049:
050: Category(String id) {
051: if (id == null) {
052: throw new NullPointerException();
053: }
054:
055: this .id = id;
056: }
057:
058: public void addCategoryListener(ICategoryListener categoryListener) {
059: if (categoryListener == null) {
060: throw new NullPointerException();
061: }
062:
063: if (categoryListeners == null) {
064: categoryListeners = new ArrayList();
065: }
066:
067: if (!categoryListeners.contains(categoryListener)) {
068: categoryListeners.add(categoryListener);
069: }
070:
071: strongReferences.add(this );
072: }
073:
074: public int compareTo(Object object) {
075: Category castedObject = (Category) object;
076: int compareTo = Util.compare(categoryActivityBindingsAsArray,
077: castedObject.categoryActivityBindingsAsArray);
078:
079: if (compareTo == 0) {
080: compareTo = Util.compare(defined, castedObject.defined);
081:
082: if (compareTo == 0) {
083: compareTo = Util.compare(id, castedObject.id);
084:
085: if (compareTo == 0) {
086: compareTo = Util.compare(name, castedObject.name);
087: }
088: }
089: }
090:
091: return compareTo;
092: }
093:
094: public boolean equals(Object object) {
095: if (!(object instanceof Category)) {
096: return false;
097: }
098:
099: final Category castedObject = (Category) object;
100: if (!Util.equals(categoryActivityBindings,
101: castedObject.categoryActivityBindings)) {
102: return false;
103: }
104:
105: if (!Util.equals(defined, castedObject.defined)) {
106: return false;
107: }
108:
109: if (!Util.equals(id, castedObject.id)) {
110: return false;
111: }
112:
113: return Util.equals(name, castedObject.name);
114: }
115:
116: void fireCategoryChanged(CategoryEvent categoryEvent) {
117: if (categoryEvent == null) {
118: throw new NullPointerException();
119: }
120:
121: if (categoryListeners != null) {
122: for (int i = 0; i < categoryListeners.size(); i++) {
123: ((ICategoryListener) categoryListeners.get(i))
124: .categoryChanged(categoryEvent);
125: }
126: }
127: }
128:
129: public Set getCategoryActivityBindings() {
130: return categoryActivityBindings;
131: }
132:
133: public String getId() {
134: return id;
135: }
136:
137: public String getName() throws NotDefinedException {
138: if (!defined) {
139: throw new NotDefinedException();
140: }
141:
142: return name;
143: }
144:
145: public int hashCode() {
146: if (hashCode == HASH_INITIAL) {
147: hashCode = hashCode * HASH_FACTOR
148: + Util.hashCode(categoryActivityBindings);
149: hashCode = hashCode * HASH_FACTOR + Util.hashCode(defined);
150: hashCode = hashCode * HASH_FACTOR + Util.hashCode(id);
151: hashCode = hashCode * HASH_FACTOR + Util.hashCode(name);
152: if (hashCode == HASH_INITIAL) {
153: hashCode++;
154: }
155: }
156:
157: return hashCode;
158: }
159:
160: public boolean isDefined() {
161: return defined;
162: }
163:
164: public void removeCategoryListener(
165: ICategoryListener categoryListener) {
166: if (categoryListener == null) {
167: throw new NullPointerException();
168: }
169:
170: if (categoryListeners != null) {
171: categoryListeners.remove(categoryListener);
172: }
173:
174: if (categoryListeners.isEmpty()) {
175: strongReferences.remove(this );
176: }
177: }
178:
179: boolean setCategoryActivityBindings(Set categoryActivityBindings) {
180: categoryActivityBindings = Util.safeCopy(
181: categoryActivityBindings,
182: ICategoryActivityBinding.class);
183:
184: if (!Util.equals(categoryActivityBindings,
185: this .categoryActivityBindings)) {
186: this .categoryActivityBindings = categoryActivityBindings;
187: this .categoryActivityBindingsAsArray = (ICategoryActivityBinding[]) this .categoryActivityBindings
188: .toArray(new ICategoryActivityBinding[this .categoryActivityBindings
189: .size()]);
190: hashCode = HASH_INITIAL;
191: string = null;
192: return true;
193: }
194:
195: return false;
196: }
197:
198: boolean setDefined(boolean defined) {
199: if (defined != this .defined) {
200: this .defined = defined;
201: hashCode = HASH_INITIAL;
202: string = null;
203: return true;
204: }
205:
206: return false;
207: }
208:
209: boolean setName(String name) {
210: if (!Util.equals(name, this .name)) {
211: this .name = name;
212: hashCode = HASH_INITIAL;
213: string = null;
214: return true;
215: }
216:
217: return false;
218: }
219:
220: public String toString() {
221: if (string == null) {
222: final StringBuffer stringBuffer = new StringBuffer();
223: stringBuffer.append('[');
224: stringBuffer.append(categoryActivityBindings);
225: stringBuffer.append(',');
226: stringBuffer.append(defined);
227: stringBuffer.append(',');
228: stringBuffer.append(id);
229: stringBuffer.append(',');
230: stringBuffer.append(name);
231: stringBuffer.append(']');
232: string = stringBuffer.toString();
233: }
234:
235: return string;
236: }
237:
238: /* (non-Javadoc)
239: * @see org.eclipse.ui.activities.ICategory#getDescription()
240: */
241: public String getDescription() throws NotDefinedException {
242: if (!defined) {
243: throw new NotDefinedException();
244: }
245:
246: return description;
247: }
248:
249: public boolean setDescription(String description) {
250: if (!Util.equals(description, this .description)) {
251: this .description = description;
252: hashCode = HASH_INITIAL;
253: string = null;
254: return true;
255: }
256:
257: return false;
258: }
259: }
|