1 11 12 package org.eclipse.core.commands.contexts; 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.core.internal.commands.util.Util; 21 22 50 public final class Context extends NamedHandleObject implements Comparable { 51 52 56 private Set listeners = null; 57 58 63 private String parentId = null; 64 65 71 Context(final String id) { 72 super(id); 73 } 74 75 84 public final void addContextListener(final IContextListener listener) { 85 if (listener == null) { 86 throw new NullPointerException (); 87 } 88 89 if (listeners == null) { 90 listeners = new HashSet (); 91 } 92 93 listeners.add(listener); 94 } 95 96 99 public final int compareTo(final Object object) { 100 final Context scheme = (Context) object; 101 int compareTo = Util.compare(this.id, scheme.id); 102 if (compareTo == 0) { 103 compareTo = Util.compare(this.name, scheme.name); 104 if (compareTo == 0) { 105 compareTo = Util.compare(this.parentId, scheme.parentId); 106 if (compareTo == 0) { 107 compareTo = Util.compare(this.description, 108 scheme.description); 109 if (compareTo == 0) { 110 compareTo = Util.compare(this.defined, scheme.defined); 111 } 112 } 113 } 114 } 115 116 return compareTo; 117 } 118 119 137 public final void define(final String name, final String description, 138 final String parentId) { 139 if (name == null) { 140 throw new NullPointerException ( 141 "The name of a scheme cannot be null"); } 143 144 final boolean definedChanged = !this.defined; 145 this.defined = true; 146 147 final boolean nameChanged = !Util.equals(this.name, name); 148 this.name = name; 149 150 final boolean descriptionChanged = !Util.equals(this.description, 151 description); 152 this.description = description; 153 154 final boolean parentIdChanged = !Util.equals(this.parentId, parentId); 155 this.parentId = parentId; 156 157 fireContextChanged(new ContextEvent(this, definedChanged, nameChanged, 158 descriptionChanged, parentIdChanged)); 159 } 160 161 169 private final void fireContextChanged(final ContextEvent event) { 170 if (event == null) { 171 throw new NullPointerException ( 172 "Cannot send a null event to listeners."); } 174 175 if (listeners == null) { 176 return; 177 } 178 179 final Iterator listenerItr = listeners.iterator(); 180 while (listenerItr.hasNext()) { 181 final IContextListener listener = (IContextListener) listenerItr 182 .next(); 183 listener.contextChanged(event); 184 } 185 } 186 187 199 public final String getParentId() throws NotDefinedException { 200 if (!defined) { 201 throw new NotDefinedException( 202 "Cannot get the parent identifier from an undefined context. " +id); 204 } 205 206 return parentId; 207 } 208 209 219 public final void removeContextListener( 220 final IContextListener contextListener) { 221 if (contextListener == null) { 222 throw new NullPointerException ("Cannot remove a null listener."); } 224 225 if (listeners == null) { 226 return; 227 } 228 229 listeners.remove(contextListener); 230 231 if (listeners.isEmpty()) { 232 listeners = null; 233 } 234 } 235 236 242 public final String toString() { 243 if (string == null) { 244 final StringBuffer stringBuffer = new StringBuffer (); 245 stringBuffer.append("Context("); stringBuffer.append(id); 247 stringBuffer.append(','); 248 stringBuffer.append(name); 249 stringBuffer.append(','); 250 stringBuffer.append(description); 251 stringBuffer.append(','); 252 stringBuffer.append(parentId); 253 stringBuffer.append(','); 254 stringBuffer.append(defined); 255 stringBuffer.append(')'); 256 string = stringBuffer.toString(); 257 } 258 return string; 259 } 260 261 266 public final void undefine() { 267 string = null; 268 269 final boolean definedChanged = defined; 270 defined = false; 271 272 final boolean nameChanged = name != null; 273 name = null; 274 275 final boolean descriptionChanged = description != null; 276 description = null; 277 278 final boolean parentIdChanged = parentId != null; 279 parentId = null; 280 281 fireContextChanged(new ContextEvent(this, definedChanged, nameChanged, 282 descriptionChanged, parentIdChanged)); 283 } 284 } 285 | Popular Tags |