KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > debug > ui > launchConfigurations > JavaMainTab


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.debug.ui.launchConfigurations;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.resources.IProject;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.resources.IWorkspace;
18 import org.eclipse.core.resources.ResourcesPlugin;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.debug.core.ILaunchConfiguration;
22 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
23 import org.eclipse.jdt.core.IJavaElement;
24 import org.eclipse.jdt.core.IJavaModel;
25 import org.eclipse.jdt.core.IJavaProject;
26 import org.eclipse.jdt.core.IType;
27 import org.eclipse.jdt.core.JavaCore;
28 import org.eclipse.jdt.core.JavaModelException;
29 import org.eclipse.jdt.core.search.IJavaSearchScope;
30 import org.eclipse.jdt.core.search.SearchEngine;
31 import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants;
32 import org.eclipse.jdt.internal.debug.ui.IJavaDebugHelpContextIds;
33 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
34 import org.eclipse.jdt.internal.debug.ui.SWTFactory;
35 import org.eclipse.jdt.internal.debug.ui.launcher.DebugTypeSelectionDialog;
36 import org.eclipse.jdt.internal.debug.ui.launcher.LauncherMessages;
37 import org.eclipse.jdt.internal.debug.ui.launcher.MainMethodSearchEngine;
38 import org.eclipse.jdt.internal.debug.ui.launcher.SharedJavaMainTab;
39 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
40 import org.eclipse.jdt.ui.ISharedImages;
41 import org.eclipse.jdt.ui.JavaUI;
42 import org.eclipse.jface.window.Window;
43 import org.eclipse.swt.graphics.Image;
44 import org.eclipse.swt.layout.GridData;
45 import org.eclipse.swt.layout.GridLayout;
46 import org.eclipse.swt.widgets.Button;
47 import org.eclipse.swt.widgets.Composite;
48 import org.eclipse.ui.PlatformUI;
49
50 import com.ibm.icu.text.MessageFormat;
51
52 /**
53  * A launch configuration tab that displays and edits project and
54  * main type name launch configuration attributes.
55  * <p>
56  * This class may be instantiated. This class is not intended to be sub-classed.
57  * </p>
58  * @since 3.2
59  */

60
61 public class JavaMainTab extends SharedJavaMainTab {
62
63     /**
64      * Boolean launch configuration attribute indicating that external jars (on
65      * the runtime classpath) should be searched when looking for a main type.
66      * Default value is <code>false</code>.
67      *
68      * @since 2.1
69      */

70     public static final String JavaDoc ATTR_INCLUDE_EXTERNAL_JARS = IJavaDebugUIConstants.PLUGIN_ID + ".INCLUDE_EXTERNAL_JARS"; //$NON-NLS-1$
71
/**
72      * Boolean launch configuration attribute indicating whether types inheriting
73      * a main method should be considered when searching for a main type.
74      * Default value is <code>false</code>.
75      *
76      * @since 3.0
77      */

78     public static final String JavaDoc ATTR_CONSIDER_INHERITED_MAIN = IJavaDebugUIConstants.PLUGIN_ID + ".CONSIDER_INHERITED_MAIN"; //$NON-NLS-1$
79

80     // UI widgets
81
private Button fSearchExternalJarsCheckButton;
82     private Button fConsiderInheritedMainButton;
83     private Button fStopInMainCheckButton;
84
85     /* (non-Javadoc)
86      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#createControl(org.eclipse.swt.widgets.Composite)
87      */

88     public void createControl(Composite parent) {
89         Composite comp = SWTFactory.createComposite(parent, parent.getFont(), 1, 1, GridData.FILL_BOTH);
90         ((GridLayout)comp.getLayout()).verticalSpacing = 0;
91         createProjectEditor(comp);
92         createVerticalSpacer(comp, 1);
93         createMainTypeEditor(comp, LauncherMessages.JavaMainTab_Main_cla_ss__4);
94         setControl(comp);
95         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaDebugHelpContextIds.LAUNCH_CONFIGURATION_DIALOG_MAIN_TAB);
96     }
97
98     /**
99      * @see org.eclipse.jdt.internal.debug.ui.launcher.SharedJavaMainTab#createMainTypeExtensions(org.eclipse.swt.widgets.Composite)
100      */

