KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > launcher > JavaLaunchShortcut


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 package org.eclipse.jdt.internal.debug.ui.launcher;
12
13  
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.debug.core.DebugPlugin;
20 import org.eclipse.debug.core.ILaunchConfiguration;
21 import org.eclipse.debug.core.ILaunchConfigurationType;
22 import org.eclipse.debug.core.ILaunchManager;
23 import org.eclipse.debug.ui.DebugUITools;
24 import org.eclipse.debug.ui.IDebugModelPresentation;
25 import org.eclipse.debug.ui.ILaunchShortcut;
26 import org.eclipse.jdt.core.IJavaElement;
27 import org.eclipse.jdt.core.IType;
28 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
29 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
30 import org.eclipse.jface.dialogs.MessageDialog;
31 import org.eclipse.jface.operation.IRunnableContext;
32 import org.eclipse.jface.viewers.ISelection;
33 import org.eclipse.jface.viewers.IStructuredSelection;
34 import org.eclipse.jface.window.Window;
35 import org.eclipse.swt.widgets.Shell;
36 import org.eclipse.ui.IEditorInput;
37 import org.eclipse.ui.IEditorPart;
38 import org.eclipse.ui.PlatformUI;
39 import org.eclipse.ui.dialogs.ElementListSelectionDialog;
40
41 /**
42  * Common behavior for Java launch shortcuts
43  *
44  * @since 3.2
45  */

46 public abstract class JavaLaunchShortcut implements ILaunchShortcut {
47     
48     /**
49      * @param search the java elements to search for a main type
50      * @param mode the mode to launch in
51      * @param editor activated on an editor (or from a selection in a viewer)
52      */

53     public void searchAndLaunch(Object JavaDoc[] search, String JavaDoc mode, String JavaDoc selectMessage, String JavaDoc emptyMessage) {
54         IType[] types = null;
55         try {
56             types = findTypes(search, PlatformUI.getWorkbench().getProgressService());
57         }
58         catch (InterruptedException JavaDoc e) {return;}
59         catch (CoreException e) {
60             MessageDialog.openError(getShell(), LauncherMessages.JavaLaunchShortcut_0, e.getMessage());
61             return;
62         }
63         IType type = null;
64         if (types.length == 0) {
65             MessageDialog.openError(getShell(), LauncherMessages.JavaLaunchShortcut_1, emptyMessage);
66         }
67         else if (types.length > 1) {
68             type = chooseType(types, selectMessage);
69         }
70         else {
71             type = types[0];
72         }
73         if (type != null) {
74             launch(type, mode);
75         }
76     }
77     
78     /**
79      * Finds and returns the launchable types in the given selection of elements.
80      *
81      * @param elements scope to search for launchable types
82      * @param context progress reporting context
83      * @return launchable types, possibly empty
84      * @exception InterruptedException if the search is canceled
85      * @exception org.eclipse.core.runtime.CoreException if the search fails
86      */

87     protected abstract IType[] findTypes(Object JavaDoc[] elements, IRunnableContext context) throws InterruptedException JavaDoc, CoreException;
88
89     /**
90      * Prompts the user to select a type from the given types.
91      *
92      * @param types the types to choose from
93      * @param title the selection dialog title
94      *
95      * @return the selected type or <code>null</code> if none.
96      */

97     protected IType chooseType(IType[] types, String JavaDoc title) {
98         DebugTypeSelectionDialog mmsd = new DebugTypeSelectionDialog(JDIDebugUIPlugin.getShell(), types, title);
99         if (mmsd.open() == Window.OK) {
100             return (IType)mmsd.getResult()[0];
101         }
102         return null;
103     }
104     
105     /**
106      * Launches a configuration for the given type
107      */

108     protected void launch(IType type, String JavaDoc mode) {
109         ILaunchConfiguration config = findLaunchConfiguration(type, getConfigurationType());
110         if (config != null) {
111             DebugUITools.launch(config, mode);
112         }
113     }
114     
115     /**
116      * Returns the type of configuration this shortcut is applicable to.
117      *
118      * @return the type of configuration this shortcut is applicable to
119      */

120     protected abstract ILaunchConfigurationType getConfigurationType();
121     
122     /**
123      * Locate a configuration to relaunch for the given type. If one cannot be found, create one.
124      *
125      * @return a re-usable config or <code>null</code> if none
126      */

127     protected ILaunchConfiguration findLaunchConfiguration(IType type, ILaunchConfigurationType configType) {
128         List JavaDoc candidateConfigs = Collections.EMPTY_LIST;
129         try {
130             ILaunchConfiguration[] configs = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations(configType);
131             candidateConfigs = new ArrayList JavaDoc(configs.length);
132             for (int i = 0; i < configs.length; i++) {
133                 ILaunchConfiguration config = configs[i];
134                 if (config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, "").equals(type.getFullyQualifiedName())) { //$NON-NLS-1$
135
if (config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, "").equals(type.getJavaProject().getElementName())) { //$NON-NLS-1$
136
candidateConfigs.add(config);
137                     }
138                 }
139             }
140         } catch (CoreException e) {
141             JDIDebugUIPlugin.log(e);
142         }
143         
144         // If there are no existing configs associated with the IType, create one.
145
// If there is exactly one config associated with the IType, return it.
146
// Otherwise, if there is more than one config associated with the IType, prompt the
147
// user to choose one.
148
int candidateCount = candidateConfigs.size();
149         if (candidateCount < 1) {
150             return createConfiguration(type);
151         } else if (candidateCount == 1) {
152             return (ILaunchConfiguration) candidateConfigs.get(0);
153         } else {
154             // Prompt the user to choose a config. A null result means the user
155
// canceled the dialog, in which case this method returns null,
156
// since canceling the dialog should also cancel launching anything.
157
ILaunchConfiguration config = chooseConfiguration(candidateConfigs);
158             if (config != null) {
159                 return config;
160             }
161         }
162         
163         return null;
164     }
165     
166     /**
167      * Show a selection dialog that allows the user to choose one of the specified
168      * launch configurations. Return the chosen config, or <code>null</code> if the
169      * user canceled the dialog.
170      */

