KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > contextlaunching > ContextRunner


1 /*******************************************************************************
2  * Copyright (c) 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.contextlaunching;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.ListIterator JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.eclipse.core.expressions.EvaluationContext;
20 import org.eclipse.core.expressions.IEvaluationContext;
21 import org.eclipse.core.resources.IProject;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.debug.core.DebugPlugin;
25 import org.eclipse.debug.core.ILaunchConfiguration;
26 import org.eclipse.debug.core.ILaunchMode;
27 import org.eclipse.debug.internal.ui.DebugUIPlugin;
28 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
29 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationManager;
30 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationSelectionDialog;
31 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchShortcutExtension;
32 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchShortcutSelectionDialog;
33 import org.eclipse.debug.internal.ui.stringsubstitution.SelectedResourceManager;
34 import org.eclipse.debug.ui.DebugUITools;
35 import org.eclipse.debug.ui.ILaunchGroup;
36 import org.eclipse.jface.dialogs.IDialogConstants;
37 import org.eclipse.jface.dialogs.MessageDialog;
38 import org.eclipse.jface.viewers.StructuredSelection;
39 import org.eclipse.jface.window.Window;
40 import org.eclipse.ui.activities.WorkbenchActivityHelper;
41
42 import com.ibm.icu.text.MessageFormat;
43
44 /**
45  * Static runner for context launching to provide the base capability of context
46  * launching to more than one form of action (drop down, toolbar, view, etc)
47  *
48  * @see org.eclipse.debug.ui.actions.AbstractLaunchHistoryAction
49  * @see org.eclipse.debug.ui.actions.LaunchShortcutsAction
50  * @see org.eclipse.debug.ui.actions.ContextualLaunchAction
51  * @see org.eclipse.debug.internal.ui.preferences.ContextLaunchingPreferencePage
52  *
53  * @since 3.3
54  * CONTEXTLAUNCHING
55  */

