1 11 12 package org.eclipse.ui.internal.commands; 13 14 import java.util.ArrayList ; 15 import java.util.List ; 16 import java.util.Set ; 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 categoriesWithListeners; 30 private List categoryListeners; 31 private boolean defined; 32 private String description; 33 34 private transient int hashCode; 35 private transient boolean hashCodeComputed; 36 private String id; 37 private String name; 38 private transient String string; 39 40 Category(Set categoriesWithListeners, String id) { 41 if (categoriesWithListeners == null || id == null) 42 throw new NullPointerException (); 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 (); 51 52 if (categoryListeners == null) 53 categoryListeners = new ArrayList (); 54 55 if (!categoryListeners.contains(categoryListener)) 56 categoryListeners.add(categoryListener); 57 58 categoriesWithListeners.add(this); 59 } 60 61 public int compareTo(Object 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 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 (); 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 getDescription() throws NotDefinedException { 103 if (!defined) 104 throw new NotDefinedException( 105 "Cannot get the description from an undefined category."); 107 return description; 108 } 109 110 public String getId() { 111 return id; 112 } 113 114 public String getName() throws NotDefinedException { 115 if (!defined) 116 throw new NotDefinedException( 117 "Cannot get the name from an undefined category"); 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 (); 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 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 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 toString() { 187 if (string == null) { 188 final StringBuffer stringBuffer = new StringBuffer (); 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 |