1 11 12 package org.eclipse.ui.internal.contexts; 13 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.HashMap ; 17 import java.util.HashSet ; 18 import java.util.Map ; 19 import java.util.Set ; 20 21 import org.eclipse.core.commands.contexts.Context; 22 import org.eclipse.core.commands.contexts.IContextManagerListener; 23 import org.eclipse.core.expressions.Expression; 24 import org.eclipse.swt.widgets.Shell; 25 import org.eclipse.ui.ISourceProvider; 26 import org.eclipse.ui.contexts.IContextActivation; 27 import org.eclipse.ui.contexts.IContextService; 28 import org.eclipse.ui.internal.expressions.AndExpression; 29 30 41 public class SlaveContextService implements IContextService { 42 43 46 protected IContextService fParentService; 47 48 53 protected Expression fDefaultExpression; 54 55 58 protected Set fParentActivations; 59 60 66 protected Map fLocalActivations; 67 68 73 private Collection fContextManagerListeners; 74 75 80 private Collection fSourceProviders; 81 82 87 private Collection fRegisteredShells; 88 89 99 public SlaveContextService(IContextService parentService, 100 Expression defaultExpression) { 101 if (parentService == null) { 102 throw new NullPointerException ( 103 "The parent context service must not be null"); } 105 fParentService = parentService; 106 fDefaultExpression = defaultExpression; 107 fParentActivations = new HashSet (); 108 fLocalActivations = new HashMap (); 109 fContextManagerListeners = new ArrayList (); 110 fSourceProviders = new ArrayList (); 111 fRegisteredShells = new ArrayList (); 112 } 113 114 119 public IContextActivation activateContext(String contextId) { 120 ContextActivation activation = new ContextActivation(contextId, 121 fDefaultExpression, this); 122 return doActivateContext(activation); 123 } 124 125 131 public IContextActivation activateContext(String contextId, 132 Expression expression) { 133 return activateContext(contextId, expression, false); 134 } 135 136 142 public IContextActivation activateContext(String contextId, 143 Expression expression, boolean global) { 144 if (global) { 145 IContextActivation activation = fParentService.activateContext( 146 contextId, expression, global); 147 fParentActivations.add(activation); 148 return activation; 149 } 150 AndExpression andExpression = null; 151 if (expression instanceof AndExpression) { 152 andExpression = (AndExpression) expression; 153 } else { 154 andExpression = new AndExpression(); 155 if (expression!=null) { 156 andExpression.add(expression); 157 } 158 } 159 if (fDefaultExpression!=null) { 160 andExpression.add(fDefaultExpression); 161 } 162 ContextActivation activation = new ContextActivation(contextId, 163 andExpression, this); 164 return doActivateContext(activation); 165 } 166 167 173 public IContextActivation activateContext(String contextId, 174 Expression expression, int sourcePriorities) { 175 return activateContext(contextId, expression); 176 } 177 178 183 public void addContextManagerListener(IContextManagerListener listener) { 184 if (!fContextManagerListeners.contains(listener)) { 185 fContextManagerListeners.add(listener); 186 } 187 fParentService.addContextManagerListener(listener); 188 } 189 190 195 public void addSourceProvider(ISourceProvider provider) { 196 if (!fSourceProviders.contains(provider)) { 197 fSourceProviders.add(provider); 198 } 199 fParentService.addSourceProvider(provider); 200 } 201 202 207 public void deactivateContext(IContextActivation activation) { 208 IContextActivation parentActivation = null; 209 if (fLocalActivations.containsKey(activation)) { 210 parentActivation = (IContextActivation) fLocalActivations 211 .remove(activation); 212 } else { 213 parentActivation = activation; 214 } 215 if (parentActivation != null) { 216 fParentService.deactivateContext(parentActivation); 217 fParentActivations.remove(parentActivation); 218 } 219 } 220 221 226 public void deactivateContexts(Collection activations) { 227 Object [] array = activations.toArray(); 228 for (int i = 0; i < array.length; i++) { 229 deactivateContext((IContextActivation) array[i]); 230 array[i] = null; 231 } 232 } 233 234 239 public void dispose() { 240 fParentService.deactivateContexts(fParentActivations); 241 fParentActivations.clear(); 242 fLocalActivations.clear(); 243 244 if (!fContextManagerListeners.isEmpty()) { 247 Object [] array = fContextManagerListeners.toArray(); 248 for (int i = 0; i < array.length; i++) { 249 removeContextManagerListener((IContextManagerListener) array[i]); 250 } 251 fContextManagerListeners.clear(); 252 } 253 if (!fSourceProviders.isEmpty()) { 254 Object [] array = fSourceProviders.toArray(); 255 for (int i = 0; i < array.length; i++) { 256 removeSourceProvider((ISourceProvider) array[i]); 257 } 258 fSourceProviders.clear(); 259 } 260 if (!fRegisteredShells.isEmpty()) { 261 Object [] array = fRegisteredShells.toArray(); 262 for (int i = 0; i < array.length; i++) { 263 unregisterShell((Shell) array[i]); 264 } 265 fRegisteredShells.clear(); 266 } 267 } 268 269 278 protected IContextActivation doActivateContext(IContextActivation activation) { 279 IContextActivation parentActivation = fParentService.activateContext( 280 activation.getContextId(), activation.getExpression()); 281 fParentActivations.add(parentActivation); 282 fLocalActivations.put(activation, parentActivation); 283 return activation; 284 } 285 286 291 public Collection getActiveContextIds() { 292 return fParentService.getActiveContextIds(); 293 } 294 295 300 public Context getContext(String contextId) { 301 return fParentService.getContext(contextId); 302 } 303 304 309 public Collection getDefinedContextIds() { 310 return fParentService.getDefinedContextIds(); 311 } 312 313 318 public Context[] getDefinedContexts() { 319 return fParentService.getDefinedContexts(); 320 } 321 322 327 public int getShellType(Shell shell) { 328 return fParentService.getShellType(shell); 329 } 330 331 336 public void readRegistry() { 337 fParentService.readRegistry(); 338 } 339 340 346 public boolean registerShell(Shell shell, int type) { 347 if (!fRegisteredShells.contains(shell)) { 348 fRegisteredShells.add(shell); 349 } 350 return fParentService.registerShell(shell, type); 351 } 352 353 358 public void removeContextManagerListener(IContextManagerListener listener) { 359 fContextManagerListeners.remove(listener); 360 fParentService.removeContextManagerListener(listener); 361 } 362 363 368 public void removeSourceProvider(ISourceProvider provider) { 369 fSourceProviders.remove(provider); 370 fParentService.removeSourceProvider(provider); 371 } 372 373 378 public boolean unregisterShell(Shell shell) { 379 fRegisteredShells.remove(shell); 380 return fParentService.unregisterShell(shell); 381 } 382 } 383 | Popular Tags |