KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > activities > Category


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal.activities;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.eclipse.ui.activities.CategoryEvent;
20 import org.eclipse.ui.activities.ICategory;
21 import org.eclipse.ui.activities.ICategoryActivityBinding;
22 import org.eclipse.ui.activities.ICategoryListener;
23 import org.eclipse.ui.activities.NotDefinedException;
24 import org.eclipse.ui.internal.util.Util;
25
26 final class Category implements ICategory {
27     private final static int HASH_FACTOR = 89;
28
29     private final static int HASH_INITIAL = Category.class.getName().hashCode();
30
31     private final static Set JavaDoc strongReferences = new HashSet JavaDoc();
32
33     private Set JavaDoc categoryActivityBindings;
34
35     private transient ICategoryActivityBinding[] categoryActivityBindingsAsArray;
36
37     private List JavaDoc categoryListeners;
38
39     private boolean defined;
40
41     private transient int hashCode = HASH_INITIAL;
42
43     private String JavaDoc id;
44
45     private String JavaDoc name;
46
47     private transient String JavaDoc string;
48
49     private String JavaDoc description;
50
51     Category(String JavaDoc id) {
52         if (id == null) {
53             throw new NullPointerException JavaDoc();
54         }
55
56         this.id = id;
57     }
58
59     public void addCategoryListener(ICategoryListener categoryListener) {
60         if (categoryListener == null) {
61             throw new NullPointerException JavaDoc();
62         }
63
64         if (categoryListeners == null) {
65             categoryListeners = new ArrayList JavaDoc();
66         }
67
68         if (!categoryListeners.contains(categoryListener)) {
69             categoryListeners.add(categoryListener);
70         }
71
72         strongReferences.add(this);
73     }
74
75     public int compareTo(Object JavaDoc object) {
76         Category castedObject = (Category) object;
77         int compareTo = Util.compare(
78                 categoryActivityBindingsAsArray,
79                 castedObject.categoryActivityBindingsAsArray);
80
81         if (compareTo == 0) {
82             compareTo = Util.compare(defined, castedObject.defined);
83
84             if (compareTo == 0) {
85                 compareTo = Util.compare(id, castedObject.id);
86
87                 if (compareTo == 0) {
88                     compareTo = Util.compare(name, castedObject.name);
89                 }
90             }
91         }
92
93         return compareTo;
94     }
95
96     public boolean equals(Object JavaDoc object) {
97         if (!(object instanceof Category)) {
98             return false;
99         }
100
101         final Category castedObject = (Category) object;
102         if (!Util.equals(categoryActivityBindings,
103                 castedObject.categoryActivityBindings)) {
104             return false;
105         }
106         
107         if (!Util.equals(defined, castedObject.defined)) {
108             return false;
109         }
110         
111         if (!Util.equals(id, castedObject.id)) {
112             return false;
113         }
114         
115         return Util.equals(name, castedObject.name);
116     }
117
118     void fireCategoryChanged(CategoryEvent categoryEvent) {
119         if (categoryEvent == null) {
120             throw new NullPointerException JavaDoc();
121         }
122
123         if (categoryListeners != null) {
124             for (int i = 0; i < categoryListeners.size(); i++) {
125                 ((ICategoryListener) categoryListeners.get(i))
126                         .categoryChanged(categoryEvent);
127             }
128         }
129     }
130
131     public Set JavaDoc getCategoryActivityBindings() {
132         return categoryActivityBindings;
133     }
134
135     public String JavaDoc getId() {
136         return id;
137     }
138
139     public String JavaDoc getName() throws NotDefinedException {
140         if (!defined) {
141             throw new NotDefinedException();
142         }
143
144         return name;
145     }
146
147     public int hashCode() {
148         if (hashCode == HASH_INITIAL) {
149             hashCode = hashCode * HASH_FACTOR
150                     + Util.hashCode(categoryActivityBindings);
151             hashCode = hashCode * HASH_FACTOR + Util.hashCode(defined);
152             hashCode = hashCode * HASH_FACTOR + Util.hashCode(id);
153             hashCode = hashCode * HASH_FACTOR + Util.hashCode(name);
154             if (hashCode == HASH_INITIAL) {
155                 hashCode++;
156             }
157         }
158
159         return hashCode;
160     }
161
162     public boolean isDefined() {
163         return defined;
164     }
165
166     public void removeCategoryListener(ICategoryListener categoryListener) {
167         if (categoryListener == null) {
168             throw new NullPointerException JavaDoc();
169         }
170
171         if (categoryListeners != null) {
172             categoryListeners.remove(categoryListener);
173         }
174
175         if (categoryListeners.isEmpty()) {
176             strongReferences.remove(this);
177         }
178     }
179
180     boolean setCategoryActivityBindings(Set JavaDoc categoryActivityBindings) {
181         categoryActivityBindings = Util.safeCopy(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 JavaDoc 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 JavaDoc toString() {
221         if (string == null) {
222             final StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
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 JavaDoc getDescription() throws NotDefinedException {
242         if (!defined) {
243             throw new NotDefinedException();
244         }
245
246         return description;
247     }
248
249     public boolean setDescription(String JavaDoc 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 }
260
Popular Tags