101     protected void createMainTypeExtensions(Composite parent) {
102         fSearchExternalJarsCheckButton = SWTFactory.createCheckButton(parent, LauncherMessages.JavaMainTab_E_xt__jars_6, null, false, 2);
103         fSearchExternalJarsCheckButton.addSelectionListener(getDefaultListener());
104         
105         fConsiderInheritedMainButton = SWTFactory.createCheckButton(parent, LauncherMessages.JavaMainTab_22, null, false, 2);
106         fConsiderInheritedMainButton.addSelectionListener(getDefaultListener());
107         
108         fStopInMainCheckButton = SWTFactory.createCheckButton(parent, LauncherMessages.JavaMainTab_St_op_in_main_1, null, false, 1);
109         fStopInMainCheckButton.addSelectionListener(getDefaultListener());
110     }
111
112     /* (non-Javadoc)
113      * @see org.eclipse.debug.ui.AbstractLaunchConfigurationTab#getImage()
114      */

115     public Image getImage() {
116         return JavaUI.getSharedImages().getImage(ISharedImages.IMG_OBJS_CLASS);
117     }
118     
119     /* (non-Javadoc)
120      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getName()
121      */

122     public String JavaDoc getName() {
123         return LauncherMessages.JavaMainTab__Main_19;
124     }
125     
126     /**
127      * @see org.eclipse.debug.ui.AbstractLaunchConfigurationTab#getId()
128      *
129      * @since 3.3
130      */

131     public String JavaDoc getId() {
132         return "org.eclipse.jdt.debug.ui.javaMainTab"; //$NON-NLS-1$
133
}
134     
135     /**
136      * Show a dialog that lists all main types
137      */

138     protected void handleSearchButtonSelected() {
139         IJavaProject project = getJavaProject();
140         IJavaElement[] elements = null;
141         if ((project == null) || !project.exists()) {
142             IJavaModel model = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
143             if (model != null) {
144                 try {
145                     elements = model.getJavaProjects();
146                 }
147                 catch (JavaModelException e) {JDIDebugUIPlugin.log(e);}
148             }
149         }
150         else {
151             elements = new IJavaElement[]{project};
152         }
153         if (elements == null) {
154             elements = new IJavaElement[]{};
155         }
156         int constraints = IJavaSearchScope.SOURCES;
157         constraints |= IJavaSearchScope.APPLICATION_LIBRARIES;
158         if (fSearchExternalJarsCheckButton.getSelection()) {
159             constraints |= IJavaSearchScope.SYSTEM_LIBRARIES;
160         }
161         IJavaSearchScope searchScope = SearchEngine.createJavaSearchScope(elements, constraints);
162         MainMethodSearchEngine engine = new MainMethodSearchEngine();
163         IType[] types = null;
164         try {
165             types = engine.searchMainMethods(getLaunchConfigurationDialog(), searchScope, fConsiderInheritedMainButton.getSelection());
166         }
167         catch (InvocationTargetException JavaDoc e) {
168             setErrorMessage(e.getMessage());
169             return;
170         }
171         catch (InterruptedException JavaDoc e) {
172             setErrorMessage(e.getMessage());
173             return;
174         }
175         DebugTypeSelectionDialog mmsd = new DebugTypeSelectionDialog(getShell(), types, LauncherMessages.JavaMainTab_Choose_Main_Type_11);
176         if (mmsd.open() == Window.CANCEL) {
177             return;
178         }
179         Object JavaDoc[] results = mmsd.getResult();
180         IType type = (IType)results[0];
181         if (type != null) {
182             fMainText.setText(type.getFullyQualifiedName());
183             fProjText.setText(type.getJavaProject().getElementName());
184         }
185     }
186     
187     /* (non-Javadoc)
188      * @see org.eclipse.jdt.internal.debug.ui.launcher.AbstractJavaMainTab#initializeFrom(org.eclipse.debug.core.ILaunchConfiguration)
189      */

190     public void initializeFrom(ILaunchConfiguration config) {
191         super.initializeFrom(config);
192         updateMainTypeFromConfig(config);
193         updateStopInMainFromConfig(config);
194         updateInheritedMainsFromConfig(config);
195         updateExternalJars(config);
196     }
197
198     /* (non-Javadoc)
199      * @see org.eclipse.debug.ui.AbstractLaunchConfigurationTab#isValid(org.eclipse.debug.core.ILaunchConfiguration)
200      */

