KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > menus > TrimAdditionCacheEntry


1 /*******************************************************************************
2  * Copyright (c) 2006 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.menus;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.core.expressions.Expression;
21 import org.eclipse.core.runtime.IConfigurationElement;
22 import org.eclipse.core.runtime.IExtensionRegistry;
23 import org.eclipse.core.runtime.Platform;
24 import org.eclipse.jface.action.IContributionItem;
25 import org.eclipse.jface.menus.IWidget;
26 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
27 import org.eclipse.ui.internal.util.Util;
28 import org.eclipse.ui.menus.IMenuService;
29 import org.eclipse.ui.menus.IWorkbenchWidget;
30
31 /**
32  * Handles the top level caching for 3.2 style trim
33  * contributions.
34  *
35  * @since 3.3
36  *
37  */

38 public class TrimAdditionCacheEntry {
39     private IConfigurationElement additionElement;
40     private MenuLocationURI uri = null;
41     
42     /**
43      * The map contains {@link IWorkbenchWidget} entries
44      * for widgets that have failed to load on a previous
45      * attempt. Used to prevent multiple retries at
46      * loading a widget (which spams the log).
47      */

48     private Map JavaDoc failedWidgets = new HashMap JavaDoc();
49     /**
50      * Maps the widget back to it's configurtation element
51      */

52     private Map JavaDoc widgetToConfigElementMap = new HashMap JavaDoc();
53     
54
55     // Caches
56

57     /**
58      * Maps an IContributionItem to its corresponding IConfigurationElement
59      */

60     Map JavaDoc iciToConfigElementMap = new HashMap JavaDoc();
61
62     public TrimAdditionCacheEntry(IConfigurationElement element,
63             MenuLocationURI uri, IMenuService service) {
64         this.additionElement = element;
65         this.uri = uri;
66     }
67
68     /**
69      * Populate the list
70      *
71      * @param additions
72      */

73     public void getContributionItems(List JavaDoc additions) {
74         additions.clear();
75
76     }
77
78     /* (non-Javadoc)
79      * @see org.eclipse.ui.internal.menus.MenuCacheEntry#generateSubCaches()
80      */

81     public void generateSubCaches() {
82         // TODO Auto-generated method stub
83

84     }
85
86     /* (non-Javadoc)
87      * @see org.eclipse.ui.internal.menus.MenuCacheEntry#getVisibleWhenForItem(org.eclipse.jface.action.IContributionItem)
88      */

89     public Expression getVisibleWhenForItem(IContributionItem item) {
90         // TODO Auto-generated method stub
91
return null;
92     }
93
94     /**
95      * @return
96      */

97     public String JavaDoc getId() {
98         return additionElement.getAttribute(IWorkbenchRegistryConstants.ATT_ID);
99     }
100
101     /**
102      * @return <code>true</code> iff the group is positioned at the start
103      * (or 'before') the entry that it is relative to. Default is true
104      *
105      */

106     public boolean isAtStart() {
107         IConfigurationElement location = additionElement.getChildren(IWorkbenchRegistryConstants.TAG_LOCATION)[0];
108         if (location.getChildren(IWorkbenchRegistryConstants.TAG_ORDER).length > 0) {
109             IConfigurationElement order = location.getChildren(IWorkbenchRegistryConstants.TAG_ORDER)[0];
110             
111             String JavaDoc pos = order.getAttribute(IWorkbenchRegistryConstants.ATT_POSITION);
112             if (pos != null)
113                 return (pos.equals("start") | pos.equals("before")); //$NON-NLS-1$//$NON-NLS-2$
114
}
115         return true;
116     }
117
118     /**
119      * Returns whether or not the defining {@link IConfigurationElement}
120      * declares that the widget should use extra space in the 'major'
121      * dimension (ie. use extra horizontal space in the status area).
122      * The space is equally divided with other elementa in the same
123      * trim area that also want to use the extra space.
124      *
125      * @param widgetElement the {@link IConfigurationElement} declaring this
126      * widget.
127      *
128      * @return <code>true</code> iff the resulting widget should use
129      * extra major space
130      */

131     public boolean fillMajor(IConfigurationElement widgetElement) {
132         if (widgetElement.getChildren(IWorkbenchRegistryConstants.TAG_LAYOUT).length==0) {
133             return false;
134         }
135         IConfigurationElement layout = widgetElement.getChildren(IWorkbenchRegistryConstants.TAG_LAYOUT)[0];
136         String JavaDoc fillMajorVal = layout.getAttribute(IWorkbenchRegistryConstants.ATT_FILL_MAJOR);
137
138         return (fillMajorVal != null && fillMajorVal.equals("true")); //$NON-NLS-1$
139
}
140
141     /**
142      * Returns whether or not the defining {@link IConfigurationElement}
143      * declares that the widget should use extra space in the 'minor'
144      * dimension (ie. use extra vertical space in the status area)
145      *
146      * @param widgetElement the {@link IConfigurationElement} declaring this
147      * widget.
148      *
149      * @return <code>true</code> iff the resulting widget should use
150      * extra minor space
151      */

152     public boolean fillMinor(IConfigurationElement widgetElement) {
153         if (widgetElement.getChildren(IWorkbenchRegistryConstants.TAG_LAYOUT).length==0) {
154             return false;
155         }
156         IConfigurationElement layout = widgetElement.getChildren(IWorkbenchRegistryConstants.TAG_LAYOUT)[0];
157         String JavaDoc fillMinorVal = layout.getAttribute(IWorkbenchRegistryConstants.ATT_FILL_MINOR);
158
159         return (fillMinorVal != null && fillMinorVal.equals("true")); //$NON-NLS-1$
160
}
161
162     /**
163      * @return The list of IConfigurationElements representing
164      * widgets to be added into this 'group'
165      */

166     private List JavaDoc getWidgetConfigs() {
167         List JavaDoc widgetConfigs = new ArrayList JavaDoc();
168         
169         // Return to the 'root' of the config tree and gather all elements
170
// for this 'group'. Note that while this is sub-optimal
171
// performace-wise that there are expected to be -very-
172
// few contributions in total (i.e. 10's, not 100's)
173
final IExtensionRegistry registry = Platform.getExtensionRegistry();
174         final IConfigurationElement[] widgetElements = registry
175                 .getConfigurationElementsFor(IWorkbenchRegistryConstants.EXTENSION_MENUS);
176
177         // Locate all 'widget' additions appropriate for -this- group
178
for (int i = 0; i < widgetElements.length; i++) {
179             // Only process 'widget' entries
180
if (!IWorkbenchRegistryConstants.TAG_WIDGET.equals(widgetElements[i].getName()))
181                 continue;
182             
183             // Define the initial URI spec
184
if (widgetElements[i].getChildren(IWorkbenchRegistryConstants.TAG_LOCATION).length > 0) {
185                 IConfigurationElement location = widgetElements[i].getChildren(IWorkbenchRegistryConstants.TAG_LOCATION)[0];
186                 if (location.getChildren(IWorkbenchRegistryConstants.TAG_BAR).length > 0) {
187                     IConfigurationElement bar = location.getChildren(IWorkbenchRegistryConstants.TAG_BAR)[0];
188
189                     // The bar's path represents the 'group' it should go into
190
String JavaDoc path = bar.getAttribute(IWorkbenchRegistryConstants.ATT_PATH);
191                     if (path != null && path.equals(getId()))
192                             widgetConfigs.add(widgetElements[i]);
193                 }
194             }
195         }
196         
197         return widgetConfigs;
198     }
199     
200     /**
201      * Attempts to load -all- widgets for this entry and
202      * keeps track of the successful loads only. Only elements
203      * who can be successfully loaded will be seen by the
204      * builder.
205      *
206      * @return The list of <code>IWorkbenchWidget</code> entries
207      * that have been successfully loaded
208      */

209     public List JavaDoc getWidgets() {
210         List JavaDoc loadedWidgets = new ArrayList JavaDoc();
211         
212         // Get the widget config elements for this 'group'
213
List JavaDoc widgetConfigs = getWidgetConfigs();
214         for (Iterator JavaDoc iterator = widgetConfigs.iterator(); iterator
215                 .hasNext();) {
216             IConfigurationElement widgetCE = (IConfigurationElement) iterator.next();
217             
218             // skip elements that are known to fail
219
if (failedWidgets.containsKey(widgetCE))
220                 continue;
221             
222             IWorkbenchWidget loadedWidget = loadWidget(widgetCE);
223
224             // Either add it to the 'valid' list or mark it
225
// as failed
226
if (loadedWidget != null) {
227                 loadedWidgets.add(loadedWidget);
228                 widgetToConfigElementMap.put(loadedWidget, widgetCE);
229             }
230             else
231                 failedWidgets.put(widgetCE, widgetCE);
232         }
233         
234         return loadedWidgets;
235     }
236
237     /**
238      * Attempts to load the executable extension defined within the given
239      * configuration element. An error is logged for any widget that fails
240      * to load.
241      *
242      * @param widgetCE The {@link IConfigurationElement} containing the
243      * widget's 'class' specification.
244      *
245      * @return The loaded {@link IWorkbenchWidget} or <code>null</code>
246      * if the loading fails
247      */

248     private IWorkbenchWidget loadWidget(IConfigurationElement widgetCE) {
249         return (IWorkbenchWidget) Util.safeLoadExecutableExtension(widgetCE,
250                     IWorkbenchRegistryConstants.ATT_CLASS,
251                     IWorkbenchWidget.class);
252     }
253
254     /**
255      * @param widget The {@link IWorkbenchWidget} to get the defining configuration
256      * element for.
257      *
258      * @return The defining {@link IConfigurationElement}
259      */

260     public IConfigurationElement getElement(IWorkbenchWidget widget) {
261         return (IConfigurationElement) widgetToConfigElementMap.get(widget);
262     }
263
264     /**
265      * @param widget
266      */

267     public void removeWidget(IWidget widget) {
268         widgetToConfigElementMap.remove(widget);
269     }
270     
271     public MenuLocationURI getUri() {
272         return uri;
273     }
274 }
275
Popular Tags