1 11 12 package org.eclipse.ui.internal.services; 13 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.Collections ; 17 import java.util.Iterator ; 18 import java.util.Map ; 19 20 import org.eclipse.core.expressions.EvaluationContext; 21 import org.eclipse.core.expressions.Expression; 22 import org.eclipse.core.expressions.IEvaluationContext; 23 import org.eclipse.jface.viewers.ISelection; 24 import org.eclipse.jface.viewers.IStructuredSelection; 25 import org.eclipse.ui.ISourceProvider; 26 import org.eclipse.ui.ISourceProviderListener; 27 import org.eclipse.ui.ISources; 28 29 45 public abstract class ExpressionAuthority implements ISourceProviderListener { 46 47 52 private final IEvaluationContext context; 53 54 60 private IEvaluationContext currentState = null; 61 62 67 private final Collection providers = new ArrayList (); 68 69 72 protected ExpressionAuthority() { 73 context = new EvaluationContext(null, this); 74 context.setAllowPluginActivation(true); 75 } 76 77 84 public final void addSourceProvider(final ISourceProvider provider) { 85 provider.addSourceProviderListener(this); 86 providers.add(provider); 87 88 final Map currentState = provider.getCurrentState(); 90 final Iterator variableItr = currentState.entrySet().iterator(); 91 while (variableItr.hasNext()) { 92 final Map.Entry entry = (Map.Entry ) variableItr.next(); 93 final String variableName = (String ) entry.getKey(); 94 final Object variableValue = entry.getValue(); 95 96 101 if ((variableName != null) 102 && (!ISources.ACTIVE_WORKBENCH_WINDOW_SHELL_NAME 103 .equals(variableName))) { 104 changeVariable(variableName, variableValue); 105 } 106 } 107 } 108 109 113 public void dispose() { 114 final Iterator providerItr = providers.iterator(); 115 while (providerItr.hasNext()) { 116 final ISourceProvider provider = (ISourceProvider) providerItr 117 .next(); 118 provider.removeSourceProviderListener(this); 119 } 120 121 providers.clear(); 122 } 123 124 135 protected final boolean evaluate(final Collection collection) { 136 final Iterator iterator = collection.iterator(); 137 while (iterator.hasNext()) { 138 final IEvaluationResultCache cache = (IEvaluationResultCache) iterator 139 .next(); 140 if (evaluate(cache)) { 141 return true; 142 } 143 } 144 145 return false; 146 } 147 148 158 protected final boolean evaluate(final IEvaluationResultCache expression) { 159 final IEvaluationContext contextWithDefaultVariable = getCurrentState(); 160 return expression.evaluate(contextWithDefaultVariable); 161 } 162 163 171 public final IEvaluationContext getCurrentState() { 172 if (currentState == null) { 173 final Object defaultVariable = context 174 .getVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME); 175 final IEvaluationContext contextWithDefaultVariable; 176 if (defaultVariable instanceof IStructuredSelection) { 177 final IStructuredSelection selection = (IStructuredSelection) defaultVariable; 178 contextWithDefaultVariable = new EvaluationContext(context, 179 selection.toList()); 180 } else if ((defaultVariable instanceof ISelection) 181 && (!((ISelection) defaultVariable).isEmpty())) { 182 contextWithDefaultVariable = new EvaluationContext(context, 183 Collections.singleton(defaultVariable)); 184 } else { 185 contextWithDefaultVariable = new EvaluationContext(context, 186 Collections.EMPTY_LIST); 187 } 188 currentState = contextWithDefaultVariable; 189 } 190 191 return currentState; 192 } 193 194 201 protected final Object getVariable(final String name) { 202 return context.getVariable(name); 203 } 204 205 212 public final void removeSourceProvider(final ISourceProvider provider) { 213 provider.removeSourceProviderListener(this); 214 providers.remove(provider); 215 216 final Map currentState = provider.getCurrentState(); 217 final Iterator variableItr = currentState.entrySet().iterator(); 218 while (variableItr.hasNext()) { 219 final Map.Entry entry = (Map.Entry ) variableItr.next(); 220 final String variableName = (String ) entry.getKey(); 221 changeVariable(variableName, null); 222 } 223 } 224 225 236 protected final void changeVariable(final String name, final Object value) { 237 if (value == null) { 238 context.removeVariable(name); 239 } else { 240 context.addVariable(name, value); 241 } 242 } 243 244 252 protected abstract void sourceChanged(final int sourcePriority); 253 254 266 protected void sourceChanged(final String [] sourceNames) { 267 } 269 270 public final void sourceChanged(final int sourcePriority, 271 final Map sourceValuesByName) { 272 if (sourceValuesByName 274 .containsKey(ISources.ACTIVE_CURRENT_SELECTION_NAME)) { 275 currentState = null; 276 } 277 278 final Iterator entryItr = sourceValuesByName.entrySet().iterator(); 279 while (entryItr.hasNext()) { 280 final Map.Entry entry = (Map.Entry ) entryItr.next(); 281 final String sourceName = (String ) entry.getKey(); 282 final Object sourceValue = entry.getValue(); 283 updateEvaluationContext(sourceName, sourceValue); 284 } 285 sourceChanged(sourcePriority, (String []) sourceValuesByName.keySet() 286 .toArray(new String [0])); 287 } 288 289 public final void sourceChanged(final int sourcePriority, 290 final String sourceName, final Object sourceValue) { 291 if (ISources.ACTIVE_CURRENT_SELECTION_NAME.equals(sourceName)) { 293 currentState = null; 294 } 295 296 updateEvaluationContext(sourceName, sourceValue); 297 sourceChanged(sourcePriority, new String [] { sourceName }); 298 } 299 300 304 private void sourceChanged(int sourcePriority, String [] sourceNames) { 305 sourceChanged(sourcePriority); 306 sourceChanged(sourceNames); 307 } 308 309 313 protected final void updateCurrentState() { 314 final Iterator providerItr = providers.iterator(); 315 while (providerItr.hasNext()) { 316 final ISourceProvider provider = (ISourceProvider) providerItr 317 .next(); 318 final Map currentState = provider.getCurrentState(); 319 final Iterator variableItr = currentState.entrySet().iterator(); 320 while (variableItr.hasNext()) { 321 final Map.Entry entry = (Map.Entry ) variableItr.next(); 322 final String variableName = (String ) entry.getKey(); 323 final Object variableValue = entry.getValue(); 324 329 if ((variableName != null) 330 && (!ISources.ACTIVE_WORKBENCH_WINDOW_SHELL_NAME 331 .equals(variableName))) { 332 changeVariable(variableName, variableValue); 333 } 334 } 335 } 336 } 337 338 348 protected void updateEvaluationContext(final String name, final Object value) { 349 if (name != null) { 350 changeVariable(name, value); 351 } 352 } 353 } 354 | Popular Tags |