KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.ResourcesPlugin;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.debug.core.ILaunchConfiguration;
18 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
19 import org.eclipse.jdt.core.IJavaElement;
20 import org.eclipse.jdt.core.IJavaProject;
21 import org.eclipse.jdt.core.IType;
22 import org.eclipse.jdt.core.JavaCore;
23 import org.eclipse.jdt.core.JavaModelException;
24 import org.eclipse.jdt.internal.debug.ui.IJavaDebugHelpContextIds;
25 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
26 import org.eclipse.jdt.internal.debug.ui.SWTFactory;
27 import org.eclipse.jdt.internal.debug.ui.launcher.AppletLaunchConfigurationUtils;
28 import org.eclipse.jdt.internal.debug.ui.launcher.DebugTypeSelectionDialog;
29 import org.eclipse.jdt.internal.debug.ui.launcher.LauncherMessages;
30 import org.eclipse.jdt.internal.debug.ui.launcher.SharedJavaMainTab;
31 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
32 import org.eclipse.jdt.launching.IVMInstall;
33 import org.eclipse.jdt.launching.JavaRuntime;
34 import org.eclipse.jdt.ui.ISharedImages;
35 import org.eclipse.jdt.ui.JavaUI;
36 import org.eclipse.jface.window.Window;
37 import org.eclipse.swt.events.SelectionEvent;
38 import org.eclipse.swt.events.SelectionListener;
39 import org.eclipse.swt.graphics.Font;
40 import org.eclipse.swt.graphics.Image;
41 import org.eclipse.swt.layout.GridData;
42 import org.eclipse.swt.layout.GridLayout;
43 import org.eclipse.swt.widgets.Button;
44 import org.eclipse.swt.widgets.Composite;
45 import org.eclipse.swt.widgets.Group;
46 import org.eclipse.swt.widgets.Text;
47 import org.eclipse.ui.PlatformUI;
48
49 /**
50  * This tab appears for Java applet launch configurations and allows the user to edit
51  * attributes such as the applet class to launch and its owning project, if any.
52  * <p>
53  * This class may be instantiated. This class is not intended to be sub-classed.
54  * </p>
55  * @since 2.1
56  */

57 public class AppletMainTab extends SharedJavaMainTab {
58     
59     // Applet viewer UI widgets
60
private Text fAppletViewerClassText;
61     private Button fAppletViewerClassDefaultButton;
62     
63     /**
64      * Creates the applet viewer control area
65      * @param parent the composite to add this control to
66      */

67     private void createAppletViewerControl(Composite parent) {
68         Font font = parent.getFont();
69         Group group = SWTFactory.createGroup(parent, LauncherMessages.AppletMainTab_1, 2, 1, GridData.FILL_HORIZONTAL);
70         Composite comp = SWTFactory.createComposite(group, font, 2, 2, GridData.FILL_BOTH, 0, 0);
71         fAppletViewerClassText= SWTFactory.createSingleText(comp, 2);
72         fAppletViewerClassText.addModifyListener(getDefaultListener());
73         createVerticalSpacer(comp, 1);
74         fAppletViewerClassDefaultButton= createCheckButton(comp, LauncherMessages.AppletMainTab_2);
75         fAppletViewerClassDefaultButton.addSelectionListener(new SelectionListener() {
76             public void widgetDefaultSelected(SelectionEvent e) {}
77             public void widgetSelected(SelectionEvent e) {
78                 handleAppletViewerClassDefaultSelected();
79             }
80         });
81     }
82     
83     /* (non-Javadoc)
84      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#createControl(org.eclipse.swt.widgets.Composite)
85      */

86     public void createControl(Composite parent) {
87         Composite projComp = SWTFactory.createComposite(parent, parent.getFont(), 1, 1, GridData.FILL_BOTH);
88         ((GridLayout)projComp.getLayout()).verticalSpacing = 0;
89         createProjectEditor(projComp);
90         createVerticalSpacer(projComp, 1);
91         createMainTypeEditor(projComp, LauncherMessages.appletlauncher_maintab_mainclasslabel_name);
92         createVerticalSpacer(projComp, 1);
93         createAppletViewerControl(projComp);
94         setControl(projComp);
95         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaDebugHelpContextIds.LAUNCH_CONFIGURATION_DIALOG_APPLET_MAIN_TAB);
96     }
97     
98     /* (non-Javadoc)
99      * @see org.eclipse.debug.ui.AbstractLaunchConfigurationTab#getImage()
100      */

