KickJava   Java API By Example, From Geeks To Geeks.

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


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

11
12 package org.eclipse.ui.internal.commands;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.eclipse.ui.commands.CategoryEvent;
19 import org.eclipse.ui.commands.ICategory;
20 import org.eclipse.ui.commands.ICategoryListener;
21 import org.eclipse.ui.commands.NotDefinedException;
22 import org.eclipse.ui.internal.util.Util;
23
24 final class Category implements ICategory {
25
26     private final static int HASH_FACTOR = 89;
27     private final static int HASH_INITIAL = Category.class.getName().hashCode();
28
29     private Set JavaDoc categoriesWithListeners;
30     private List JavaDoc categoryListeners;
31     private boolean defined;
32     private String JavaDoc description;
33
34     private transient int hashCode;
35     private transient boolean hashCodeComputed;
36     private String JavaDoc id;
37     private String JavaDoc name;
38     private transient String JavaDoc string;
39
40     Category(Set JavaDoc categoriesWithListeners, String JavaDoc id) {
41         if (categoriesWithListeners == null || id == null)
42             throw new NullPointerException JavaDoc();
43
44         this.categoriesWithListeners = categoriesWithListeners;
45         this.id = id;
46     }
47
48     public void addCategoryListener(ICategoryListener categoryListener) {
49         if (categoryListener == null)
50             throw new NullPointerException JavaDoc();
51
52         if (categoryListeners == null)
53             categoryListeners = new ArrayList JavaDoc();
54
55         if (!categoryListeners.contains(categoryListener))
56             categoryListeners.add(categoryListener);
57
58         categoriesWithListeners.add(this);
59     }
60
61     public int compareTo(Object JavaDoc object) {
62         Category castedObject = (Category) object;
63         int compareTo = Util.compare(defined, castedObject.defined);
64
65         if (compareTo == 0) {
66             compareTo = Util.compare(description, castedObject.description);
67
68             if (compareTo == 0) {
69                 compareTo = Util.compare(id, castedObject.id);
70
71                 if (compareTo == 0)
72                     compareTo = Util.compare(name, castedObject.name);
73             }
74         }
75
76         return compareTo;
77     }
78
79     public boolean equals(Object JavaDoc object) {
80         if (!(object instanceof Category))
81             return false;
82
83         Category castedObject = (Category) object;
84         boolean equals = true;
85         equals &= Util.equals(defined, castedObject.defined);
86         equals &= Util.equals(description, castedObject.description);
87         equals &= Util.equals(id, castedObject.id);
88         equals &= Util.equals(name, castedObject.name);
89         return equals;
90     }
91
92     void fireCategoryChanged(CategoryEvent categoryEvent) {
93         if (categoryEvent == null)
94             throw new NullPointerException JavaDoc();
95
96         if (categoryListeners != null)
97             for (int i = 0; i < categoryListeners.size(); i++)
98                 ((ICategoryListener) categoryListeners.get(i)).categoryChanged(
99                     categoryEvent);
100     }
101
102     public String JavaDoc getDescription() throws NotDefinedException {
103         if (!defined)
104             throw new NotDefinedException(
105                         "Cannot get the description from an undefined category."); //$NON-NLS-1$
106

107         return description;
108     }
109
110     public String JavaDoc getId() {
111         return id;
112     }
113
114     public String JavaDoc getName() throws NotDefinedException {
115         if (!defined)
116             throw new NotDefinedException(
117                         "Cannot get the name from an undefined category"); //$NON-NLS-1$
118

119         return name;
120     }
121
122     public int hashCode() {
123         if (!hashCodeComputed) {
124             hashCode = HASH_INITIAL;
125             hashCode = hashCode * HASH_FACTOR + Util.hashCode(defined);
126             hashCode = hashCode * HASH_FACTOR + Util.hashCode(description);
127             hashCode = hashCode * HASH_FACTOR + Util.hashCode(id);
128             hashCode = hashCode * HASH_FACTOR + Util.hashCode(name);
129             hashCodeComputed = true;
130         }
131
132         return hashCode;
133     }
134
135     public boolean isDefined() {
136         return defined;
137     }
138
139     public void removeCategoryListener(ICategoryListener categoryListener) {
140         if (categoryListener == null)
141             throw new NullPointerException JavaDoc();
142
143         if (categoryListeners != null)
144             categoryListeners.remove(categoryListener);
145
146         if (categoryListeners.isEmpty())
147             categoriesWithListeners.remove(this);
148     }
149
150     boolean setDefined(boolean defined) {
151         if (defined != this.defined) {
152             this.defined = defined;
153             hashCodeComputed = false;
154             hashCode = 0;
155             string = null;
156             return true;
157         }
158
159         return false;
160     }
161
162     boolean setDescription(String JavaDoc description) {
163         if (!Util.equals(description, this.description)) {
164             this.description = description;
165             hashCodeComputed = false;
166             hashCode = 0;
167             string = null;
168             return true;
169         }
170
171         return false;
172     }
173
174     boolean setName(String JavaDoc name) {
175         if (!Util.equals(name, this.name)) {
176             this.name = name;
177             hashCodeComputed = false;
178             hashCode = 0;
179             string = null;
180             return true;
181         }
182
183         return false;
184     }
185
186     public String JavaDoc toString() {
187         if (string == null) {
188             final StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
189             stringBuffer.append('[');
190             stringBuffer.append(defined);
191             stringBuffer.append(',');
192             stringBuffer.append(description);
193             stringBuffer.append(',');
194             stringBuffer.append(id);
195             stringBuffer.append(',');
196             stringBuffer.append(name);
197             stringBuffer.append(']');
198             string = stringBuffer.toString();
199         }
200
201         return string;
202     }
203 }
204
Popular Tags