KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > launcher > EquinoxLaunchShortcut


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.internal.ui.launcher;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Properties JavaDoc;
16 import java.util.StringTokenizer JavaDoc;
17 import java.util.TreeMap JavaDoc;
18
19 import org.eclipse.core.resources.IProject;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.Preferences;
22 import org.eclipse.debug.core.DebugPlugin;
23 import org.eclipse.debug.core.ILaunchConfiguration;
24 import org.eclipse.debug.core.ILaunchConfigurationType;
25 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
26 import org.eclipse.debug.core.ILaunchManager;
27 import org.eclipse.debug.ui.DebugUITools;
28 import org.eclipse.debug.ui.IDebugModelPresentation;
29 import org.eclipse.debug.ui.ILaunchShortcut;
30 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
31 import org.eclipse.jface.viewers.ISelection;
32 import org.eclipse.jface.window.Window;
33 import org.eclipse.pde.core.plugin.IPluginModelBase;
34 import org.eclipse.pde.internal.core.ICoreConstants;
35 import org.eclipse.pde.internal.core.PDECore;
36 import org.eclipse.pde.internal.core.PluginModelManager;
37 import org.eclipse.pde.internal.core.TargetPlatform;
38 import org.eclipse.pde.internal.ui.PDEPlugin;
39 import org.eclipse.pde.internal.ui.PDEUIMessages;
40 import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
41 import org.eclipse.ui.IEditorPart;
42 import org.eclipse.ui.dialogs.ElementListSelectionDialog;
43
44 public class EquinoxLaunchShortcut implements ILaunchShortcut {
45     
46     private static final String JavaDoc CLASSPATH_PROVIDER = "org.eclipse.pde.ui.workbenchClasspathProvider"; //$NON-NLS-1$
47
private static final String JavaDoc CONFIGURATION_TYPE = "org.eclipse.pde.ui.EquinoxLauncher"; //$NON-NLS-1$
48

49     public void run(IProject project) {
50         launch(PDECore.getDefault().getModelManager().findModel(project), ILaunchManager.RUN_MODE);
51     }
52     
53     public void debug(IProject project) {
54         launch(PDECore.getDefault().getModelManager().findModel(project), ILaunchManager.DEBUG_MODE);
55     }
56     
57     public void launch(ISelection selection, String JavaDoc mode) {
58         launch((IPluginModelBase)null, mode);
59     }
60
61     public void launch(IEditorPart editor, String JavaDoc mode) {
62         launch((IPluginModelBase)null, mode);
63     }
64     
65     private void launch(IPluginModelBase model, String JavaDoc mode) {
66         ILaunchConfiguration[] configs = getLaunchConfigurations();
67         ILaunchConfiguration configuration = null;
68         if (configs.length == 0) {
69             PluginModelManager manager = PDECore.getDefault().getModelManager();
70             IPluginModelBase[] models = (model == null) ? manager.getWorkspaceModels() : new IPluginModelBase[] {model};
71             if (models.length == 0) {
72                 IPluginModelBase osgi = manager.findModel("org.eclipse.osgi"); //$NON-NLS-1$
73
if (osgi != null)
74                     models = new IPluginModelBase[] {osgi};
75             }
76             configuration = createNewConfiguration(models, mode);
77         } else if (configs.length == 1) {
78             configuration = configs[0];
79         } else {
80             configuration = chooseConfiguration(configs, mode);
81         }
82         
83         if (configuration != null)
84             DebugUITools.launch(configuration, mode);
85     }
86     
87     private ILaunchConfiguration[] getLaunchConfigurations() {
88         ArrayList JavaDoc result = new ArrayList JavaDoc();
89         try {
90             ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
91             ILaunchConfiguration[] configs = manager.getLaunchConfigurations(getLaunchConfigurationType());
92             for (int i = 0; i < configs.length; i++) {
93                 if (!DebugUITools.isPrivate(configs[i])) {
94                             result.add(configs[i]);
95                 }
96             }
97         } catch (CoreException e) {
98         }
99         return (ILaunchConfiguration[]) result.toArray(new ILaunchConfiguration[result.size()]);
100     }
101     
102     private ILaunchConfigurationType getLaunchConfigurationType() {
103         ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
104         return manager.getLaunchConfigurationType(CONFIGURATION_TYPE);
105     }
106     
107     protected ILaunchConfiguration chooseConfiguration(ILaunchConfiguration[] configs, String JavaDoc mode) {
108         IDebugModelPresentation labelProvider = DebugUITools.newDebugModelPresentation();
109         ElementListSelectionDialog dialog= new ElementListSelectionDialog(PDEPlugin.getActiveWorkbenchShell(), labelProvider);
110         dialog.setElements(configs);
111         dialog.setTitle(PDEUIMessages.RuntimeWorkbenchShortcut_title);
112         if (mode.equals(ILaunchManager.DEBUG_MODE)) {
113             dialog.setMessage(PDEUIMessages.RuntimeWorkbenchShortcut_select_debug);
114         } else {
115             dialog.setMessage(PDEUIMessages.RuntimeWorkbenchShortcut_select_run);
116         }
117         dialog.setMultipleSelection(false);
118         int result= dialog.open();
119         labelProvider.dispose();
120         if (result == Window.OK) {
121             return (ILaunchConfiguration)dialog.getFirstResult();
122         }
123         return null;
124     }
125     
126     private ILaunchConfiguration createNewConfiguration(IPluginModelBase[] selected, String JavaDoc mode) {
127         ILaunchConfiguration config = null;
128         try {
129             ILaunchConfigurationType configType = getLaunchConfigurationType();
130             String JavaDoc computedName = getComputedName("Equinox"); //$NON-NLS-1$
131
ILaunchConfigurationWorkingCopy wc = configType.newInstance(null, computedName);
132             setJavaArguments(wc);
133             wc.setAttribute(IPDELauncherConstants.TRACING_CHECKED, IPDELauncherConstants.TRACING_NONE);
134             wc.setAttribute(IPDELauncherConstants.AUTOMATIC_ADD, true);
135             initializePluginState(wc, selected);
136             wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_SOURCE_PATH_PROVIDER, CLASSPATH_PROVIDER);
137             config = wc.doSave();
138         } catch (CoreException ce) {
139             PDEPlugin.logException(ce);
140         }
141         return config;
142     }
143     
144     private void setJavaArguments(ILaunchConfigurationWorkingCopy wc) {
145         Preferences preferences = PDECore.getDefault().getPluginPreferences();
146         String JavaDoc progArgs = preferences.getString(ICoreConstants.PROGRAM_ARGS);
147         if (progArgs.indexOf("-console") == -1) //$NON-NLS-1$
148
progArgs = "-console " + progArgs; //$NON-NLS-1$
149
wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, progArgs); //$NON-NLS-1$
150
String JavaDoc vmArgs = preferences.getString(ICoreConstants.VM_ARGS);
151         if (vmArgs.length() > 0)
152             wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, vmArgs);
153     }
154     
155     public static void initializePluginState(ILaunchConfigurationWorkingCopy wc, IPluginModelBase[] selected) {
156         Map JavaDoc startLevelMap = getStartLevelMap();
157         TreeMap JavaDoc pluginMap = new TreeMap JavaDoc();
158         for (int i = 0; i < selected.length; i++)
159             RuntimeWorkbenchShortcut.addPluginAndDependencies(selected[i], pluginMap);
160         Object JavaDoc[] models = pluginMap.values().toArray();
161         StringBuffer JavaDoc wsplugins = new StringBuffer JavaDoc();
162         StringBuffer JavaDoc explugins = new StringBuffer JavaDoc();
163         for (int i = 0; i < models.length; i++) {
164             IPluginModelBase model = (IPluginModelBase)models[i];
165             String JavaDoc id = model.getPluginBase().getId();
166             String JavaDoc value = "org.eclipse.osgi".equals(id) ? "@:" : "@default:default"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
167
if (startLevelMap.containsKey(id))
168                 value = (String JavaDoc)startLevelMap.get(id);
169             if (model.getUnderlyingResource() == null) {
170                 if (explugins.length() > 0)
171                     explugins.append(","); //$NON-NLS-1$
172
explugins.append(id);
173                 explugins.append(value);
174             } else {
175                 if (wsplugins.length() > 0)
176                     wsplugins.append(","); //$NON-NLS-1$
177
wsplugins.append(id);
178                 wsplugins.append(value);
179             }
180         }
181         wc.setAttribute(IPDELauncherConstants.WORKSPACE_BUNDLES, wsplugins.toString());
182         wc.setAttribute(IPDELauncherConstants.TARGET_BUNDLES, explugins.toString());
183         
184     }
185     
186     private static TreeMap JavaDoc getStartLevelMap() {
187         TreeMap JavaDoc startLevels = new TreeMap JavaDoc();
188         Properties JavaDoc props = TargetPlatform.getConfigIniProperties();
189         if (props != null) {
190             String JavaDoc value = (String JavaDoc)props.get("osgi.bundles"); //$NON-NLS-1$
191
if (value != null) {
192                 StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(value, ","); //$NON-NLS-1$
193
while (tokenizer.hasMoreTokens()) {
194                     String JavaDoc tokenValue = tokenizer.nextToken();
195                     int index = tokenValue.indexOf("@"); //$NON-NLS-1$
196
if (index > 0) {
197                         String JavaDoc plugin = tokenValue.substring(0,index).trim();
198                         startLevels.put(plugin, getStartValue(tokenValue.substring(index)));
199                     }
200                 }
201             }
202         }
203         return startLevels;
204     }
205     
206     private static String JavaDoc getStartValue(String JavaDoc value) {
207         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(value);
208                 
209         StringBuffer JavaDoc result = new StringBuffer JavaDoc("@"); //$NON-NLS-1$
210
result.append(":"); //$NON-NLS-1$
211

212         int index = value.indexOf("start"); //$NON-NLS-1$
213
result.append(Boolean.toString(index != -1));
214         
215         if (index != -1)
216             buffer.delete(index, index + 5);
217         
218         int colon = value.indexOf(':');
219         if (colon != -1)
220             buffer.deleteCharAt(colon);
221         
222         // delete the first char '@'
223
buffer.deleteCharAt(0);
224         
225         try {
226             result.insert(1, Integer.parseInt(buffer.toString().trim()));
227         } catch (NumberFormatException JavaDoc e) {
228             result.insert(1, "default"); //$NON-NLS-1$
229
}
230         return result.toString();
231     }
232
233     private String JavaDoc getComputedName(String JavaDoc prefix) {
234         ILaunchManager lm = DebugPlugin.getDefault().getLaunchManager();
235         return lm.generateUniqueLaunchConfigurationNameFrom(prefix);
236     }
237
238 }
239
Popular Tags