KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > contexts > Context


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

122         return name;
123     }
124
125     public String JavaDoc getParentId() throws NotDefinedException {
126         if (!defined)
127                 throw new NotDefinedException(
128                         "Cannot get the parent identifier from an undefined context."); //$NON-NLS-1$
129

130         return parentId;
131     }
132
133     public int hashCode() {
134         if (!hashCodeComputed) {
135             hashCode = HASH_INITIAL;
136             hashCode = hashCode * HASH_FACTOR + Util.hashCode(defined);
137             hashCode = hashCode * HASH_FACTOR + Util.hashCode(enabled);
138             hashCode = hashCode * HASH_FACTOR + Util.hashCode(id);
139             hashCode = hashCode * HASH_FACTOR + Util.hashCode(name);
140             hashCode = hashCode * HASH_FACTOR + Util.hashCode(parentId);
141             hashCodeComputed = true;
142         }
143
144         return hashCode;
145     }
146
147     public boolean isDefined() {
148         return defined;
149     }
150
151     public boolean isEnabled() {
152         return enabled;
153     }
154
155     public void removeContextListener(IContextListener contextListener) {
156         if (contextListener == null) throw new NullPointerException JavaDoc();
157
158         if (contextListeners != null) contextListeners.remove(contextListener);
159
160         if (contextListeners.isEmpty()) strongReferences.remove(this);
161     }
162
163     boolean setDefined(boolean defined) {
164         if (defined != this.defined) {
165             this.defined = defined;
166             hashCodeComputed = false;
167             hashCode = 0;
168             string = null;
169             return true;
170         }
171
172         return false;
173     }
174
175     boolean setEnabled(boolean enabled) {
176         if (enabled != this.enabled) {
177             this.enabled = enabled;
178             hashCodeComputed = false;
179             hashCode = 0;
180             string = null;
181             return true;
182         }
183
184         return false;
185     }
186
187     boolean setName(String JavaDoc name) {
188         if (!Util.equals(name, this.name)) {
189             this.name = name;
190             hashCodeComputed = false;
191             hashCode = 0;
192             string = null;
193             return true;
194         }
195
196         return false;
197     }
198
199     boolean setParentId(String JavaDoc parentId) {
200         if (!Util.equals(parentId, this.parentId)) {
201             this.parentId = parentId;
202             hashCodeComputed = false;
203             hashCode = 0;
204             string = null;
205             return true;
206         }
207
208         return false;
209     }
210
211     public String JavaDoc toString() {
212         if (string == null) {
213             final StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
214             stringBuffer.append('[');
215             stringBuffer.append(defined);
216             stringBuffer.append(',');
217             stringBuffer.append(enabled);
218             stringBuffer.append(',');
219             stringBuffer.append(id);
220             stringBuffer.append(',');
221             stringBuffer.append(name);
222             stringBuffer.append(',');
223             stringBuffer.append(parentId);
224             stringBuffer.append(']');
225             string = stringBuffer.toString();
226         }
227
228         return string;
229     }
230 }
231
Popular Tags