KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > services > SourcePriorityNameMapping


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.services;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.core.expressions.Expression;
18 import org.eclipse.core.expressions.ExpressionInfo;
19 import org.eclipse.ui.ISources;
20
21 /**
22  * <p>
23  * A static class linking the names of variables in an IEvaluationContext to the
24  * priority they should be given when doing conflict resolution.
25  * </p>
26  * <p>
27  * In the future, it will possible to define a new variable (i.e., piece of
28  * application state) that you want to use inside of the
29  * <code>org.eclipse.ui.contexts</code>, <code>org.eclipse.ui.handlers</code>
30  * or <code>org.eclipse.ui.menus</code> extension points. As it stands right
31  * now, it is not possible to run code soon enough for the
32  * <code>IHandlerService</code>, <code>IMenuService</code> or
33  * <code>IContextService</code> to become aware of the new variables. This
34  * will likely be fixed with a new extension point.
35  * </p>
36  * <p>
37  * TODO Move to "org.eclipse.ui" and resolve the above issue.
38  * </p>
39  *
40  * @since 3.2
41  * @see org.eclipse.ui.ISources
42  * @see org.eclipse.ui.contexts.IContextService
43  * @see org.eclipse.ui.handlers.IHandlerService
44  * @see org.eclipse.ui.menus.IMenuService
45  */

46 public final class SourcePriorityNameMapping implements ISources {
47
48     /**
49      * The variable name to use when boosting priority on an activation.
50      */

51     public static final String JavaDoc LEGACY_LEGACY_NAME = "LEGACY"; //$NON-NLS-1$
52

53     /**
54      * The value returned if there is source priority for the given name
55      *
56      * @see SourcePriorityNameMapping#getMapping(String)
57      */

58     public static final int NO_SOURCE_PRIORITY = 0;
59
60     /**
61      * The map of source priorities indexed by name. This value is never
62      * <code>null</code>.
63      */

64     private static final Map JavaDoc sourcePrioritiesByName = new HashMap JavaDoc();
65
66     static {
67         addMapping(ACTIVE_ACTION_SETS_NAME, ACTIVE_ACTION_SETS);
68         addMapping(ACTIVE_CONTEXT_NAME, ACTIVE_CONTEXT);
69         addMapping(ACTIVE_CURRENT_SELECTION_NAME, ACTIVE_CURRENT_SELECTION);
70         addMapping(ACTIVE_EDITOR_NAME, ACTIVE_EDITOR);
71         addMapping(ACTIVE_EDITOR_ID_NAME, ACTIVE_EDITOR_ID);
72         addMapping(ACTIVE_MENU_NAME, ACTIVE_MENU);
73         addMapping(ACTIVE_MENU_SELECTION_NAME, ACTIVE_MENU);
74         addMapping(ACTIVE_MENU_EDITOR_INPUT_NAME, ACTIVE_MENU);
75         addMapping(ACTIVE_FOCUS_CONTROL_ID_NAME, ACTIVE_MENU);
76         addMapping(ACTIVE_FOCUS_CONTROL_NAME, ACTIVE_MENU);
77         addMapping(ACTIVE_PART_NAME, ACTIVE_PART);
78         addMapping(ACTIVE_PART_ID_NAME, ACTIVE_PART_ID);
79         addMapping(ACTIVE_SHELL_NAME, ACTIVE_SHELL);
80         addMapping(ACTIVE_SITE_NAME, ACTIVE_SITE);
81         addMapping(ACTIVE_WORKBENCH_WINDOW_NAME, ACTIVE_WORKBENCH_WINDOW);
82         addMapping(ACTIVE_WORKBENCH_WINDOW_SHELL_NAME,
83                 ACTIVE_WORKBENCH_WINDOW_SHELL);
84         addMapping(ACTIVE_WORKBENCH_WINDOW_IS_COOLBAR_VISIBLE_NAME,
85                 ACTIVE_WORKBENCH_WINDOW_SUBORDINATE);
86         addMapping(ACTIVE_WORKBENCH_WINDOW_IS_PERSPECTIVEBAR_VISIBLE_NAME,
87                 ACTIVE_WORKBENCH_WINDOW_SUBORDINATE);
88         addMapping(LEGACY_LEGACY_NAME, LEGACY_LEGACY);
89     }
90
91     /**
92      * Adds a mapping between a source name and a source priority. This method
93      * also cleans up any existing mappings using the same name or priority.
94      * There is a one-to-one relationship between name and priority.
95      *
96      * @param sourceName
97      * The name of the variable as it would appear in an XML
98      * expression; must not be <code>null</code>.
99      * @param sourcePriority
100      * The priority of the source with respect to other sources. A
101      * higher value means that expressions including this priority
102      * will win ties more often. It is recommended that this value is
103      * simply a single bit shifted to a particular place.
104      * @see ISources
105      */

106     public static final void addMapping(final String JavaDoc sourceName,
107             final int sourcePriority) {
108         if (sourceName == null) {
109             throw new NullPointerException JavaDoc("The source name cannot be null."); //$NON-NLS-1$
110
}
111
112         final Integer JavaDoc priority = new Integer JavaDoc(sourcePriority);
113
114         sourcePrioritiesByName.put(sourceName, priority);
115     }
116
117     /**
118      * Computes the source priority for the given expression. The source
119      * priority is a bit mask of all of the variables references by the
120      * expression. The default variable is considered to be
121      * {@link ISources#ACTIVE_CURRENT_SELECTION}. The source priority is used
122      * to minimize recomputations of the expression, and it can also be used for
123      * conflict resolution.
124      *
125      * @param expression
126      * The expression for which the source priority should be
127      * computed; may be <code>null</code>.
128      * @return The bit mask of all the sources required for this expression;
129      * <code>0</code> if none.
130      */

131     public static final int computeSourcePriority(final Expression expression) {
132         int sourcePriority = ISources.WORKBENCH;
133
134         if (expression == null) {
135             return sourcePriority;
136         }
137
138         final ExpressionInfo info = expression.computeExpressionInfo();
139
140         // Add the default variable, if any.
141
if (info.hasDefaultVariableAccess()) {
142             sourcePriority |= ISources.ACTIVE_CURRENT_SELECTION;
143         }
144
145         // Add all of the reference variables.
146
final String JavaDoc[] sourceNames = info.getAccessedVariableNames();
147         for (int i = 0; i < sourceNames.length; i++) {
148             final String JavaDoc sourceName = sourceNames[i];
149             sourcePriority |= getMapping(sourceName);
150         }
151
152         return sourcePriority;
153     }
154
155     /**
156      * Gets the priority for the source with the given name.
157      *
158      * @param sourceName
159      * The name of the variable as it would appear in an XML
160      * expression; should not be <code>null</code>.
161      * @return The source priority that matches, if any;
162      * <code>NO_SOURCE_PRIORITY</code> if none is found.
163      */

164     public static final int getMapping(final String JavaDoc sourceName) {
165         final Object JavaDoc object = sourcePrioritiesByName.get(sourceName);
166         if (object instanceof Integer JavaDoc) {
167             return ((Integer JavaDoc) object).intValue();
168         }
169
170         return NO_SOURCE_PRIORITY;
171     }
172
173     /**
174      * This class should not be instantiated.
175      */

176     private SourcePriorityNameMapping() {
177         // This class should not be instantiated.
178
}
179 }
180
Popular Tags