1 11 12 package org.eclipse.jface.bindings; 13 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 import java.util.Set ; 17 18 import org.eclipse.core.commands.common.NamedHandleObject; 19 import org.eclipse.core.commands.common.NotDefinedException; 20 import org.eclipse.jface.util.Util; 21 22 49 public final class Scheme extends NamedHandleObject implements Comparable { 50 51 55 private Set listeners = null; 56 57 62 private String parentId = null; 63 64 70 Scheme(final String id) { 71 super(id); 72 } 73 74 85 public final void addSchemeListener(final ISchemeListener schemeListener) { 86 if (schemeListener == null) { 87 throw new NullPointerException ("Can't add a null scheme listener."); } 89 90 if (listeners == null) { 91 listeners = new HashSet (); 92 } 93 94 listeners.add(schemeListener); 95 } 96 97 102 public final int compareTo(final Object object) { 103 final Scheme scheme = (Scheme) object; 104 int compareTo = Util.compare(this.id, scheme.id); 105 if (compareTo == 0) { 106 compareTo = Util.compare(this.name, scheme.name); 107 if (compareTo == 0) { 108 compareTo = Util.compare(this.parentId, scheme.parentId); 109 if (compareTo == 0) { 110 compareTo = Util.compare(this.description, 111 scheme.description); 112 if (compareTo == 0) { 113 compareTo = Util.compare(this.defined, scheme.defined); 114 } 115 } 116 } 117 } 118 119 return compareTo; 120 } 121 122 140 public final void define(final String name, final String description, 141 final String parentId) { 142 if (name == null) { 143 throw new NullPointerException ( 144 "The name of a scheme cannot be null"); } 146 147 final boolean definedChanged = !this.defined; 148 this.defined = true; 149 150 final boolean nameChanged = !Util.equals(this.name, name); 151 this.name = name; 152 153 final boolean descriptionChanged = !Util.equals(this.description, 154 description); 155 this.description = description; 156 157 final boolean parentIdChanged = !Util.equals(this.parentId, parentId); 158 this.parentId = parentId; 159 160 fireSchemeChanged(new SchemeEvent(this, definedChanged, nameChanged, 161 descriptionChanged, parentIdChanged)); 162 } 163 164 172 private final void fireSchemeChanged(final SchemeEvent event) { 173 if (event == null) { 174 throw new NullPointerException ( 175 "Cannot send a null event to listeners."); } 177 178 if (listeners == null) { 179 return; 180 } 181 182 final Iterator listenerItr = listeners.iterator(); 183 while (listenerItr.hasNext()) { 184 final ISchemeListener listener = (ISchemeListener) listenerItr 185 .next(); 186 listener.schemeChanged(event); 187 } 188 } 189 190 205 public final String getParentId() throws NotDefinedException { 206 if (!defined) { 207 throw new NotDefinedException( 208 "Cannot get the parent identifier from an undefined scheme. " + id); 210 } 211 212 return parentId; 213 } 214 215 226 public final void removeSchemeListener(final ISchemeListener schemeListener) { 227 if (schemeListener == null) { 228 throw new NullPointerException ("Cannot remove a null listener."); } 230 231 if (listeners == null) { 232 return; 233 } 234 235 listeners.remove(schemeListener); 236 237 if (listeners.isEmpty()) { 238 listeners = null; 239 } 240 } 241 242 248 public final String toString() { 249 if (string == null) { 250 final StringBuffer stringBuffer = new StringBuffer (); 251 stringBuffer.append("Scheme("); stringBuffer.append(id); 253 stringBuffer.append(','); 254 stringBuffer.append(name); 255 stringBuffer.append(','); 256 stringBuffer.append(description); 257 stringBuffer.append(','); 258 stringBuffer.append(parentId); 259 stringBuffer.append(','); 260 stringBuffer.append(defined); 261 stringBuffer.append(')'); 262 string = stringBuffer.toString(); 263 } 264 return string; 265 } 266 267 272 public final void undefine() { 273 string = null; 274 275 final boolean definedChanged = defined; 276 defined = false; 277 278 final boolean nameChanged = name != null; 279 name = null; 280 281 final boolean descriptionChanged = description != null; 282 description = null; 283 284 final boolean parentIdChanged = parentId != null; 285 parentId = null; 286 287 fireSchemeChanged(new SchemeEvent(this, definedChanged, nameChanged, 288 descriptionChanged, parentIdChanged)); 289 } 290 } 291 | Popular Tags |