1 11 12 package org.eclipse.ui.internal.activities; 13 14 import java.util.ArrayList ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Set ; 19 20 import org.eclipse.ui.activities.ActivityEvent; 21 import org.eclipse.ui.activities.IActivity; 22 import org.eclipse.ui.activities.IActivityListener; 23 import org.eclipse.ui.activities.IActivityPatternBinding; 24 import org.eclipse.ui.activities.IActivityRequirementBinding; 25 import org.eclipse.ui.activities.NotDefinedException; 26 import org.eclipse.ui.internal.util.Util; 27 28 final class Activity implements IActivity { 29 private final static int HASH_FACTOR = 89; 30 31 private final static int HASH_INITIAL = Activity.class.getName().hashCode(); 32 33 private final static Set strongReferences = new HashSet (); 34 35 private Set activityRequirementBindings; 36 37 private transient IActivityRequirementBinding[] activityRequirementBindingsAsArray; 38 39 private List activityListeners; 40 41 private Set activityPatternBindings; 42 43 private transient IActivityPatternBinding[] activityPatternBindingsAsArray; 44 45 private boolean defined; 46 47 private boolean enabled; 48 49 private transient int hashCode = HASH_INITIAL; 50 51 private String id; 52 53 private String name; 54 55 private transient String string; 56 57 private String description; 58 59 private boolean defaultEnabled; 60 61 Activity(String id) { 62 if (id == null) { 63 throw new NullPointerException (); 64 } 65 66 this.id = id; 67 } 68 69 public void addActivityListener(IActivityListener activityListener) { 70 if (activityListener == null) { 71 throw new NullPointerException (); 72 } 73 74 if (activityListeners == null) { 75 activityListeners = new ArrayList (); 76 } 77 78 if (!activityListeners.contains(activityListener)) { 79 activityListeners.add(activityListener); 80 } 81 82 strongReferences.add(this); 83 } 84 85 public int compareTo(Object object) { 86 Activity castedObject = (Activity) object; 87 88 int compareTo = Util.compare( 89 activityRequirementBindingsAsArray, 90 castedObject.activityRequirementBindingsAsArray); 91 92 if (compareTo == 0) { 93 compareTo = Util.compare( 94 activityPatternBindingsAsArray, 95 castedObject.activityPatternBindingsAsArray); 96 97 if (compareTo == 0) { 98 compareTo = Util.compare(defined, castedObject.defined); 99 100 if (compareTo == 0) { 101 compareTo = Util.compare(enabled, castedObject.enabled); 102 103 if (compareTo == 0) { 104 compareTo = Util.compare(id, castedObject.id); 105 106 if (compareTo == 0) { 107 compareTo = Util.compare(name, castedObject.name); 108 } 109 } 110 } 111 } 112 } 113 114 return compareTo; 115 } 116 117 public boolean equals(Object object) { 118 if (!(object instanceof Activity)) { 119 return false; 120 } 121 122 final Activity castedObject = (Activity) object; 123 124 if (!Util.equals(activityRequirementBindings, 125 castedObject.activityRequirementBindings)) { 126 return false; 127 } 128 129 if (!Util.equals(activityPatternBindings, 130 castedObject.activityPatternBindings)) { 131 return false; 132 } 133 134 if (!Util.equals(defined, castedObject.defined)) { 135 return false; 136 } 137 138 if (!Util.equals(enabled, castedObject.enabled)) { 139 return false; 140 } 141 142 if (!Util.equals(id, castedObject.id)) { 143 return false; 144 } 145 146 return Util.equals(name, castedObject.name); 147 } 148 149 void fireActivityChanged(ActivityEvent activityEvent) { 150 if (activityEvent == null) { 151 throw new NullPointerException (); 152 } 153 154 if (activityListeners != null) { 155 for (int i = 0; i < activityListeners.size(); i++) { 156 ((IActivityListener) activityListeners.get(i)) 157 .activityChanged(activityEvent); 158 } 159 } 160 } 161 162 public Set getActivityRequirementBindings() { 163 return activityRequirementBindings; 164 } 165 166 public Set getActivityPatternBindings() { 167 return activityPatternBindings; 168 } 169 170 public String getId() { 171 return id; 172 } 173 174 public String getName() throws NotDefinedException { 175 if (!defined) { 176 throw new NotDefinedException(); 177 } 178 179 return name; 180 } 181 182 public int hashCode() { 183 if (hashCode == HASH_INITIAL) { 184 hashCode = hashCode * HASH_FACTOR 185 + Util.hashCode(activityRequirementBindings); 186 hashCode = hashCode * HASH_FACTOR 187 + Util.hashCode(activityPatternBindings); 188 hashCode = hashCode * HASH_FACTOR + Util.hashCode(defined); 189 hashCode = hashCode * HASH_FACTOR + Util.hashCode(enabled); 190 hashCode = hashCode * HASH_FACTOR + Util.hashCode(id); 191 hashCode = hashCode * HASH_FACTOR + Util.hashCode(name); 192 if (hashCode == HASH_INITIAL) { 193 hashCode++; 194 } 195 } 196 197 return hashCode; 198 } 199 200 public boolean isDefined() { 201 return defined; 202 } 203 204 public boolean isEnabled() { 205 return enabled; 206 } 207 208 public boolean isMatch(String string) { 209 if (isDefined()) { 210 for (Iterator iterator = activityPatternBindings.iterator(); iterator 211 .hasNext();) { 212 ActivityPatternBinding activityPatternBinding = (ActivityPatternBinding) iterator 213 .next(); 214 215 if (activityPatternBinding.isMatch(string)) { 216 return true; 217 } 218 } 219 } 220 221 return false; 222 } 223 224 public void removeActivityListener(IActivityListener activityListener) { 225 if (activityListener == null) { 226 throw new NullPointerException (); 227 } 228 229 if (activityListeners != null) { 230 activityListeners.remove(activityListener); 231 } 232 233 if (activityListeners.isEmpty()) { 234 strongReferences.remove(this); 235 } 236 } 237 238 boolean setActivityRequirementBindings(Set activityRequirementBindings) { 239 activityRequirementBindings = Util.safeCopy( 240 activityRequirementBindings, IActivityRequirementBinding.class); 241 242 if (!Util.equals(activityRequirementBindings, 243 this.activityRequirementBindings)) { 244 this.activityRequirementBindings = activityRequirementBindings; 245 this.activityRequirementBindingsAsArray = (IActivityRequirementBinding[]) this.activityRequirementBindings 246 .toArray(new IActivityRequirementBinding[this.activityRequirementBindings 247 .size()]); 248 hashCode = HASH_INITIAL; 249 string = null; 250 return true; 251 } 252 253 return false; 254 } 255 256 boolean setActivityPatternBindings(Set activityPatternBindings) { 257 activityPatternBindings = Util.safeCopy(activityPatternBindings, 258 IActivityPatternBinding.class); 259 260 if (!Util.equals(activityPatternBindings, this.activityPatternBindings)) { 261 this.activityPatternBindings = activityPatternBindings; 262 this.activityPatternBindingsAsArray = (IActivityPatternBinding[]) this.activityPatternBindings 263 .toArray(new IActivityPatternBinding[this.activityPatternBindings 264 .size()]); 265 hashCode = HASH_INITIAL; 266 string = null; 267 return true; 268 } 269 270 return false; 271 } 272 273 boolean setDefined(boolean defined) { 274 if (defined != this.defined) { 275 this.defined = defined; 276 hashCode = HASH_INITIAL; 277 string = null; 278 return true; 279 } 280 281 return false; 282 } 283 284 boolean setEnabled(boolean enabled) { 285 if (enabled != this.enabled) { 286 this.enabled = enabled; 287 hashCode = HASH_INITIAL; 288 string = null; 289 return true; 290 } 291 292 return false; 293 } 294 295 boolean setName(String name) { 296 if (!Util.equals(name, this.name)) { 297 this.name = name; 298 hashCode = HASH_INITIAL; 299 string = null; 300 return true; 301 } 302 303 return false; 304 } 305 306 boolean setDescription(String description) { 307 if (!Util.equals(description, this.description)) { 308 this.description = description; 309 hashCode = HASH_INITIAL; 310 string = null; 311 return true; 312 } 313 314 return false; 315 } 316 317 public String toString() { 318 if (string == null) { 319 final StringBuffer stringBuffer = new StringBuffer (); 320 stringBuffer.append('['); 321 stringBuffer.append(activityRequirementBindings); 322 stringBuffer.append(','); 323 stringBuffer.append(activityPatternBindings); 324 stringBuffer.append(','); 325 stringBuffer.append(defined); 326 stringBuffer.append(','); 327 stringBuffer.append(enabled); 328 stringBuffer.append(','); 329 stringBuffer.append(id); 330 stringBuffer.append(','); 331 stringBuffer.append(name); 332 stringBuffer.append(']'); 333 string = stringBuffer.toString(); 334 } 335 336 return string; 337 } 338 339 342 public String getDescription() throws NotDefinedException { 343 if (!defined) { 344 throw new NotDefinedException(); 345 } 346 347 return description; 348 } 349 350 353 public boolean isDefaultEnabled() { 354 return defaultEnabled; 355 } 356 357 boolean setDefaultEnabled(boolean defaultEnabled) { 358 if (!Util.equals(defaultEnabled, this.defaultEnabled)) { 359 this.defaultEnabled = defaultEnabled; 360 hashCode = HASH_INITIAL; 361 string = null; 362 return true; 363 } 364 365 return false; 366 } 367 } 368 | Popular Tags |