101     public Image getImage() {
102         return JavaUI.getSharedImages().getImage(ISharedImages.IMG_OBJS_CLASS);
103     }
104     
105     /* (non-Javadoc)
106      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getName()
107      */

108     public String JavaDoc getName() {
109         return LauncherMessages.appletlauncher_maintab_name;
110     }
111
112     /**
113      * When the "use default" button is selected, update the "applet viewer class" text.
114      */

115     private void handleAppletViewerClassDefaultSelected() {
116         setAppletViewerTextEnabledState();
117         if (isDefaultAppletViewerClassName()) {
118             fAppletViewerClassText.setText(IJavaLaunchConfigurationConstants.DEFAULT_APPLETVIEWER_CLASS);
119         }
120         else {
121             fAppletViewerClassText.setText(EMPTY_STRING);
122         }
123     }
124     
125     /**
126      * Show a dialog that lists all main types
127      */

128     protected void handleSearchButtonSelected() {
129         IJavaElement[] scope= null;
130         IJavaProject project = getJavaProject();
131         if (project == null) {
132             try {
133                 scope = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects();
134             }
135             catch (JavaModelException e) {
136                 setErrorMessage(e.getMessage());
137                 return;
138             }
139         }
140         else {
141             scope = new IJavaElement[]{project};
142         }
143         IType[] types = null;
144         try {
145             types = AppletLaunchConfigurationUtils.findApplets(getLaunchConfigurationDialog(), scope);
146         }
147         catch (InterruptedException JavaDoc e) {return;}
148         catch (InvocationTargetException JavaDoc e) {
149             setErrorMessage(e.getTargetException().getMessage());
150             return;
151         }
152         DebugTypeSelectionDialog dialog = new DebugTypeSelectionDialog(getShell(), types, LauncherMessages.appletlauncher_maintab_selection_applet_dialog_title);
153         if (dialog.open() == Window.CANCEL) {
154             return;
155         }
156         Object JavaDoc[] results = dialog.getResult();
157         IType type = (IType)results[0];
158         if (type != null) {
159             fMainText.setText(type.getFullyQualifiedName());
160             fProjText.setText(type.getJavaProject().getElementName());
161         }
162     }
163
164     /**
165      * Initialize the applet viewer class name attribute.
166      */

167     private void initializeAppletViewerClass(ILaunchConfigurationWorkingCopy config) {
168         config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_APPLET_APPLETVIEWER_CLASS, (String JavaDoc)null);
169     }
170     
171     /**
172      * Initialize default attribute values based on the
173      * given Java element.
174      */

175     private void initializeDefaults(IJavaElement javaElement, ILaunchConfigurationWorkingCopy config) {
176         initializeJavaProject(javaElement, config);
177         initializeMainTypeAndName(javaElement, config);
178         initializeHardCodedDefaults(config);
179         initializeAppletViewerClass(config);
180     }
181     
182     /**
183      * Set the VM attributes on the working copy based on the workbench default VM.
184      */

185     private void initializeDefaultVM(ILaunchConfigurationWorkingCopy config) {
186         IVMInstall vmInstall= JavaRuntime.getDefaultVMInstall();
187         if (vmInstall == null) {
188             config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_NAME, (String JavaDoc)null);
189             config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_TYPE, (String JavaDoc)null);
190         }
191         else {
192             config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_NAME, vmInstall.getName());
193             config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_TYPE, vmInstall.getVMInstallType().getId());
194         }
195     }
196         
197     /* (non-Javadoc)
198      * @see org.eclipse.jdt.internal.debug.ui.launcher.AbstractJavaMainTab#initializeFrom(org.eclipse.debug.core.ILaunchConfiguration)
199      */

200     public void initializeFrom(ILaunchConfiguration config) {
201         super.initializeFrom(config);
202         updateMainTypeFromConfig(config);
203         updateAppletViewerClassNameFromConfig(config);
204     }
205
206     /**
207      * Initialize those attributes whose default values are independent of any context.
208      */

