KickJava   Java API By Example, From Geeks To Geeks.

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


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.IKeyConfiguration;
19 import org.eclipse.ui.commands.IKeyConfigurationListener;
20 import org.eclipse.ui.commands.KeyConfigurationEvent;
21 import org.eclipse.ui.commands.NotDefinedException;
22 import org.eclipse.ui.internal.util.Util;
23
24 final class KeyConfiguration implements IKeyConfiguration {
25
26     private final static int HASH_FACTOR = 89;
27     private final static int HASH_INITIAL =
28         KeyConfiguration.class.getName().hashCode();
29
30     private boolean active;
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 List JavaDoc keyConfigurationListeners;
38     private Set JavaDoc keyConfigurationsWithListeners;
39     private String JavaDoc name;
40     private String JavaDoc parentId;
41     private transient String JavaDoc string;
42
43     KeyConfiguration(Set JavaDoc keyConfigurationsWithListeners, String JavaDoc id) {
44         if (keyConfigurationsWithListeners == null || id == null)
45             throw new NullPointerException JavaDoc();
46
47         this.keyConfigurationsWithListeners = keyConfigurationsWithListeners;
48         this.id = id;
49     }
50
51     public void addKeyConfigurationListener(IKeyConfigurationListener keyConfigurationListener) {
52         if (keyConfigurationListener == null)
53             throw new NullPointerException JavaDoc();
54
55         if (keyConfigurationListeners == null)
56             keyConfigurationListeners = new ArrayList JavaDoc();
57
58         if (!keyConfigurationListeners.contains(keyConfigurationListener))
59             keyConfigurationListeners.add(keyConfigurationListener);
60
61         keyConfigurationsWithListeners.add(this);
62     }
63
64     public int compareTo(Object JavaDoc object) {
65         KeyConfiguration castedObject = (KeyConfiguration) object;
66         int compareTo = Util.compare(active, castedObject.active);
67
68         if (compareTo == 0) {
69             compareTo = Util.compare(defined, castedObject.defined);
70
71             if (compareTo == 0) {
72                 compareTo = Util.compare(description, castedObject.description);
73
74                 if (compareTo == 0) {
75                     compareTo = Util.compare(id, castedObject.id);
76
77                     if (compareTo == 0) {
78                         compareTo = Util.compare(name, castedObject.name);
79
80                         if (compareTo == 0)
81                             compareTo =
82                                 Util.compare(parentId, castedObject.parentId);
83                     }
84                 }
85             }
86         }
87
88         return compareTo;
89     }
90
91     public boolean equals(Object JavaDoc object) {
92         if (!(object instanceof KeyConfiguration))
93             return false;
94
95         KeyConfiguration castedObject = (KeyConfiguration) object;
96         boolean equals = true;
97         equals &= Util.equals(active, castedObject.active);
98         equals &= Util.equals(defined, castedObject.defined);
99         equals &= Util.equals(description, castedObject.description);
100         equals &= Util.equals(id, castedObject.id);
101         equals &= Util.equals(name, castedObject.name);
102         equals &= Util.equals(parentId, castedObject.parentId);
103         return equals;
104     }
105
106     void fireKeyConfigurationChanged(KeyConfigurationEvent keyConfigurationEvent) {
107         if (keyConfigurationEvent == null)
108             throw new NullPointerException JavaDoc();
109
110         if (keyConfigurationListeners != null)
111             for (int i = 0; i < keyConfigurationListeners.size(); i++)
112                 (
113                     (IKeyConfigurationListener) keyConfigurationListeners.get(
114                         i)).keyConfigurationChanged(
115                     keyConfigurationEvent);
116     }
117
118     public String JavaDoc getDescription() throws NotDefinedException {
119         if (!defined)
120             throw new NotDefinedException(
121                         "Cannot get the description from an undefined key configuration."); //$NON-NLS-1$
122

123         return description;
124     }
125
126     public String JavaDoc getId() {
127         return id;
128     }
129
130     public String JavaDoc getName() throws NotDefinedException {
131         if (!defined)
132             throw new NotDefinedException(
133                         "Cannot get name from an undefined key configuration."); //$NON-NLS-1$
134

135         return name;
136     }
137
138     public String JavaDoc getParentId() throws NotDefinedException {
139         if (!defined)
140             throw new NotDefinedException(
141                         "Cannot get the parent id from an undefined key configuration."); //$NON-NLS-1$
142

143         return parentId;
144     }
145
146     public int hashCode() {
147         if (!hashCodeComputed) {
148             hashCode = HASH_INITIAL;
149             hashCode = hashCode * HASH_FACTOR + Util.hashCode(active);
150             hashCode = hashCode * HASH_FACTOR + Util.hashCode(defined);
151             hashCode = hashCode * HASH_FACTOR + Util.hashCode(description);
152             hashCode = hashCode * HASH_FACTOR + Util.hashCode(id);
153             hashCode = hashCode * HASH_FACTOR + Util.hashCode(name);
154             hashCode = hashCode * HASH_FACTOR + Util.hashCode(parentId);
155             hashCodeComputed = true;
156         }
157
158         return hashCode;
159     }
160
161     public boolean isActive() {
162         return active;
163     }
164
165     public boolean isDefined() {
166         return defined;
167     }
168
169     public void removeKeyConfigurationListener(IKeyConfigurationListener keyConfigurationListener) {
170         if (keyConfigurationListener == null)
171             throw new NullPointerException JavaDoc();
172
173         if (keyConfigurationListeners != null)
174             keyConfigurationListeners.remove(keyConfigurationListener);
175
176         if (keyConfigurationListeners.isEmpty())
177             keyConfigurationsWithListeners.remove(this);
178     }
179
180     boolean setActive(boolean active) {
181         if (active != this.active) {
182             this.active = active;
183             hashCodeComputed = false;
184             hashCode = 0;
185             string = null;
186             return true;
187         }
188
189         return false;
190     }
191
192     boolean setDefined(boolean defined) {
193         if (defined != this.defined) {
194             this.defined = defined;
195             hashCodeComputed = false;
196             hashCode = 0;
197             string = null;
198             return true;
199         }
200
201         return false;
202     }
203
204     boolean setDescription(String JavaDoc description) {
205         if (!Util.equals(description, this.description)) {
206             this.description = description;
207             hashCodeComputed = false;
208             hashCode = 0;
209             string = null;
210             return true;
211         }
212
213         return false;
214     }
215
216     boolean setName(String JavaDoc name) {
217         if (!Util.equals(name, this.name)) {
218             this.name = name;
219             hashCodeComputed = false;
220             hashCode = 0;
221             string = null;
222             return true;
223         }
224
225         return false;
226     }
227
228     boolean setParentId(String JavaDoc parentId) {
229         if (!Util.equals(parentId, this.parentId)) {
230             this.parentId = parentId;
231             hashCodeComputed = false;
232             hashCode = 0;
233             string = null;
234             return true;
235         }
236
237         return false;
238     }
239
240     public String JavaDoc toString() {
241         if (string == null) {
242             final StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
243             stringBuffer.append('[');
244             stringBuffer.append(active);
245             stringBuffer.append(',');
246             stringBuffer.append(defined);
247             stringBuffer.append(',');
248             stringBuffer.append(description);
249             stringBuffer.append(',');
250             stringBuffer.append(id);
251             stringBuffer.append(',');
252             stringBuffer.append(name);
253             stringBuffer.append(',');
254             stringBuffer.append(parentId);
255             stringBuffer.append(']');
256             string = stringBuffer.toString();
257         }
258
259         return string;
260     }
261 }
262
Popular Tags