56 public final class ContextRunner {
57     
58     /**
59      * The singleton instance of the context runner
60      */

61     private static ContextRunner fgInstance = null;
62     
63     /**
64      * Returns the singleton instance of <code>ContextRunner</code>
65      * @return the singleton instance of <code>ContextRunner</code>
66      */

67     public static ContextRunner getDefault() {
68         if(fgInstance == null) {
69             fgInstance = new ContextRunner();
70         }
71         return fgInstance;
72     }
73     
74     /**
75      * Performs the context launching given the object context and the mode to launch in.
76      * @param group
77      */

78     public void launch(ILaunchGroup group) {
79         IResource resource = SelectedResourceManager.getDefault().getSelectedResource();
80         //1. resolve resource
81
if(resource != null) {
82             selectAndLaunch(resource, group);
83             return;
84         }
85         //2. launch last if no resource
86
if(!launchLast(group)) {
87             //3. might be empty workspace try to get shortcuts
88
List JavaDoc shortcuts = getLaunchShortcutsForEmptySelection();
89             if(!shortcuts.isEmpty()) {
90                 showShortcutSelectionDialog(resource, shortcuts, group.getMode());
91             }
92             else {
93                 MessageDialog.openInformation(DebugUIPlugin.getShell(), ContextMessages.ContextRunner_0, ContextMessages.ContextRunner_7);
94             }
95         }
96     }
97     
98     /**
99      * This method launches the last configuration that was launched, if any.
100      * @param group the launch group to launch with
101      * @return true if there was a last launch and it was launched, false otherwise
102      */

103     protected boolean launchLast(ILaunchGroup group) {
104         ILaunchConfiguration config = null;
105         if(group != null) {
106             config = DebugUIPlugin.getDefault().getLaunchConfigurationManager().getFilteredLastLaunch(group.getIdentifier());
107         }
108         if(config != null) {
109             launch(config, group.getMode());
110             return true;
111         }
112         return false;
113     }
114     
115     /**
116      * Creates a listing of the launch shortcut extensions that are applicable to the underlying resource
117      * @param resource the underlying resource
118      * @return a listing of applicable launch shortcuts or an empty list, never <code>null</code>
119      */

120     public List JavaDoc getLaunchShortcutsForEmptySelection() {
121         List JavaDoc list = new ArrayList JavaDoc();
122         List JavaDoc sc = getLaunchConfigurationManager().getLaunchShortcuts();
123         List JavaDoc ctxt = new ArrayList JavaDoc();
124         IEvaluationContext context = new EvaluationContext(null, ctxt);
125         context.addVariable("selection", ctxt); //$NON-NLS-1$
126
LaunchShortcutExtension ext = null;
127         for(Iterator JavaDoc iter = sc.iterator(); iter.hasNext();) {
128             ext = (LaunchShortcutExtension) iter.next();
129             try {
130                 if(ext.evalEnablementExpression(context, ext.getContextualLaunchEnablementExpression()) && !WorkbenchActivityHelper.filterItem(ext)) {
131                     if(!list.contains(ext)) {
132                         list.add(ext);
133                     }
134                 }
135             }
136             catch(CoreException ce) {/*do nothing*/}
137         }
138         return list;
139     }
140     
141     /**
142      * Prompts the user to select a way of launching the current resource, where a 'way'
143      * is defined as a launch shortcut.
144      *
145      * @param resource
146      * @param group
147      */

148     protected void selectAndLaunch(IResource resource, ILaunchGroup group) {
149         if(group == null) {
150             return;
151         }
152         ILaunchConfiguration config = getLaunchConfigurationManager().isSharedConfig(resource);
153         if(config != null) {
154             launch(config, group.getMode());
155             return;
156         }
157         List JavaDoc configs = getLaunchConfigurationManager().getApplicableLaunchConfigurations(resource);
158         configs = getConfigsApplicableToMode(configs, group.getMode());
159         int csize = configs.size();
160         if(csize == 1) {
161             launch((ILaunchConfiguration) configs.get(0), group.getMode());
162             return;
163         }
164         if(csize < 1) {
165             List JavaDoc exts = getLaunchConfigurationManager().getLaunchShortcuts(resource);
166             List JavaDoc modeSpecificExts = getLaunchConfigurationManager().getShortcutsSupportingMode(exts, group.getMode());
167             int esize = modeSpecificExts.size();
168             if(esize == 1) {
169                 LaunchShortcutExtension ext = (LaunchShortcutExtension) modeSpecificExts.get(0);
170                 ext.launch(new StructuredSelection(resource), group.getMode());
171                 return;
172             }
173             if(esize > 1) {
174                 showShortcutSelectionDialog(resource, modeSpecificExts, group.getMode());
175                 return;
176             }
177             if(esize < 1) {
178                 if(DebugUIPlugin.getDefault().getPreferenceStore().getBoolean(IInternalDebugUIConstants.PREF_LAUNCH_LAST_IF_NOT_LAUNCHABLE)) {
179                     if(!launchLast(group)) {
180                         MessageDialog.openInformation(DebugUIPlugin.getShell(), ContextMessages.ContextRunner_0, ContextMessages.ContextRunner_7);
181                     }
182                     return;
183                 }
184                 if (exts.size() > 0) {
185                     // there are shortcuts, but not applicable to the selected mode
186
ILaunchMode launchMode = DebugPlugin.getDefault().getLaunchManager().getLaunchMode(group.getMode());
187                     if (launchMode == null) {
188                         DebugUIPlugin.logErrorMessage("Unsupported launch mode: " + group.getMode()); //$NON-NLS-1$
189
} else {
190                         String JavaDoc label = launchMode.getLabel();
191                         String JavaDoc modeLabel = DebugUIPlugin.removeAccelerators(label);
192                         MessageDialog.openInformation(DebugUIPlugin.getShell(), MessageFormat.format(ContextMessages.ContextRunner_1, new String JavaDoc[]{modeLabel}),
193                                 MessageFormat.format(ContextMessages.ContextRunner_2, new String JavaDoc[]{modeLabel.toLowerCase()}));
194                     }
195                 } else {
196                     IProject project = resource.getProject();
197                     if(project != null && !project.equals(resource)) {
198                         selectAndLaunch(project, group);
199                     }
200                     else {
201                         String JavaDoc msg = ContextMessages.ContextRunner_7;
202                         if(!resource.isAccessible()) {
203                             msg = MessageFormat.format(ContextMessages.ContextRunner_13, new String JavaDoc[] {resource.getName()});
204                         }
205                         MessageDialog.openInformation(DebugUIPlugin.getShell(), ContextMessages.ContextRunner_0, msg);
206                     }
207                 }
208             }
209         }
210         else if(csize > 1){
211             config = getLaunchConfigurationManager().getMRUConfiguration(configs, group, resource);
212             if(config != null) {
213                 launch(config, group.getMode());
214             } else {
215                 showConfigurationSelectionDialog(configs, group.getMode());
216             }
217         }
218     }
219     
220     /**
221      * Validates the given launch mode and launches.
222      *
223      * @param configuration configuration to launch
224      * @param mode launch mode identifier
225      */

226     private void launch(ILaunchConfiguration configuration, String JavaDoc mode) {
227         if (validateMode(configuration, mode)) {
228             DebugUITools.launch(configuration, mode);
229         }
230     }
231     
232     /**
233      * Validates the given launch mode is supported, and returns whether to continue with
234      * the launch.
235      *
236      * @param configuration launch configuration
237      * @param mode launch mode
238      * @return whether the mode is supported
239      */

240     private boolean validateMode(ILaunchConfiguration configuration, String JavaDoc mode) {
241         try {
242             // if this is a multi-mode launch, allow the launch dialog to be opened
243
// to resolve a valid mode, if needed.
244
if (configuration.getModes().isEmpty()) {
245                 if (!configuration.supportsMode(mode)) {
246                     ILaunchMode launchMode = DebugPlugin.getDefault().getLaunchManager().getLaunchMode(mode);
247                     if (launchMode == null) {
248                         DebugUIPlugin.logErrorMessage("Unsupported launch mode: " + mode); //$NON-NLS-1$
249
} else {
250                         String JavaDoc label = launchMode.getLabel();
251                         String JavaDoc modeLabel = DebugUIPlugin.removeAccelerators(label);
252                         MessageDialog.openInformation(DebugUIPlugin.getShell(), MessageFormat.format(ContextMessages.ContextRunner_1, new String JavaDoc[]{modeLabel}),
253                                 MessageFormat.format(ContextMessages.ContextRunner_3, new String JavaDoc[]{configuration.getName(), modeLabel.toLowerCase()}));
254                     }
255                     return false;
256                 }
257             }
258         } catch (CoreException e) {
259             DebugUIPlugin.log(e.getStatus());
260             return false;
261         }
262         return true;
263     }
264     
265     /**
266      * Presents the user with a dialog to pick the launch configuration to launch
267      * and launches that configuration.
268      *
269      * @param configurations the listing of applicable configurations to present
270      * @param mode the mode
271      */

272     protected void showConfigurationSelectionDialog(List JavaDoc configurations, String JavaDoc mode) {
273         LaunchConfigurationSelectionDialog lsd = new LaunchConfigurationSelectionDialog(DebugUIPlugin.getShell());
274         if(configurations != null) {
275             lsd.setInput(configurations);
276         }
277         if(lsd.open() == IDialogConstants.OK_ID) {
278             ILaunchConfiguration config = (ILaunchConfiguration) lsd.getResult()[0];
279             launch(config, mode);
280         }
281     }
282     
283     /**
284      * Presents a selection dialog to the user to pick a launch shortcut and
285      * launch using that shortcut.
286      *
287      * @param resource the resource context
288      * @param mode the mode
289      */

290     protected void showShortcutSelectionDialog(IResource resource, List JavaDoc shortcuts, String JavaDoc mode) {
291         LaunchShortcutSelectionDialog dialog = new LaunchShortcutSelectionDialog(resource, mode);
292         dialog.setInput(shortcuts);
293         if (dialog.open() == Window.OK) {
294             Object JavaDoc[] result = dialog.getResult();
295             if(result.length > 0) {
296                 LaunchShortcutExtension method = (LaunchShortcutExtension) result[0];
297                 if(method != null) {
298                     method.launch((resource == null ? new StructuredSelection() : new StructuredSelection(resource)), mode);
299                 }
300             }
301         }
302     }
303     
304     /**
305      * Returns the launch configuration manager
306      * @return the launch configuration manager
307      */

308     protected LaunchConfigurationManager getLaunchConfigurationManager() {
309         return DebugUIPlugin.getDefault().getLaunchConfigurationManager();
310     }
311     
312     /**
313      * Filters the given list of launch configurations that apply to the specified mode
314      * @param configs the list of configs to filter
315      * @param mode the mode we want to filter against
316      * @return the listing of launch configurations that support the specified mode
317      */

318     private List JavaDoc getConfigsApplicableToMode(List JavaDoc configs, String JavaDoc mode) {
319         ArrayList JavaDoc applicable = new ArrayList JavaDoc(configs);
320         ListIterator JavaDoc iterator = applicable.listIterator();
321         while (iterator.hasNext()) {
322             ILaunchConfiguration config = (ILaunchConfiguration) iterator.next();
323             try {
324                 Set JavaDoc modes = config.getModes();
325                 modes.add(mode);
326                 if (!config.getType().supportsModeCombination(modes)) {
327                     iterator.remove();
328                 }
329             }
330             catch (CoreException e) {}
331         }
332         return applicable;
333     }
334 }
335
Popular Tags