1 11 12 package org.eclipse.ui.internal.menus; 13 14 import org.eclipse.core.commands.common.NamedHandleObject; 15 import org.eclipse.core.commands.common.NotDefinedException; 16 import org.eclipse.jface.util.IPropertyChangeListener; 17 import org.eclipse.jface.util.PropertyChangeEvent; 18 import org.eclipse.jface.util.Util; 19 20 49 public final class SActionSet extends NamedHandleObject { 50 51 55 public static final String PROPERTY_DEFINED = "DEFINED"; 57 61 public static final String PROPERTY_DESCRIPTION = "DESCRIPTION"; 63 67 public static final String PROPERTY_NAME = "NAME"; 69 73 public static final String PROPERTY_REFERENCES = "REFERENCES"; 75 79 public static final String PROPERTY_VISIBLE = "VISIBLE"; 81 85 private SReference[] references; 86 87 90 private boolean visible = false; 91 92 99 public SActionSet(final String id) { 100 super(id); 101 } 102 103 110 public final void addListener(final IPropertyChangeListener listener) { 111 addListenerObject(listener); 112 } 113 114 136 public final void define(final String name, final String description, 137 final boolean visible, final SReference[] references) { 138 if (name == null) { 139 throw new NullPointerException ("An action set needs a name"); } 141 142 if (references == null) { 143 throw new NullPointerException ( 144 "The action set must reference at least one menu element"); } 146 147 if (references.length == 0) { 148 throw new IllegalArgumentException ( 149 "The action set must reference at least one menu element"); } 151 152 setDefined(true); 153 setName(name); 154 setDescription(description); 155 setVisible(visible); 156 setReferences(references); 157 } 158 159 165 protected final void firePropertyChangeEvent(final PropertyChangeEvent event) { 166 if (event == null) { 167 return; 168 } 169 170 final Object [] listeners = getListeners(); 171 for (int i = 0; i < listeners.length; i++) { 172 final IPropertyChangeListener listener = (IPropertyChangeListener) listeners[i]; 173 listener.propertyChange(event); 174 } 175 } 176 177 185 public final SReference[] getReferences() throws NotDefinedException { 186 if (!isDefined()) { 187 throw new NotDefinedException( 188 "Cannot get the references from an undefined action set"); } 190 191 final SReference[] result = new SReference[references.length]; 192 System.arraycopy(references, 0, result, 0, references.length); 193 return result; 194 } 195 196 204 public final boolean isVisible() throws NotDefinedException { 205 if (!isDefined()) { 206 throw new NotDefinedException( 207 "Cannot get the visibility from an undefined action set"); } 209 210 return visible; 211 } 212 213 219 public final void removeListener(final IPropertyChangeListener listener) { 220 addListenerObject(listener); 221 } 222 223 230 protected final void setDefined(final boolean defined) { 231 if (this.defined != defined) { 232 PropertyChangeEvent event = null; 233 if (isListenerAttached()) { 234 event = new PropertyChangeEvent(this, PROPERTY_DEFINED, 235 (this.defined ? Boolean.TRUE : Boolean.FALSE), 236 (defined ? Boolean.TRUE : Boolean.FALSE)); 237 } 238 this.defined = defined; 239 firePropertyChangeEvent(event); 240 } 241 } 242 243 251 protected final void setDescription(final String description) { 252 if (!Util.equals(this.description, description)) { 253 PropertyChangeEvent event = null; 254 if (isListenerAttached()) { 255 event = new PropertyChangeEvent(this, PROPERTY_DESCRIPTION, 256 this.description, description); 257 } 258 this.description = description; 259 firePropertyChangeEvent(event); 260 } 261 } 262 263 270 protected final void setName(final String name) { 271 if (!Util.equals(this.name, name)) { 272 PropertyChangeEvent event = null; 273 if (isListenerAttached()) { 274 event = new PropertyChangeEvent(this, PROPERTY_NAME, this.name, 275 name); 276 } 277 this.name = name; 278 firePropertyChangeEvent(event); 279 } 280 } 281 282 289 protected final void setReferences(final SReference[] references) { 290 if (!Util.equals(this.references, references)) { 291 PropertyChangeEvent event = null; 292 if (isListenerAttached()) { 293 event = new PropertyChangeEvent(this, PROPERTY_REFERENCES, 294 this.references, references); 295 } 296 this.references = references; 297 firePropertyChangeEvent(event); 298 } 299 } 300 301 308 protected final void setVisible(final boolean visible) { 309 if (this.visible != visible) { 310 PropertyChangeEvent event = null; 311 if (isListenerAttached()) { 312 event = new PropertyChangeEvent(this, PROPERTY_VISIBLE, 313 (this.visible ? Boolean.TRUE : Boolean.FALSE), 314 (visible ? Boolean.TRUE : Boolean.FALSE)); 315 } 316 this.visible = visible; 317 firePropertyChangeEvent(event); 318 } 319 } 320 321 327 public final String toString() { 328 if (string == null) { 329 final StringBuffer stringBuffer = new StringBuffer (); 330 stringBuffer.append("SGroup("); stringBuffer.append(id); 332 stringBuffer.append(','); 333 stringBuffer.append(name); 334 stringBuffer.append(','); 335 stringBuffer.append(description); 336 stringBuffer.append(','); 337 stringBuffer.append(visible); 338 stringBuffer.append(','); 339 stringBuffer.append(references); 340 stringBuffer.append(','); 341 stringBuffer.append(defined); 342 stringBuffer.append(')'); 343 string = stringBuffer.toString(); 344 } 345 return string; 346 } 347 348 353 public final void undefine() { 354 string = null; 355 356 setReferences(references); 357 setVisible(visible); 358 setDescription(description); 359 setName(name); 360 setDefined(false); 361 } 362 } 363 | Popular Tags |