1 11 12 package org.eclipse.core.commands.contexts; 13 14 import java.util.Collections ; 15 import java.util.HashSet ; 16 import java.util.Set ; 17 18 import org.eclipse.core.commands.common.HandleObjectManager; 19 import org.eclipse.core.commands.util.Tracing; 20 import org.eclipse.core.internal.commands.util.Util; 21 22 35 public final class ContextManager extends HandleObjectManager implements 36 IContextListener { 37 38 private static final String DEFER_EVENTS = "org.eclipse.ui.internal.contexts.deferEvents"; private static final String SEND_EVENTS = "org.eclipse.ui.internal.contexts.sendEvents"; 41 46 public static boolean DEBUG = false; 47 48 52 private Set activeContextIds = new HashSet (); 53 54 private boolean caching = false; 56 57 private int cachingRef = 0; 58 59 private boolean activeContextsChange = false; 60 61 private Set oldIds = null; 62 63 70 public final void addActiveContext(final String contextId) { 71 if (DEFER_EVENTS.equals(contextId)) { 72 cachingRef++; 73 if (cachingRef==1) { 74 setEventCaching(true); 75 } 76 return; 77 } else if (SEND_EVENTS.equals(contextId)) { 78 cachingRef--; 79 if (cachingRef==0) { 80 setEventCaching(false); 81 } 82 return; 83 } 84 85 if (activeContextIds.contains(contextId)) { 86 return; 87 } 88 activeContextsChange = true; 89 90 if (caching) { 91 activeContextIds.add(contextId); 92 } else { 93 final Set previouslyActiveContextIds = new HashSet (activeContextIds); 94 activeContextIds.add(contextId); 95 96 fireContextManagerChanged(new ContextManagerEvent(this, null, 97 false, true, previouslyActiveContextIds)); 98 } 99 100 if (DEBUG) { 101 Tracing.printTrace("CONTEXTS", activeContextIds.toString()); } 103 104 } 105 106 114 public final void addContextManagerListener( 115 final IContextManagerListener listener) { 116 addListenerObject(listener); 117 } 118 119 public final void contextChanged(final ContextEvent contextEvent) { 120 if (contextEvent.isDefinedChanged()) { 121 final Context context = contextEvent.getContext(); 122 final String contextId = context.getId(); 123 final boolean contextIdAdded = context.isDefined(); 124 if (contextIdAdded) { 125 definedHandleObjects.add(context); 126 } else { 127 definedHandleObjects.remove(context); 128 } 129 if (isListenerAttached()) { 130 fireContextManagerChanged(new ContextManagerEvent(this, 131 contextId, contextIdAdded, false, null)); 132 } 133 } 134 } 135 136 144 private final void fireContextManagerChanged(final ContextManagerEvent event) { 145 if (event == null) { 146 throw new NullPointerException (); 147 } 148 149 final Object [] listeners = getListeners(); 150 for (int i = 0; i < listeners.length; i++) { 151 final IContextManagerListener listener = (IContextManagerListener) listeners[i]; 152 listener.contextManagerChanged(event); 153 } 154 } 155 156 164 public final Set getActiveContextIds() { 165 return Collections.unmodifiableSet(activeContextIds); 166 } 167 168 178 public final Context getContext(final String contextId) { 179 checkId(contextId); 180 181 Context context = (Context) handleObjectsById.get(contextId); 182 if (context == null) { 183 context = new Context(contextId); 184 handleObjectsById.put(contextId, context); 185 context.addContextListener(this); 186 } 187 188 return context; 189 } 190 191 197 public final Set getDefinedContextIds() { 198 return getDefinedHandleObjectIds(); 199 } 200 201 208 public final Context[] getDefinedContexts() { 209 return (Context[]) definedHandleObjects 210 .toArray(new Context[definedHandleObjects.size()]); 211 } 212 213 220 public final void removeActiveContext(final String contextId) { 221 if (!activeContextIds.contains(contextId)) { 222 return; 223 } 224 225 activeContextsChange = true; 226 if (caching) { 227 activeContextIds.remove(contextId); 228 } else { 229 final Set previouslyActiveContextIds = new HashSet (activeContextIds); 230 activeContextIds.remove(contextId); 231 232 fireContextManagerChanged(new ContextManagerEvent(this, null, 233 false, true, previouslyActiveContextIds)); 234 } 235 236 if (DEBUG) { 237 Tracing.printTrace("CONTEXTS", activeContextIds.toString()); } 239 } 240 241 247 public final void removeContextManagerListener( 248 final IContextManagerListener listener) { 249 removeListenerObject(listener); 250 } 251 252 261 public final void setActiveContextIds(final Set activeContextIds) { 262 if (Util.equals(this.activeContextIds, activeContextIds)) { 263 return; 264 } 265 266 activeContextsChange = true; 267 268 final Set previouslyActiveContextIds = this.activeContextIds; 269 if (activeContextIds != null) { 270 this.activeContextIds = new HashSet (); 271 this.activeContextIds.addAll(activeContextIds); 272 } else { 273 this.activeContextIds = null; 274 } 275 276 if (DEBUG) { 277 Tracing.printTrace("CONTEXTS", (activeContextIds == null) ? "none" : activeContextIds.toString()); 279 } 280 281 if (!caching) { 282 fireContextManagerChanged(new ContextManagerEvent(this, null, 283 false, true, previouslyActiveContextIds)); 284 } 285 } 286 287 295 private void setEventCaching(boolean cache) { 296 if (caching == cache) { 297 return; 298 } 299 caching = cache; 300 boolean fireChange = activeContextsChange; 301 Set holdOldIds = (oldIds==null?Collections.EMPTY_SET:oldIds); 302 303 if (caching) { 304 oldIds = new HashSet (activeContextIds); 305 } else { 306 oldIds = null; 307 } 308 activeContextsChange = false; 309 310 if (!caching && fireChange) { 311 fireContextManagerChanged(new ContextManagerEvent(this, null, 312 false, true, holdOldIds)); 313 } 314 } 315 } 316 | Popular Tags |