KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > launchConfigurations > LaunchConfigurationPresentationManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 package org.eclipse.debug.internal.ui.launchConfigurations;
12
13  
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Hashtable JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IConfigurationElement;
25 import org.eclipse.core.runtime.IExtensionPoint;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.core.runtime.Platform;
28 import org.eclipse.core.runtime.Status;
29 import org.eclipse.debug.core.DebugPlugin;
30 import org.eclipse.debug.core.ILaunchConfiguration;
31 import org.eclipse.debug.core.ILaunchConfigurationType;
32 import org.eclipse.debug.core.ILaunchDelegate;
33 import org.eclipse.debug.core.ILaunchManager;
34 import org.eclipse.debug.core.ILaunchMode;
35 import org.eclipse.debug.internal.core.IConfigurationElementConstants;
36 import org.eclipse.debug.internal.ui.DebugUIPlugin;
37 import org.eclipse.debug.internal.ui.LaunchConfigurationTabExtension;
38 import org.eclipse.debug.ui.IDebugUIConstants;
39 import org.eclipse.debug.ui.ILaunchConfigurationTabGroup;
40 import org.eclipse.ui.PlatformUI;
41 import org.eclipse.ui.activities.IWorkbenchActivitySupport;
42 import org.eclipse.ui.activities.WorkbenchActivityHelper;
43
44 import com.ibm.icu.text.MessageFormat;
45
46 /**
47  * Manages contributed launch configuration tabs
48  */

