1 11 12 package org.eclipse.ui.internal.menus; 13 14 import java.util.Map ; 15 import java.util.WeakHashMap ; 16 17 import org.eclipse.core.commands.common.HandleObject; 18 import org.eclipse.core.commands.common.NotDefinedException; 19 import org.eclipse.jface.util.IPropertyChangeListener; 20 import org.eclipse.jface.util.PropertyChangeEvent; 21 import org.eclipse.jface.util.Util; 22 import org.eclipse.jface.window.Window; 23 24 58 public abstract class MenuElement extends HandleObject { 59 60 64 private static final SLocation[] NO_LOCATIONS = new SLocation[0]; 65 66 70 public static final String PROPERTY_DEFINED = "DEFINED"; 72 76 public static final String PROPERTY_LOCATIONS = "LOCATIONS"; 78 82 public static final String PROPERTY_VISIBILITY = "VISIBILITY"; 84 88 protected SLocation[] locations; 89 90 96 private Map windowToShowingMap = null; 97 98 104 private Map windowToVisibleMap = null; 105 106 113 public MenuElement(final String id) { 114 super(id); 115 } 116 117 124 public final void addListener(final IPropertyChangeListener listener) { 125 addListenerObject(listener); 126 } 127 128 135 public boolean containsLocation(final SLocation location) { 136 if (locations!=null && location!=null) { 137 for (int i = 0; i < locations.length; i++) { 138 if (location.equals(locations[i])) { 139 return true; 140 } 141 } 142 } 143 return false; 144 } 145 146 153 public final void addLocation(final SLocation location) { 154 if (location == null) { 155 throw new NullPointerException ("The location cannot be null"); } 157 158 if (containsLocation(location)) { 159 return; 160 } 161 162 final SLocation[] newLocations; 163 if ((locations == null) || (locations.length == 0)) { 164 newLocations = new SLocation[] { location }; 165 } else { 166 newLocations = new SLocation[locations.length + 1]; 167 System.arraycopy(locations, 0, newLocations, 0, locations.length); 168 newLocations[locations.length] = location; 169 } 170 setLocations(newLocations); 171 } 172 173 179 protected final void firePropertyChangeEvent(final PropertyChangeEvent event) { 180 if (event == null) { 181 return; 182 } 183 184 final Object [] listeners = getListeners(); 185 for (int i = 0; i < listeners.length; i++) { 186 final IPropertyChangeListener listener = (IPropertyChangeListener) listeners[i]; 187 listener.propertyChange(event); 188 } 189 } 190 191 197 protected final void fireVisibleChanged(final boolean visible) { 198 if (isListenerAttached()) { 199 final PropertyChangeEvent event; 200 if (visible) { 201 event = new PropertyChangeEvent(this, PROPERTY_VISIBILITY, 202 Boolean.FALSE, Boolean.TRUE); 203 } else { 204 event = new PropertyChangeEvent(this, PROPERTY_VISIBILITY, 205 Boolean.TRUE, Boolean.FALSE); 206 } 207 firePropertyChangeEvent(event); 208 } 209 } 210 211 219 public final SLocation[] getLocations() throws NotDefinedException { 220 if (!isDefined()) { 221 throw new NotDefinedException( 222 "Cannot get the locations from an undefined menu element"); } 224 225 if (locations == null) { 226 return NO_LOCATIONS; 227 } 228 229 final SLocation[] result = new SLocation[locations.length]; 230 System.arraycopy(locations, 0, result, 0, locations.length); 231 return result; 232 } 233 234 245 public final boolean isShowing(final Window window) { 246 if (windowToShowingMap != null) { 247 Object value = windowToShowingMap.get(window); 248 if (value == null) { 249 value = windowToShowingMap.get(null); 250 } 251 if (value == Boolean.TRUE) { 252 return true; 253 } 254 } 255 256 return false; 257 } 258 259 269 public final boolean isVisible(final Window window) { 270 if (windowToVisibleMap != null) { 271 Object value = windowToVisibleMap.get(window); 272 if (value == null) { 273 value = windowToVisibleMap.get(null); 274 } 275 if (value == Boolean.FALSE) { 276 return false; 277 } 278 } 279 280 return true; 281 } 282 283 289 public final void removeListener(final IPropertyChangeListener listener) { 290 removeListenerObject(listener); 291 } 292 293 300 protected final void setDefined(final boolean defined) { 301 if (this.defined != defined) { 302 PropertyChangeEvent event = null; 303 if (isListenerAttached()) { 304 event = new PropertyChangeEvent(this, PROPERTY_DEFINED, 305 (this.defined ? Boolean.TRUE : Boolean.FALSE), 306 (defined ? Boolean.TRUE : Boolean.FALSE)); 307 } 308 this.defined = defined; 309 firePropertyChangeEvent(event); 310 } 311 } 312 313 321 protected final void setLocations(final SLocation[] locations) { 322 if (!Util.equals(this.locations, locations)) { 323 PropertyChangeEvent event = null; 324 if (isListenerAttached()) { 325 event = new PropertyChangeEvent(this, PROPERTY_LOCATIONS, 326 this.locations, locations); 327 } 328 this.locations = locations; 329 firePropertyChangeEvent(event); 330 } 331 } 332 333 346 final void setShowing(final Window window, final boolean showing) { 347 if (window == null) { 348 throw new NullPointerException ( 349 "The window in which a menu element is showing must not be null"); } 351 352 if (windowToShowingMap == null) { 353 windowToShowingMap = new WeakHashMap (3); 354 } 355 windowToShowingMap.put(window, showing ? Boolean.TRUE : Boolean.FALSE); 356 } 357 358 369 public final void setVisible(final Window window, final boolean visible) { 370 final boolean initialVisible = isVisible(window); 371 if (initialVisible != visible) { 372 if (windowToVisibleMap == null) { 373 windowToVisibleMap = new WeakHashMap (3); 374 } 375 windowToVisibleMap.put(window, visible ? Boolean.TRUE 376 : Boolean.FALSE); 377 fireVisibleChanged(visible); 378 } 379 } 380 } 381 | Popular Tags |