KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > handlers > HandlerAuthority


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal.handlers;
13
14 import java.io.BufferedWriter JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.StringWriter JavaDoc;
17 import java.util.Collection JavaDoc;
18 import java.util.Collections JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
24 import java.util.SortedSet JavaDoc;
25 import java.util.TreeSet JavaDoc;
26
27 import org.eclipse.core.commands.Command;
28 import org.eclipse.core.commands.IHandler;
29 import org.eclipse.core.commands.util.Tracing;
30 import org.eclipse.core.expressions.EvaluationContext;
31 import org.eclipse.core.expressions.EvaluationResult;
32 import org.eclipse.core.expressions.Expression;
33 import org.eclipse.core.expressions.IEvaluationContext;
34 import org.eclipse.core.runtime.CoreException;
35 import org.eclipse.core.runtime.IStatus;
36 import org.eclipse.core.runtime.MultiStatus;
37 import org.eclipse.core.runtime.Status;
38 import org.eclipse.swt.widgets.Shell;
39 import org.eclipse.ui.ISources;
40 import org.eclipse.ui.commands.ICommandService;
41 import org.eclipse.ui.handlers.IHandlerActivation;
42 import org.eclipse.ui.internal.WorkbenchPlugin;
43 import org.eclipse.ui.internal.misc.Policy;
44 import org.eclipse.ui.internal.services.EvaluationResultCacheComparator;
45 import org.eclipse.ui.internal.services.ExpressionAuthority;
46
47 /**
48  * <p>
49  * A central authority for resolving conflicts between handlers. This authority
50  * listens to a variety of incoming sources, and updates the underlying commands
51  * if changes in the active handlers occur.
52  * </p>
53  * <p>
54  * This authority encapsulates all of the handler conflict resolution mechanisms
55  * for the workbench. A conflict occurs if two or more handlers are assigned to
56  * the same command identifier. To resolve this conflict, the authority
57  * considers which source the handler came from.
58  * </p>
59  *
60  * @since 3.1
61  */