201     public boolean isValid(ILaunchConfiguration config) {
202         setErrorMessage(null);
203         setMessage(null);
204         String JavaDoc name = fProjText.getText().trim();
205         if (name.length() > 0) {
206             IWorkspace workspace = ResourcesPlugin.getWorkspace();
207             IStatus status = workspace.validateName(name, IResource.PROJECT);
208             if (status.isOK()) {
209                 IProject project= ResourcesPlugin.getWorkspace().getRoot().getProject(name);
210                 if (!project.exists()) {
211                     setErrorMessage(MessageFormat.format(LauncherMessages.JavaMainTab_20, new String JavaDoc[] {name}));
212                     return false;
213                 }
214                 if (!project.isOpen()) {
215                     setErrorMessage(MessageFormat.format(LauncherMessages.JavaMainTab_21, new String JavaDoc[] {name}));
216                     return false;
217                 }
218             }
219             else {
220                 setErrorMessage(MessageFormat.format(LauncherMessages.JavaMainTab_19, new String JavaDoc[]{status.getMessage()}));
221                 return false;
222             }
223         }
224         name = fMainText.getText().trim();
225         if (name.length() == 0) {
226             setErrorMessage(LauncherMessages.JavaMainTab_Main_type_not_specified_16);
227             return false;
228         }
229         return true;
230     }
231             
232     /* (non-Javadoc)
233      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#performApply(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
234      */

235     public void performApply(ILaunchConfigurationWorkingCopy config) {
236         config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, fProjText.getText().trim());
237         config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, fMainText.getText().trim());
238         mapResources(config);
239         
240         // attribute added in 2.1, so null must be used instead of false for backwards compatibility
241
if (fStopInMainCheckButton.getSelection()) {
242             config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_STOP_IN_MAIN, true);
243         }
244         else {
245             config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_STOP_IN_MAIN, (String JavaDoc)null);
246         }
247         
248         // attribute added in 2.1, so null must be used instead of false for backwards compatibility
249
if (fSearchExternalJarsCheckButton.getSelection()) {
250             config.setAttribute(ATTR_INCLUDE_EXTERNAL_JARS, true);
251         }
252         else {
253             config.setAttribute(ATTR_INCLUDE_EXTERNAL_JARS, (String JavaDoc)null);
254         }
255         
256         // attribute added in 3.0, so null must be used instead of false for backwards compatibility
257
if (fConsiderInheritedMainButton.getSelection()) {
258             config.setAttribute(ATTR_CONSIDER_INHERITED_MAIN, true);
259         }
260         else {
261             config.setAttribute(ATTR_CONSIDER_INHERITED_MAIN, (String JavaDoc)null);
262         }
263     }
264     
265     /* (non-Javadoc)
266      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#setDefaults(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
267      */

268     public void setDefaults(ILaunchConfigurationWorkingCopy config) {
269         IJavaElement javaElement = getContext();
270         if (javaElement != null) {
271             initializeJavaProject(javaElement, config);
272         }
273         else {
274             config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, EMPTY_STRING);
275         }
276         initializeMainTypeAndName(javaElement, config);
277     }
278     
279     /**
280      * updates the external jars attribute from the specified launch config
281      * @param config the config to load from
282      */

283     private void updateExternalJars(ILaunchConfiguration config) {
284         boolean search = false;
285         try {
286             search = config.getAttribute(ATTR_INCLUDE_EXTERNAL_JARS, false);
287         }
288         catch (CoreException e) {JDIDebugUIPlugin.log(e);}
289         fSearchExternalJarsCheckButton.setSelection(search);
290     }
291
292     /**
293      * update the inherited mains attribute from the specified launch config
294      * @param config the config to load from
295      */

296     private void updateInheritedMainsFromConfig(ILaunchConfiguration config) {
297         boolean inherit = false;
298         try {
299             inherit = config.getAttribute(ATTR_CONSIDER_INHERITED_MAIN, false);
300         }
301         catch (CoreException e) {JDIDebugUIPlugin.log(e);}
302         fConsiderInheritedMainButton.setSelection(inherit);
303     }
304
305     /**
306      * updates the stop in main attribute from the specified launch config
307      * @param config the config to load the stop in main attribute from
308      */

309     private void updateStopInMainFromConfig(ILaunchConfiguration config) {
310         boolean stop = false;
311         try {
312             stop = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_STOP_IN_MAIN, false);
313         }
314         catch (CoreException e) {JDIDebugUIPlugin.log(e);}
315         fStopInMainCheckButton.setSelection(stop);
316     }
317
318 }
319
Popular Tags