49 public class LaunchConfigurationPresentationManager {
50     
51     /**
52      * The singleton launch configuration presentation manager
53      */

54     private static LaunchConfigurationPresentationManager fgDefault;
55             
56     /**
57      * Collection of launch configuration tab group extensions
58      * defined in plug-in xml. Entries are keyed by launch
59      * configuration type identifier (<code>String</code>),
60      * and entires are tables of launch modes (<code>String</code>)
61      * to <code>LaunchConfigurationTabGroupExtension</code>. "*" is
62      * used to represent the default tab group (i.e. unspecified mode).
63      */

64     private Hashtable JavaDoc fTabGroupExtensions;
65     
66     /**
67      * contributed tabs are stored by the tab group id that they contribute to.
68      * each entry is a futher <code>Hashtable</code> consisting of the corrseponding
69      * <code>LaunchConfigurationTabExtension</code> objects for each contributed tab stored by their
70      * id
71      *
72      * @since 3.3
73      */

74     private Hashtable JavaDoc fContributedTabs;
75             
76     /**
77      * Constructs the singleton launch configuration presentation
78      * manager.
79      */

80     private LaunchConfigurationPresentationManager() {
81         fgDefault = this;
82     }
83
84     /**
85      * Returns the launch configuration presentation manager
86      */

87     public static LaunchConfigurationPresentationManager getDefault() {
88         if (fgDefault == null) {
89             fgDefault = new LaunchConfigurationPresentationManager();
90         }
91         return fgDefault;
92     }
93         
94     /**
95      * Creates launch configuration tab group extensions for each extension
96      * defined in XML, and adds them to the table of tab group extensions.
97      */

98     private void initializeTabGroupExtensions() {
99         if(fTabGroupExtensions == null) {
100             fTabGroupExtensions = new Hashtable JavaDoc();
101             IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(DebugUIPlugin.getUniqueIdentifier(), IDebugUIConstants.EXTENSION_POINT_LAUNCH_CONFIGURATION_TAB_GROUPS);
102             IConfigurationElement[] groups = extensionPoint.getConfigurationElements();
103             LaunchConfigurationTabGroupExtension group = null;
104             String JavaDoc typeId = null;
105             Map JavaDoc map = null;
106             List JavaDoc modes = null;
107             for (int i = 0; i < groups.length; i++) {
108                 group = new LaunchConfigurationTabGroupExtension(groups[i]);
109                 typeId = group.getTypeIdentifier();
110                 map = (Map JavaDoc)fTabGroupExtensions.get(typeId);
111                 if (map == null) {
112                     map = new Hashtable JavaDoc();
113                     fTabGroupExtensions.put(typeId, map);
114                 }
115                 modes = group.getModes();
116                 if(modes.isEmpty()) {
117                     map.put("*", group); //$NON-NLS-1$
118
}
119                 for(Iterator JavaDoc iter = modes.iterator(); iter.hasNext();) {
120                     map.put(iter.next(), group);
121                 }
122             }
123         }
124     }
125     
126     /**
127      * This method is used to collect all of the contributed tabs defined by the <code>launchConfigurationTabs</code>
128      * extension point
129      *
130      * @since 3.3
131      */

132     private void initializeContributedTabExtensions() {
133         fContributedTabs = new Hashtable JavaDoc();
134         IExtensionPoint epoint = Platform.getExtensionRegistry().getExtensionPoint(DebugUIPlugin.getUniqueIdentifier(), IDebugUIConstants.EXTENSION_POINT_LAUNCH_TABS);
135         IConfigurationElement[] elements = epoint.getConfigurationElements();
136         LaunchConfigurationTabExtension tab = null;
137         Hashtable JavaDoc element = null;
138         for(int i = 0; i < elements.length; i++) {
139             tab = new LaunchConfigurationTabExtension(elements[i]);
140             element = (Hashtable JavaDoc) fContributedTabs.get(tab.getTabGroupId());
141             if(element == null) {
142                 element = new Hashtable JavaDoc();
143                 element.put(tab.getIdentifier(), tab);
144                 fContributedTabs.put(tab.getTabGroupId(), element);
145             }
146             element.put(tab.getIdentifier(), tab);
147         }
148     }
149     
150     /**
151      * Returns the tab group for the given launch configuration type and mode.
152      *
153      * @param type launch configuration type
154      * @param mode launch mode
155      * @return the tab group for the given type of launch configuration, or <code>null</code> if none
156      * @exception CoreException if an exception occurs creating the group
157      */

158     public ILaunchConfigurationTabGroup getTabGroup(ILaunchConfigurationType type, String JavaDoc mode) throws CoreException {
159         HashSet JavaDoc modes = new HashSet JavaDoc();
160         modes.add(mode);
161         LaunchConfigurationTabGroupExtension ext = getExtension(type.getIdentifier(), modes);
162         if (ext == null) {
163             IStatus status = new Status(IStatus.ERROR, IDebugUIConstants.PLUGIN_ID, IDebugUIConstants.INTERNAL_ERROR,
164              MessageFormat.format(LaunchConfigurationsMessages.LaunchConfigurationPresentationManager_No_tab_group_defined_for_launch_configuration_type__0__3, (new String JavaDoc[] {type.getIdentifier()})), null);
165              throw new CoreException(status);
166         }
167         return new LaunchConfigurationTabGroupWrapper(ext.newTabGroup(), ext.getIdentifier(), null);
168     }
169     
170     /**
171      * Returns the tab group for the given launch configutation and the mode the dialog opened in
172      * @param type the type of the configuration
173      * @param config
174      * @param mode
175      * @return
176      * @throws CoreException
177      */

178     public ILaunchConfigurationTabGroup getTabGroup(ILaunchConfiguration config, String JavaDoc mode) throws CoreException {
179         HashSet JavaDoc modes = new HashSet JavaDoc();
180         modes.add(mode);
181         LaunchConfigurationTabGroupExtension ext = getExtension(config.getType().getIdentifier(), modes);
182         if (ext == null) {
183             IStatus status = new Status(IStatus.ERROR, IDebugUIConstants.PLUGIN_ID, IDebugUIConstants.INTERNAL_ERROR,
184              MessageFormat.format(LaunchConfigurationsMessages.LaunchConfigurationPresentationManager_No_tab_group_defined_for_launch_configuration_type__0__3, (new String JavaDoc[] {config.getType().getIdentifier()})), null);
185              throw new CoreException(status);
186         }
187         return new LaunchConfigurationTabGroupWrapper(ext.newTabGroup(), ext.getIdentifier(), config);
188     }
189     
190     /**
191      * Returns the proxy elements for all contributed tabs for the specified tab group id
192      * @param groupid the id of the tab group
193      * @param config the config the tab group is opened on
194      * @param mode the mode the associated launch dialog is opened on
195      * @return the listing of all of the tab extensions or an empty array, never <code>null</code>
196      *
197      * @since 3.3
198      */

199     protected LaunchConfigurationTabExtension[] getTabExtensions(String JavaDoc groupid, ILaunchConfiguration config, String JavaDoc mode) throws CoreException {
200         initializeContributedTabExtensions();
201         Hashtable JavaDoc tabs = (Hashtable JavaDoc) fContributedTabs.get(groupid);
202         if(tabs != null) {
203             return filterLaunchTabExtensions((LaunchConfigurationTabExtension[]) tabs.values().toArray(new LaunchConfigurationTabExtension[tabs.size()]), config, mode);
204         }
205         return new LaunchConfigurationTabExtension[0];
206     }
207     
208     /**
209      * Returns a listing of <code>LaunchConfiguraitonTabExtension</code>s that does not contain any tabs
210      * from disabled activities
211      * <p>
212      * There are thre ways that tabs can be filtered form the launch dialog:
213      * <ol>
214      * <li>The tabs can belong to tooling that is contributed via a specific type of workbench activity, and is therefore filtered with capabilities</li>
215      * <li>The tabs can be filtered via the associatedDelegate extension point, if a tab is said to apply only to certain tooling, only show it in the instance when that tooling is used</li>
216      * <li>A tab is not part of a workbench activity, nor specifies an associated launch delegate -- show the tab</li>
217      * </ol>
218      * </p>
219      * @param tabs the raw listing of tabs to filter
220      * @return the listing of filtered <code>LaunchConfigurationTabExtension</code>s or an empty array, never <code>null</code>
221      *
222      * @since 3.3
223      */

224     protected LaunchConfigurationTabExtension[] filterLaunchTabExtensions(LaunchConfigurationTabExtension[] tabs, ILaunchConfiguration config, String JavaDoc mode) throws CoreException {
225         IWorkbenchActivitySupport as = PlatformUI.getWorkbench().getActivitySupport();
226         if(as == null || config == null) {
227             return tabs;
228         }
229         HashSet JavaDoc set = new HashSet JavaDoc();
230         for(int i = 0; i < tabs.length; i ++) {
231         //filter capabilities
232
if(!WorkbenchActivityHelper.filterItem(new LaunchTabContribution(tabs[i]))) {
233             //filter to preferred delegate (if there is one)
234
HashSet JavaDoc modes = (HashSet JavaDoc) config.getModes();
235                 modes.add(mode);
236                 ILaunchDelegate delegate = config.getPreferredDelegate(modes);
237                 if(delegate == null) {
238                     delegate = config.getType().getPreferredDelegate(modes);
239                 }
240                 Set JavaDoc delegateSet = tabs[i].getDelegateSet();
241                 if(delegate != null) {
242                     if(delegateSet.isEmpty() || delegateSet.contains(delegate.getId())) {
243                         set.add(tabs[i]);
244                     }
245                 }
246                 else {
247                     //otherwise filter based on the collection of delegates for the modes
248
ILaunchDelegate[] delegates = config.getType().getDelegates(modes);
249                     for(int j = 0; j < delegates.length; j++) {
250                         if(delegateSet.size() == 0 || delegateSet.contains(delegates[j].getId())) {
251                             //associated with all modes and tab groups or only specific ones if indicated
252
set.add(tabs[i]);
253                         }
254                     }
255                 }
256             }
257         }
258         return (LaunchConfigurationTabExtension[]) set.toArray(new LaunchConfigurationTabExtension[set.size()]);
259     }
260     
261     /**
262      * Returns the launch tab group extension for the given type and mode, or
263      * <code>null</code> if none
264      *
265      * @param type launch configuration type identifier
266      * @param mode launch mode identifier
267      * @return launch tab group extension or <code>null</code>
268      */

269     protected LaunchConfigurationTabGroupExtension getExtension(String JavaDoc type, Set JavaDoc modes) {
270         initializeTabGroupExtensions();
271         Map JavaDoc map = (Map JavaDoc)fTabGroupExtensions.get(type);
272         if (map != null) {
273             Object JavaDoc extension = map.get(modes);
274             if (extension == null) {
275                 // get the default tabs
276
extension = map.get("*"); //$NON-NLS-1$
277
}
278             return (LaunchConfigurationTabGroupExtension)extension;
279         }
280         return null;
281     }
282     
283     /**
284      * Returns the identifier of the help context that is associated with the
285      * specified launch configuration type and mode, or <code>null</code> if none.
286      *
287      * @param type launch config type
288      * @param mode launch mode
289      * @return the identifier for the help context associated with the given
290      * type of launch configuration, or <code>null</code>
291      * @exception CoreException if an exception occurs creating the group
292      * @since 2.1
293      */

294     public String JavaDoc getHelpContext(ILaunchConfigurationType type, String JavaDoc mode) throws CoreException {
295         HashSet JavaDoc modes = new HashSet JavaDoc();
296         modes.add(mode);
297         LaunchConfigurationTabGroupExtension ext = getExtension(type.getIdentifier(), modes);
298         if (ext == null) {
299             IStatus status = new Status(IStatus.ERROR, IDebugUIConstants.PLUGIN_ID, IDebugUIConstants.INTERNAL_ERROR,
300              MessageFormat.format(LaunchConfigurationsMessages.LaunchConfigurationPresentationManager_No_tab_group_defined_for_launch_configuration_type__0__3, (new String JavaDoc[] {type.getIdentifier()})), null);
301              throw new CoreException(status);
302         }
303         return ext.getHelpContextId();
304     }
305     
306     /**
307      * Returns the description of the given configuration type
308      * in the specified mode or <code>null</code> if none.
309      *
310      * @param configType the config type
311      * @param mode the launch mode
312      * @return the description of the given configuration type, possible <code>null</code>
313      */

314     public String JavaDoc getDescription(ILaunchConfigurationType configType, String JavaDoc mode) {
315         HashSet JavaDoc modes = new HashSet JavaDoc();
316         modes.add(mode);
317         LaunchConfigurationTabGroupExtension extension = getExtension(configType.getAttribute(IConfigurationElementConstants.ID), modes);
318         return (extension != null ? extension.getDescription(modes) : null);
319     }
320     
321     /**
322      * Returns a sorted list of launch mode names corresponding to the given identifiers.
323      *
324      * @param modes set of launch mode identifiers
325      * @return sorted list of launch mode names
326      */

327     public List JavaDoc getLaunchModeNames(Set JavaDoc modes) {
328         List JavaDoc names = new ArrayList JavaDoc();
329         Iterator JavaDoc iterator = modes.iterator();
330         ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
331         while (iterator.hasNext()) {
332             String JavaDoc id = (String JavaDoc) iterator.next();
333             ILaunchMode mode = manager.getLaunchMode(id);
334             if (mode == null) {
335                 names.add(id);
336             } else {
337                 names.add(DebugUIPlugin.removeAccelerators(mode.getLabel()));
338             }
339         }
340         Collections.sort(names);
341         return names;
342     }
343     
344     /**
345      * Returns the label of the mode id with all accelerators removed
346      * @param modeid the id of the mode i.e. 'run'
347      * @return the formatted label of the specified mode id with all accelerators removed, or <code>null</code> if no label is available
348      * @since 3.3
349      */

350     public String JavaDoc getLaunchModeLabel(String JavaDoc modeid) {
351         String JavaDoc mode = null;
352         ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
353         ILaunchMode lmode = manager.getLaunchMode(modeid);
354         if(lmode != null) {
355             return lmode.getLabel();
356         }
357         return mode;
358     }
359     
360 }
361
362
Popular Tags