KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > ui > launcher > AbstractLaunchShortcut


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 package org.eclipse.pde.ui.launcher;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.debug.core.DebugPlugin;
17 import org.eclipse.debug.core.ILaunchConfiguration;
18 import org.eclipse.debug.core.ILaunchConfigurationType;
19 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
20 import org.eclipse.debug.core.ILaunchManager;
21 import org.eclipse.debug.ui.DebugUITools;
22 import org.eclipse.debug.ui.IDebugModelPresentation;
23 import org.eclipse.debug.ui.ILaunchShortcut;
24 import org.eclipse.jface.window.Window;
25 import org.eclipse.pde.internal.ui.PDEPlugin;
26 import org.eclipse.pde.internal.ui.PDEUIMessages;
27 import org.eclipse.ui.dialogs.ElementListSelectionDialog;
28
29 /**
30  * An abstract class subclassed by the Eclipse Application and OSGi Framework launch shortcuts.
31  * <p>
32  * This class may be subclassed by clients.
33  * </p>
34  * @since 3.3
35  */

36 public abstract class AbstractLaunchShortcut implements ILaunchShortcut {
37
38     /**
39      * Launches the application in the specified mode, or does nothing if the user canceled
40      * the launch when offered to select one of several available launch configurations.
41      *
42      * @param mode
43      * mode of launch (run, debug or profile)
44      *
45      * @see org.eclipse.debug.core.ILaunchManager
46      */

47     protected void launch(String JavaDoc mode) {
48         ILaunchConfiguration configuration = findLaunchConfiguration(mode);
49         if (configuration != null)
50             DebugUITools.launch(configuration, mode);
51     }
52     
53     /**
54      * This method first tries to locate existing launch configurations that are suitable
55      * for the application or framework being launched.
56      * <p>
57      * <ul>
58      * <li>If none are found, a new launch configuration is created and initialized</li>
59      * <li>If one is found, it is launched automatically</li>
60      * <li>If more than one is found, a selection dialog is presented to the user and the chosen
61      * one will be launched</li>
62      * </ul>
63      * </p>
64      * @param mode
65      * mode of launch (run, debug or profile)
66      *
67      * @return a launch configuration to run or <code>null</code> if launch is canceled
68      */

69     protected ILaunchConfiguration findLaunchConfiguration(String JavaDoc mode) {
70         ILaunchConfiguration[] configs = getLaunchConfigurations();
71         ILaunchConfiguration configuration = null;
72         if (configs.length == 0) {
73             configuration = createNewConfiguration();
74         } else if (configs.length == 1) {
75             configuration = configs[0];
76         } else {
77             configuration = chooseConfiguration(configs, mode);
78         }
79         return configuration;
80     }
81     
82     /**
83      * Returns a list of existing launch configurations that are suitable to launch to selected
84      * application or framework.
85      *
86      * @return an array of launch configurations
87      */

88     private ILaunchConfiguration[] getLaunchConfigurations() {
89         ArrayList JavaDoc result = new ArrayList JavaDoc();
90         try {
91             ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
92             ILaunchConfigurationType type = manager.getLaunchConfigurationType(getLaunchConfigurationTypeName());
93             ILaunchConfiguration[] configurations = manager.getLaunchConfigurations(type);
94             for (int i = 0; i < configurations.length; i++) {
95                 if (!DebugUITools.isPrivate(configurations[i]) && isGoodMatch(configurations[i])) {
96                     result.add(configurations[i]);
97                 }
98             }
99         } catch (CoreException e) {
100         }
101         return (ILaunchConfiguration[]) result.toArray(new ILaunchConfiguration[result.size()]);
102     }
103
104     /**
105      * Display to the user a list of matching existing launch configurations and return the user's selection.
106      *
107      * @param configs
108      * an array of matching existing launch configurations
109      * @param mode
110      * mode of launch
111      * @return
112      * the launch configuration selected by the user or <code>null</code> if Cancel was pressed
113      */

114     protected ILaunchConfiguration chooseConfiguration(ILaunchConfiguration[] configs, String JavaDoc mode) {
115         IDebugModelPresentation labelProvider = DebugUITools.newDebugModelPresentation();
116         ElementListSelectionDialog dialog= new ElementListSelectionDialog(PDEPlugin.getActiveWorkbenchShell(), labelProvider);
117         dialog.setElements(configs);
118         dialog.setTitle(PDEUIMessages.RuntimeWorkbenchShortcut_title);
119         if (mode.equals(ILaunchManager.DEBUG_MODE)) {
120             dialog.setMessage(PDEUIMessages.RuntimeWorkbenchShortcut_select_debug);
121         } else {
122             dialog.setMessage(PDEUIMessages.RuntimeWorkbenchShortcut_select_run);
123         }
124         dialog.setMultipleSelection(false);
125         int result = dialog.open();
126         labelProvider.dispose();
127         return (result == Window.OK) ? (ILaunchConfiguration)dialog.getFirstResult() : null;
128     }
129     
130     /**
131      * Create, initialize and return a new launch configuration of the given type
132      *
133      * @return a new, properly-initialized launch configuration
134      */

135     private ILaunchConfiguration createNewConfiguration() {
136         try {
137             ILaunchManager lm = DebugPlugin.getDefault().getLaunchManager();
138             ILaunchConfigurationType type = lm.getLaunchConfigurationType(getLaunchConfigurationTypeName());
139             String JavaDoc name = lm.generateUniqueLaunchConfigurationNameFrom(getName(type));
140             ILaunchConfigurationWorkingCopy wc = type.newInstance(null, name);
141             initializeConfiguration(wc);
142             return wc.doSave();
143         } catch (CoreException ce) {
144             PDEPlugin.logException(ce);
145         }
146         return null;
147     }
148     
149     /**
150      * Returns the name assigned to the new launch configuration
151      *
152      * @return a name for the new launch configuration
153      */

154     protected String JavaDoc getName(ILaunchConfigurationType type) {
155         return type.getName();
156     }
157     
158     /**
159      * Initialize launch attributes on the new launch configuration.
160      * Must be overridden by subclasses.
161      *
162      * @param wc
163      * the launch configuration working copy to be initialize
164      *
165      * @see IPDELauncherConstants
166      */

167     protected abstract void initializeConfiguration(ILaunchConfigurationWorkingCopy wc);
168     
169     /**
170      * Returns the launch configuration type name.
171      * Must be overridden by subclasses
172      *
173      * @return the launch configuration type name
174      */

175     protected abstract String JavaDoc getLaunchConfigurationTypeName();
176     
177     /**
178      * Determines whether a given launch configuration is a good match given the current application or framework
179      * being launched. This method must be overridden by subclasses. Its purpose is to add criteria on
180      * what makes a good match or not.
181      *
182      * @param configuration
183      * the launch configuration being evaluated
184      * @return
185      * <code>true</code> if the launch configuration is a good match for the application or
186      * framework being launched, <code>false</code> otherwise.
187      */

188     protected abstract boolean isGoodMatch(ILaunchConfiguration configuration);
189
190 }
191
Popular Tags