209     private void initializeHardCodedDefaults(ILaunchConfigurationWorkingCopy config) {
210         initializeDefaultVM(config);
211     }
212     
213     /**
214      * Returns whether the default applet viewer is to be used
215      */

216     private boolean isDefaultAppletViewerClassName() {
217         return fAppletViewerClassDefaultButton.getSelection();
218     }
219
220     /* (non-Javadoc)
221      * @see org.eclipse.debug.ui.AbstractLaunchConfigurationTab#isValid(org.eclipse.debug.core.ILaunchConfiguration)
222      */

223     public boolean isValid(ILaunchConfiguration launchConfig) {
224         setErrorMessage(null);
225         setMessage(null);
226         String JavaDoc name= fProjText.getText().trim();
227         if (name.length() > 0) {
228             if (!ResourcesPlugin.getWorkspace().getRoot().getProject(name).exists()) {
229                 setErrorMessage(LauncherMessages.appletlauncher_maintab_project_error_doesnotexist);
230                 return false;
231             }
232         }
233         name = fMainText.getText().trim();
234         if (name.length() == 0) {
235             setErrorMessage(LauncherMessages.appletlauncher_maintab_type_error_doesnotexist);
236             return false;
237         }
238         name = fAppletViewerClassText.getText().trim();
239         if (name.length() == 0) {
240             setErrorMessage(LauncherMessages.AppletMainTab_3);
241             return false;
242         }
243         return true;
244     }
245
246     /* (non-Javadoc)
247      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#performApply(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
248      */

249     public void performApply(ILaunchConfigurationWorkingCopy config) {
250         config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, fProjText.getText());
251         config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, fMainText.getText());
252         mapResources(config);
253         String JavaDoc appletViewerClassName= null;
254         if (!isDefaultAppletViewerClassName()) {
255             appletViewerClassName= fAppletViewerClassText.getText().trim();
256             if (appletViewerClassName.length() <= 0) {
257                 appletViewerClassName= null;
258             }
259         }
260         config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_APPLET_APPLETVIEWER_CLASS, appletViewerClassName);
261     }
262
263     /**
264      * Set the appropriate enabled state for the applet viewer text widget.
265      */

266     private void setAppletViewerTextEnabledState() {
267         if (isDefaultAppletViewerClassName()) {
268             fAppletViewerClassText.setEnabled(false);
269         }
270         else {
271             fAppletViewerClassText.setEnabled(true);
272         }
273     }
274
275     /* (non-Javadoc)
276      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#setDefaults(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
277      */

278     public void setDefaults(ILaunchConfigurationWorkingCopy config) {
279         IJavaElement je= getContext();
280         if (je == null) {
281             initializeHardCodedDefaults(config);
282         }
283         else {
284             initializeDefaults(je, config);
285         }
286     }
287
288     /**
289      * updates the applet class name from the specified launch configuration
290      * @param config the config to load the class name attribute from
291      */

292     private void updateAppletViewerClassNameFromConfig(ILaunchConfiguration config) {
293         String JavaDoc appletViewerClassName = EMPTY_STRING;
294         try {
295             appletViewerClassName= config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_APPLET_APPLETVIEWER_CLASS, EMPTY_STRING);
296             if (appletViewerClassName.equals(EMPTY_STRING)) {
297                 fAppletViewerClassText.setText(IJavaLaunchConfigurationConstants.DEFAULT_APPLETVIEWER_CLASS);
298                 fAppletViewerClassDefaultButton.setSelection(true);
299             }
300             else {
301                 fAppletViewerClassText.setText(appletViewerClassName);
302                 fAppletViewerClassDefaultButton.setSelection(false);
303             }
304             setAppletViewerTextEnabledState();
305         }
306         catch (CoreException ce) {JDIDebugUIPlugin.log(ce);}
307     }
308
309     /**
310      * @see org.eclipse.debug.ui.AbstractLaunchConfigurationTab#getId()
311      *
312      * @since 3.3
313      */

314     public String JavaDoc getId() {
315         return "org.eclipse.jdt.debug.ui.appletMainTab"; //$NON-NLS-1$
316
}
317 }
318
Popular Tags