62 final class HandlerAuthority extends ExpressionAuthority {
63
64     /**
65      * The default size of the set containing the activations to recompute. This
66      * is more than enough to cover the average case.
67      */

68     private static final int ACTIVATIONS_BY_SOURCE_SIZE = 256;
69
70     /**
71      * The default size of the set containing the activations to recompute. This
72      * is more than enough to cover the average case.
73      */

74     private static final int ACTIVATIONS_TO_RECOMPUTE_SIZE = 1024;
75
76     /**
77      * Whether the workbench command support should kick into debugging mode.
78      * This causes the unresolvable handler conflicts to be printed to the
79      * console.
80      */

81     private static final boolean DEBUG = Policy.DEBUG_HANDLERS;
82
83     /**
84      * Whether the performance information should be printed about the
85      * performance of the handler authority.
86      */

87     private static final boolean DEBUG_PERFORMANCE = Policy.DEBUG_HANDLERS_PERFORMANCE;
88
89     /**
90      * Whether the workbench command support should kick into verbose debugging
91      * mode. This causes the resolvable handler conflicts to be printed to the
92      * console.
93      */

94     private static final boolean DEBUG_VERBOSE = Policy.DEBUG_HANDLERS
95             && Policy.DEBUG_HANDLERS_VERBOSE;
96
97     /**
98      * The command identifier to which the verbose output should be restricted.
99      */

100     private static final String JavaDoc DEBUG_VERBOSE_COMMAND_ID = Policy.DEBUG_HANDLERS_VERBOSE_COMMAND_ID;
101
102     /**
103      * The component name to print when displaying tracing information.
104      */

105     private static final String JavaDoc TRACING_COMPONENT = "HANDLERS"; //$NON-NLS-1$
106

107     /**
108      * A bucket sort of the handler activations based on source priority of its
109      * expression. Each expression will appear only once per set, but may appear
110      * in multiple sets. If no activations are defined for a particular priority
111      * level, then the array at that index will only contain <code>null</code>.
112      * This is an array of {@link Map}, where the maps contain instances of
113      * {@link Collection} containing instances of {@link IHandlerActivation}
114      * indexed by instances of {@link Expression}.
115      */

116     private final Map JavaDoc[] activationsByExpressionBySourcePriority = new Map JavaDoc[33];
117
118     /**
119      * The command service that should be updated when the handlers are
120      * changing. This value is never <code>null</code>.
121      */

122     private final ICommandService commandService;
123
124     /**
125      * This is a map of handler activations (<code>SortedSet</code> of
126      * <code>IHandlerActivation</code>) sorted by command identifier (<code>String</code>).
127      * If there is only one handler activation for a command, then the
128      * <code>SortedSet</code> is replaced by a <code>IHandlerActivation</code>.
129      * If there is no activation, the entry should be removed entirely.
130      */

131     private final Map JavaDoc handlerActivationsByCommandId = new HashMap JavaDoc();
132
133     private Set JavaDoc previousLogs = new HashSet JavaDoc();
134
135     /**
136      * Constructs a new instance of <code>HandlerAuthority</code>.
137      *
138      * @param commandService
139      * The command service from which commands can be retrieved (to
140      * update their handlers); must not be <code>null</code>.
141      */

142     HandlerAuthority(final ICommandService commandService) {
143         if (commandService == null) {
144             throw new NullPointerException JavaDoc(
145                     "The handler authority needs a command service"); //$NON-NLS-1$
146
}
147
148         this.commandService = commandService;
149     }
150
151     /**
152      * Activates a handler on the workbench. This will add it to a master list.
153      * If conflicts exist, they will be resolved based on the source priority.
154      * If conflicts still exist, then no handler becomes active.
155      *
156      * @param activation
157      * The activation; must not be <code>null</code>.
158      */

159     final void activateHandler(final IHandlerActivation activation) {
160         // First we update the handlerActivationsByCommandId map.
161
final String JavaDoc commandId = activation.getCommandId();
162         MultiStatus conflicts = new MultiStatus("org.eclipse.ui.workbench", 0, //$NON-NLS-1$
163
"A handler conflict occurred. This may disable some commands.", //$NON-NLS-1$
164
null);
165         final Object JavaDoc value = handlerActivationsByCommandId.get(commandId);
166         if (value instanceof SortedSet JavaDoc) {
167             final SortedSet JavaDoc handlerActivations = (SortedSet JavaDoc) value;
168             if (!handlerActivations.contains(activation)) {
169                 handlerActivations.add(activation);
170                 updateCommand(commandId, resolveConflicts(commandId,
171                         handlerActivations, conflicts));
172             }
173         } else if (value instanceof IHandlerActivation) {
174             if (value != activation) {
175                 final SortedSet JavaDoc handlerActivations = new TreeSet JavaDoc(
176                         new EvaluationResultCacheComparator());
177                 handlerActivations.add(value);
178                 handlerActivations.add(activation);
179                 handlerActivationsByCommandId
180                         .put(commandId, handlerActivations);
181                 updateCommand(commandId, resolveConflicts(commandId,
182                         handlerActivations, conflicts));
183             }
184         } else {
185             handlerActivationsByCommandId.put(commandId, activation);
186             updateCommand(commandId, (evaluate(activation) ? activation : null));
187         }
188         
189         if (conflicts.getSeverity()!=IStatus.OK) {
190             WorkbenchPlugin.log(conflicts);
191         }
192
193         // Next we update the source priority bucket sort of activations.
194
final int sourcePriority = activation.getSourcePriority();
195         for (int i = 1; i <= 32; i++) {
196             if ((sourcePriority & (1 << i)) != 0) {
197                 Map JavaDoc activationsByExpression = activationsByExpressionBySourcePriority[i];
198                 if (activationsByExpression == null) {
199                     activationsByExpression = new HashMap JavaDoc(
200                             ACTIVATIONS_BY_SOURCE_SIZE);
201                     activationsByExpressionBySourcePriority[i] = activationsByExpression;
202                 }
203
204                 final Expression expression = activation.getExpression();
205                 Collection JavaDoc activations = (Collection JavaDoc) activationsByExpression
206                         .get(expression);
207                 if (activations == null) {
208                     activations = new HashSet JavaDoc();
209                     activationsByExpression.put(expression, activations);
210                 }
211                 activations.add(activation);
212             }
213         }
214     }
215
216     /**
217      * Removes an activation for a handler on the workbench. This will remove it
218      * from the master list, and update the appropriate command, if necessary.
219      *
220      * @param activation
221      * The activation; must not be <code>null</code>.
222      */

223     final void deactivateHandler(final IHandlerActivation activation) {
224         // First we update the handlerActivationsByCommandId map.
225
final String JavaDoc commandId = activation.getCommandId();
226         MultiStatus conflicts = new MultiStatus("org.eclipse.ui.workbench", 0, //$NON-NLS-1$
227
"A handler conflict occurred. This may disable some commands.", //$NON-NLS-1$
228
null);
229         final Object JavaDoc value = handlerActivationsByCommandId.get(commandId);
230         if (value instanceof SortedSet JavaDoc) {
231             final SortedSet JavaDoc handlerActivations = (SortedSet JavaDoc) value;
232             if (handlerActivations.contains(activation)) {
233                 handlerActivations.remove(activation);
234                 if (handlerActivations.isEmpty()) {
235                     handlerActivationsByCommandId.remove(commandId);
236                     updateCommand(commandId, null);
237
238                 } else if (handlerActivations.size() == 1) {
239                     final IHandlerActivation remainingActivation = (IHandlerActivation) handlerActivations
240                             .iterator().next();
241                     handlerActivationsByCommandId.put(commandId,
242                             remainingActivation);
243                     updateCommand(
244                             commandId,
245                             (evaluate(remainingActivation) ? remainingActivation
246                                     : null));
247
248                 } else {
249                     updateCommand(commandId, resolveConflicts(commandId,
250                             handlerActivations, conflicts));
251                 }
252             }
253         } else if (value instanceof IHandlerActivation) {
254             if (value == activation) {
255                 handlerActivationsByCommandId.remove(commandId);
256                 updateCommand(commandId, null);
257             }
258         }
259         if (conflicts.getSeverity()!=IStatus.OK) {
260             WorkbenchPlugin.log(conflicts);
261         }
262
263         // Next we update the source priority bucket sort of activations.
264
final int sourcePriority = activation.getSourcePriority();
265         for (int i = 1; i <= 32; i++) {
266             if ((sourcePriority & (1 << i)) != 0) {
267                 final Map JavaDoc activationsByExpression = activationsByExpressionBySourcePriority[i];
268                 if (activationsByExpression == null) {
269                     continue;
270                 }
271
272                 final Expression expression = activation.getExpression();
273                 final Collection JavaDoc activations = (Collection JavaDoc) activationsByExpression
274                         .get(expression);
275                 activations.remove(activation);
276                 if (activations.isEmpty()) {
277                     activationsByExpression.remove(expression);
278                 }
279
280                 if (activationsByExpression.isEmpty()) {
281                     activationsByExpressionBySourcePriority[i] = null;
282                 }
283             }
284         }
285     }
286
287     /**
288      * Returns the currently active shell.
289      *
290      * @return The currently active shell; may be <code>null</code>.
291      */

292     final Shell getActiveShell() {
293         return (Shell) getVariable(ISources.ACTIVE_SHELL_NAME);
294     }
295
296     /**
297      * Resolves conflicts between multiple handlers for the same command
298      * identifier. This tries to select the best activation based on the source
299      * priority. For the sake of comparison, activations with the same handler
300      * are considered equivalent (i.e., non-conflicting).
301      *
302      * @param commandId
303      * The identifier of the command for which the conflicts should
304      * be detected; must not be <code>null</code>. This is only
305      * used for debugging purposes.
306      * @param activations
307      * All of the possible handler activations for the given command
308      * identifier; must not be <code>null</code>.
309      * @return The best matching handler activation. If none can be found (e.g.,
310      * because of unresolvable conflicts), then this returns
311      * <code>null</code>.
312      */

313     private final IHandlerActivation resolveConflicts(final String JavaDoc commandId,
314             final SortedSet JavaDoc activations, MultiStatus conflicts) {
315         // If we don't have any, then there is no match.
316
if (activations.isEmpty()) {
317             return null;
318         }
319
320         // Cycle over the activations, remembered the current best.
321
final Iterator JavaDoc activationItr = activations.iterator();
322         IHandlerActivation bestActivation = null;
323         IHandlerActivation currentActivation = null;
324         boolean conflict = false;
325         while (activationItr.hasNext()) {
326             currentActivation = (IHandlerActivation) activationItr.next();
327             if (!evaluate(currentActivation)) {
328                 continue; // only consider potentially active handlers
329
}
330
331             // Check to see if we haven't found a potentially active handler yet
332
if ((DEBUG_VERBOSE)
333                     && ((DEBUG_VERBOSE_COMMAND_ID == null) || (DEBUG_VERBOSE_COMMAND_ID
334                             .equals(commandId)))) {
335                 Tracing.printTrace(TRACING_COMPONENT,
336                         " resolveConflicts: eval: " + currentActivation); //$NON-NLS-1$
337
}
338             if (bestActivation == null) {
339                 bestActivation = currentActivation;
340                 conflict = false;
341                 continue;
342             }
343
344             // Compare the two handlers.
345
final int comparison = bestActivation.compareTo(currentActivation);
346             if (comparison < 0) {
347                 bestActivation = currentActivation;
348                 conflict = false;
349
350             } else if (comparison == 0) {
351                 if (currentActivation.getHandler() != bestActivation
352                         .getHandler()) {
353                     conflict = true;
354                     break;
355                 }
356
357             } else {
358                 break;
359             }
360         }
361
362         // If we are logging information, now is the time to do it.
363
if (DEBUG) {
364             if (conflict) {
365                 Tracing.printTrace(TRACING_COMPONENT,
366                         "Unresolved conflict detected for '" //$NON-NLS-1$
367
+ commandId + '\'');
368             } else if ((bestActivation != null)
369                     && (DEBUG_VERBOSE)
370                     && ((DEBUG_VERBOSE_COMMAND_ID == null) || (DEBUG_VERBOSE_COMMAND_ID
371                             .equals(commandId)))) {
372                 Tracing
373                         .printTrace(TRACING_COMPONENT,
374                                 "Resolved conflict detected. The following activation won: "); //$NON-NLS-1$
375
Tracing.printTrace(TRACING_COMPONENT, " " + bestActivation); //$NON-NLS-1$
376
}
377         }
378
379         // Return the current best.
380
if (conflict) {
381             if (previousLogs.add(commandId)) {
382                 final StringWriter JavaDoc sw = new StringWriter JavaDoc();
383                 final BufferedWriter JavaDoc buffer = new BufferedWriter JavaDoc(sw);
384                 try {
385                     buffer.write("Conflict for \'"); //$NON-NLS-1$
386
buffer.write(commandId);
387                     buffer.write("\':"); //$NON-NLS-1$
388
buffer.newLine();
389                     buffer.write(bestActivation.toString());
390                     buffer.newLine();
391                     buffer.write(currentActivation.toString());
392                     buffer.flush();
393                 } catch (IOException JavaDoc e) {
394                     //should never get this.
395
}
396
397                 IStatus s = new Status(IStatus.WARNING,
398                         "org.eclipse.ui.workbench", //$NON-NLS-1$
399
sw.toString());
400                 conflicts.add(s);
401             }
402             return null;
403         }
404         return bestActivation;
405     }
406
407     /**
408      * Carries out the actual source change notification. It assumed that by the
409      * time this method is called, <code>context</code> is up-to-date with the
410      * current state of the application.
411      *
412      * @param sourcePriority
413      * A bit mask of all the source priorities that have changed.
414      */

415     protected final void sourceChanged(final int sourcePriority) {
416         // If tracing, then track how long it takes to process the activations.
417
long startTime = 0L;
418         if (DEBUG_PERFORMANCE) {
419             startTime = System.currentTimeMillis();
420         }
421
422         /*
423          * In this first phase, we cycle through all of the activations that
424          * could have potentially changed. Each such activation is added to a
425          * set for future processing. We add it to a set so that we avoid
426          * handling any individual activation more than once.
427          */

428         final Collection JavaDoc changedCommandIds = new HashSet JavaDoc(
429                 ACTIVATIONS_TO_RECOMPUTE_SIZE);
430         for (int i = 1; i <= 32; i++) {
431             if ((sourcePriority & (1 << i)) != 0) {
432                 final Map JavaDoc activationsByExpression = activationsByExpressionBySourcePriority[i];
433                 if (activationsByExpression != null) {
434                     final Iterator JavaDoc activationByExpressionItr = activationsByExpression
435                             .values().iterator();
436                     while (activationByExpressionItr.hasNext()) {
437                         final Collection JavaDoc activations = (Collection JavaDoc) activationByExpressionItr
438                                 .next();
439                         final Iterator JavaDoc activationItr = activations.iterator();
440
441                         // Check the first activation to see if it has changed.
442
if (activationItr.hasNext()) {
443                             IHandlerActivation activation = (IHandlerActivation) activationItr
444                                     .next();
445                             final boolean currentActive = evaluate(activation);
446                             activation.clearResult();
447                             final boolean newActive = evaluate(activation);
448                             if (newActive != currentActive) {
449                                 changedCommandIds
450                                         .add(activation.getCommandId());
451
452                                 // Then add every other activation as well.
453
while (activationItr.hasNext()) {
454                                     activation = (IHandlerActivation) activationItr
455                                             .next();
456                                     activation.setResult(newActive);
457
458                                     changedCommandIds.add(activation
459                                             .getCommandId());
460                                 }
461                             } else {
462                                 while (activationItr.hasNext()) {
463                                     activation = (IHandlerActivation) activationItr
464                                             .next();
465                                     // if for some reason another activation
466
// doesn't match the new result, update and
467
// mark as changed. It's not as expensive
468
// as it looks :-)
469
if (newActive != evaluate(activation)) {
470                                         activation.setResult(newActive);
471                                         changedCommandIds.add(activation
472                                                 .getCommandId());
473                                     }
474                                 }
475                             }
476                         }
477                     }
478                 }
479             }
480         }
481
482         MultiStatus conflicts = new MultiStatus("org.eclipse.ui.workbench", 0, //$NON-NLS-1$
483
"A handler conflict occurred. This may disable some commands.", //$NON-NLS-1$
484
null);
485         
486         /*
487          * For every command identifier with a changed activation, we resolve
488          * conflicts and trigger an update.
489          */

490         final Iterator JavaDoc changedCommandIdItr = changedCommandIds.iterator();
491         while (changedCommandIdItr.hasNext()) {
492             final String JavaDoc commandId = (String JavaDoc) changedCommandIdItr.next();
493             final Object JavaDoc value = handlerActivationsByCommandId.get(commandId);
494             if (value instanceof IHandlerActivation) {
495                 final IHandlerActivation activation = (IHandlerActivation) value;
496                 updateCommand(commandId, (evaluate(activation) ? activation
497                         : null));
498             } else if (value instanceof SortedSet JavaDoc) {
499                 final IHandlerActivation activation = resolveConflicts(
500                         commandId, (SortedSet JavaDoc) value, conflicts);
501                 updateCommand(commandId, activation);
502             } else {
503                 updateCommand(commandId, null);
504             }
505         }
506         if (conflicts.getSeverity()!=IStatus.OK) {
507             WorkbenchPlugin.log(conflicts);
508         }
509
510         // If tracing performance, then print the results.
511
if (DEBUG_PERFORMANCE) {
512             final long elapsedTime = System.currentTimeMillis() - startTime;
513             final int size = changedCommandIds.size();
514             if (size > 0) {
515                 Tracing.printTrace(TRACING_COMPONENT, size
516                         + " command ids changed in " + elapsedTime + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
517
}
518         }
519     }
520
521     /**
522      * Updates the command with the given handler activation.
523      *
524      * @param commandId
525      * The identifier of the command which should be updated; must
526      * not be <code>null</code>.
527      * @param activation
528      * The activation to use; may be <code>null</code> if the
529      * command should have a <code>null</code> handler.
530      */

531     private final void updateCommand(final String JavaDoc commandId,
532             final IHandlerActivation activation) {
533         final Command command = commandService.getCommand(commandId);
534         if (activation == null) {
535             command.setHandler(null);
536         } else {
537             command.setHandler(activation.getHandler());
538             commandService.refreshElements(commandId, null);
539         }
540     }
541
542     /**
543      * <p>
544      * Bug 95792. A mechanism by which the key binding architecture can force an
545      * update of the handlers (based on the active shell) before trying to
546      * execute a command. This mechanism is required for GTK+ only.
547      * </p>
548      * <p>
549      * DO NOT CALL THIS METHOD.
550      * </p>
551      */

552     final void updateShellKludge() {
553         updateCurrentState();
554         sourceChanged(ISources.ACTIVE_SHELL);
555     }
556
557     /**
558      * Currently this is a an internal method to help locate a handler.
559      * <p>
560      * DO NOT CALL THIS METHOD.
561      * </p>
562      *
563      * @param commandId
564      * the command id to check
565      * @param context
566      * the context to use for activations
567      * @since 3.3
568      */

569     public final IHandler findHandler(String JavaDoc commandId,
570             IEvaluationContext context) {
571         Object JavaDoc o = handlerActivationsByCommandId.get(commandId);
572         if (o instanceof IHandlerActivation) {
573             IHandlerActivation activation = (IHandlerActivation) o;
574             try {
575                 if (eval(context, activation)) {
576                     return activation.getHandler();
577                 }
578             } catch (CoreException e) {
579                 // the evalution failed
580
}
581         } else if (o instanceof SortedSet JavaDoc) {
582             SortedSet JavaDoc activations = (SortedSet JavaDoc) o;
583             IHandlerActivation lastActivation = null;
584             IHandlerActivation currentActivation = null;
585             Iterator JavaDoc i = activations.iterator();
586             while (i.hasNext() && lastActivation==null) {
587                 IHandlerActivation activation = (IHandlerActivation) i.next();
588                 try {
589                     if (eval(context, activation)) {
590                         lastActivation = currentActivation;
591                         currentActivation = activation;
592                     }
593                 } catch (CoreException e) {
594                     // OK, this one is out of the running
595
}
596             }
597             if (currentActivation != null) {
598                 if (lastActivation == null) {
599                     return currentActivation.getHandler();
600                 }
601                 if (lastActivation.getSourcePriority() != currentActivation
602                         .getSourcePriority()) {
603                     return lastActivation.getHandler();
604                 }
605             }
606         }
607         return null;
608     }
609
610     /**
611      * Evaluate the expression for the handler and bypass the result cache.
612      * <p>
613      * DO NOT CALL THIS METHOD.
614      * </p>
615      *
616      * @param context
617      * @param activation
618      * @return <code>true</code> if the handler expression can evaluate to
619      * true.
620      * @throws CoreException
621      * @since 3.3
622      */

623     private boolean eval(IEvaluationContext context,
624             IHandlerActivation activation) throws CoreException {
625         Expression expression = activation.getExpression();
626         if (expression == null) {
627             return true;
628         }
629         return expression.evaluate(context) == EvaluationResult.TRUE;
630     }
631
632     /**
633      * Normally the context returned from getCurrentState() still tracks the
634      * application state. This method creates a copy and fills it in with the
635      * variables that we know about. Currently it does not fill in the active
636      * selection.
637      * <p>
638      * DO NOT CALL THIS METHOD. It is experimental in 3.3.
639      * </p>
640      *
641      * @return an evaluation context with no parent.
642      * @since 3.3
643      */

644     public IEvaluationContext getContextSnapshot() {
645         return fillInContext(false);
646     }
647     
648     /**
649      * Normally the context returned from getCurrentState() still tracks the
650      * application state. This method creates a copy and fills it in with all the
651      * variables that we know about.
652      * <p>
653      * DO NOT CALL THIS METHOD. It is experimental in 3.3.
654      * </p>
655      *
656      * @return an evaluation context with no parent.
657      * @since 3.3
658      */

659     public IEvaluationContext getFullContextSnapshot() {
660         return fillInContext(true);
661     }
662     
663     private IEvaluationContext fillInContext(boolean fullContext) {
664         IEvaluationContext tmpContext = getCurrentState();
665
666         EvaluationContext context = null;
667         if (fullContext) {
668             context = new EvaluationContext(null, tmpContext.getDefaultVariable());
669             copyVariable(context, tmpContext, ISources.ACTIVE_CURRENT_SELECTION_NAME);
670             copyVariable(context, tmpContext, ISources.ACTIVE_FOCUS_CONTROL_ID_NAME);
671             copyVariable(context, tmpContext, ISources.ACTIVE_FOCUS_CONTROL_NAME);
672             copyVariable(context, tmpContext, ISources.ACTIVE_MENU_EDITOR_INPUT_NAME);
673             copyVariable(context, tmpContext, ISources.ACTIVE_MENU_NAME);
674             copyVariable(context, tmpContext, ISources.ACTIVE_MENU_SELECTION_NAME);
675         } else {
676             context = new EvaluationContext(null, Collections.EMPTY_LIST);
677         }
678
679         copyVariable(context, tmpContext, ISources.ACTIVE_ACTION_SETS_NAME);
680         copyVariable(context, tmpContext, ISources.ACTIVE_CONTEXT_NAME);
681         copyVariable(context, tmpContext, ISources.ACTIVE_EDITOR_ID_NAME);
682         copyVariable(context, tmpContext, ISources.ACTIVE_EDITOR_NAME);
683         copyVariable(context, tmpContext, ISources.ACTIVE_PART_ID_NAME);
684         copyVariable(context, tmpContext, ISources.ACTIVE_PART_NAME);
685         copyVariable(context, tmpContext, ISources.ACTIVE_SITE_NAME);
686         copyVariable(context, tmpContext,
687                 ISources.ACTIVE_WORKBENCH_WINDOW_IS_COOLBAR_VISIBLE_NAME);
688         copyVariable(context, tmpContext,
689                 ISources.ACTIVE_WORKBENCH_WINDOW_IS_PERSPECTIVEBAR_VISIBLE_NAME);
690         copyVariable(context, tmpContext, ISources.ACTIVE_WORKBENCH_WINDOW_NAME);
691         copyVariable(context, tmpContext,
692                 ISources.ACTIVE_WORKBENCH_WINDOW_SHELL_NAME);
693         copyVariable(context, tmpContext, ISources.ACTIVE_SHELL_NAME);
694
695         return context;
696     }
697     
698     private void copyVariable(IEvaluationContext context,
699             IEvaluationContext tmpContext, String JavaDoc var) {
700         Object JavaDoc o = tmpContext.getVariable(var);
701         if (o != null) {
702             context.addVariable(var, o);
703         }
704     }
705 }
706
Popular Tags