171     protected ILaunchConfiguration chooseConfiguration(List JavaDoc configList) {
172         IDebugModelPresentation labelProvider = DebugUITools.newDebugModelPresentation();
173         ElementListSelectionDialog dialog= new ElementListSelectionDialog(getShell(), labelProvider);
174         dialog.setElements(configList.toArray());
175         dialog.setTitle(getTypeSelectionTitle());
176         dialog.setMessage(LauncherMessages.JavaLaunchShortcut_2);
177         dialog.setMultipleSelection(false);
178         int result = dialog.open();
179         labelProvider.dispose();
180         if (result == Window.OK) {
181             return (ILaunchConfiguration) dialog.getFirstResult();
182         }
183         return null;
184     }
185     
186     /**
187      * Create and returns a new configuration based on the specified <code>IType</code>.
188      */

189     protected abstract ILaunchConfiguration createConfiguration(IType type);
190     
191     /**
192      * Opens an error dialog on the given exception.
193      *
194      * @param exception
195      */

196     protected void reportErorr(CoreException exception) {
197         MessageDialog.openError(getShell(), LauncherMessages.JavaLaunchShortcut_3, exception.getStatus().getMessage());
198     }
199     
200     protected ILaunchManager getLaunchManager() {
201         return DebugPlugin.getDefault().getLaunchManager();
202     }
203     
204     /**
205      * Convenience method to get the window that owns this action's Shell.
206      */

207     protected Shell getShell() {
208         return JDIDebugUIPlugin.getActiveWorkbenchShell();
209     }
210     
211     /**
212      * @see ILaunchShortcut#launch(IEditorPart, String)
213      */

214     public void launch(IEditorPart editor, String JavaDoc mode) {
215         IEditorInput input = editor.getEditorInput();
216         IJavaElement je = (IJavaElement) input.getAdapter(IJavaElement.class);
217         if (je != null) {
218             searchAndLaunch(new Object JavaDoc[] {je}, mode, getTypeSelectionTitle(), getEditorEmptyMessage());
219         }
220     }
221
222     /**
223      * @see ILaunchShortcut#launch(ISelection, String)
224      */

225     public void launch(ISelection selection, String JavaDoc mode) {
226         if (selection instanceof IStructuredSelection) {
227             searchAndLaunch(((IStructuredSelection)selection).toArray(), mode, getTypeSelectionTitle(), getSelectionEmptyMessage());
228         }
229     }
230
231     /**
232      * Returns the title for type selection dialog for this launch shortcut.
233      *
234      * @return type selection dialog title
235      */

236     protected abstract String JavaDoc getTypeSelectionTitle();
237     
238     /**
239      * Returns an error message to use when the editor does not contain a launchable type.
240      *
241      * @return error message
242      */

243     protected abstract String JavaDoc getEditorEmptyMessage();
244     
245     /**
246      * Returns an error message to use when the selection does not contain a launchable type.
247      *
248      * @return error message
249      */

250     protected abstract String JavaDoc getSelectionEmptyMessage();
251 }
252
Popular Tags