KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 package org.eclipse.jdt.internal.debug.ui.launcher;
13
14 import org.eclipse.core.resources.IWorkspaceRoot;
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.IJavaModel;
20 import org.eclipse.jdt.core.IJavaProject;
21 import org.eclipse.jdt.core.JavaCore;
22 import org.eclipse.jdt.core.JavaModelException;
23 import org.eclipse.jdt.debug.ui.launchConfigurations.JavaLaunchTab;
24 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
25 import org.eclipse.jdt.internal.launching.JavaMigrationDelegate;
26 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
27 import org.eclipse.jdt.ui.JavaElementLabelProvider;
28 import org.eclipse.jface.viewers.ILabelProvider;
29 import org.eclipse.jface.window.Window;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.events.ModifyEvent;
32 import org.eclipse.swt.events.ModifyListener;
33 import org.eclipse.swt.events.SelectionEvent;
34 import org.eclipse.swt.events.SelectionListener;
35 import org.eclipse.swt.graphics.Font;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.layout.GridLayout;
38 import org.eclipse.swt.widgets.Button;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.swt.widgets.Group;
41 import org.eclipse.swt.widgets.Text;
42 import org.eclipse.ui.dialogs.ElementListSelectionDialog;
43
44 /**
45  * Provides general widgets and methods for a Java type launch configuration
46  * 'Main' tab.
47  * Currently there are only three Java type launch configurations: Local Java Application, Applet, and Remote Debug
48  * which this class is used by
49  *
50  * @since 3.2
51  */

52 public abstract class AbstractJavaMainTab extends JavaLaunchTab {
53
54 /**
55  * A listener which handles widget change events for the controls
56  * in this tab.
57  */

58 private class WidgetListener implements ModifyListener, SelectionListener {
59     
60     public void modifyText(ModifyEvent e) {
61         updateLaunchConfigurationDialog();
62     }
63     
64     public void widgetDefaultSelected(SelectionEvent e) {/*do nothing*/}
65     
66     public void widgetSelected(SelectionEvent e) {
67         Object JavaDoc source = e.getSource();
68         if (source == fProjButton) {
69             handleProjectButtonSelected();
70         }
71         else {
72             updateLaunchConfigurationDialog();
73         }
74     }
75 }
76     
77     protected static final String JavaDoc EMPTY_STRING = ""; //$NON-NLS-1$
78
//Project UI widgets
79
protected Text fProjText;
80
81     private Button fProjButton;
82     
83     private WidgetListener fListener = new WidgetListener();
84     
85     /**
86      * chooses a project for the type of java launch config that it is
87      * @return
88      */

89     private IJavaProject chooseJavaProject() {
90         ILabelProvider labelProvider= new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT);
91         ElementListSelectionDialog dialog= new ElementListSelectionDialog(getShell(), labelProvider);
92         dialog.setTitle(LauncherMessages.AbstractJavaMainTab_4);
93         dialog.setMessage(LauncherMessages.AbstractJavaMainTab_3);
94         try {
95             dialog.setElements(JavaCore.create(getWorkspaceRoot()).getJavaProjects());
96         }
97         catch (JavaModelException jme) {JDIDebugUIPlugin.log(jme);}
98         IJavaProject javaProject= getJavaProject();
99         if (javaProject != null) {
100             dialog.setInitialSelections(new Object JavaDoc[] { javaProject });
101         }
102         if (dialog.open() == Window.OK) {
103             return (IJavaProject) dialog.getFirstResult();
104         }
105         return null;
106     }
107     
108     /**
109      * Creates the widgets for specifying a main type.
110      *
111      * @param parent the parent composite
112      */

113     protected void createProjectEditor(Composite parent) {
114         Font font= parent.getFont();
115         Group group= new Group(parent, SWT.NONE);
116         group.setText(LauncherMessages.AbstractJavaMainTab_0);
117         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
118         group.setLayoutData(gd);
119         GridLayout layout = new GridLayout();
120         layout.numColumns = 2;
121         group.setLayout(layout);
122         group.setFont(font);
123         fProjText = new Text(group, SWT.SINGLE | SWT.BORDER);
124         gd = new GridData(GridData.FILL_HORIZONTAL);
125         fProjText.setLayoutData(gd);
126         fProjText.setFont(font);
127         fProjText.addModifyListener(fListener);
128         fProjButton = createPushButton(group, LauncherMessages.AbstractJavaMainTab_1, null);
129         fProjButton.addSelectionListener(fListener);
130     }
131     
132     /**
133      * returns the default listener from this class. For all subclasses
134      * this listener will only provide the functionality of updating the current tab
135      *
136      * @return a widget listener
137      */

138     protected WidgetListener getDefaultListener() {
139         return fListener;
140     }
141     
142     /**
143      * Convenience method to get access to the java model.
144      */

145     private IJavaModel getJavaModel() {
146         return JavaCore.create(getWorkspaceRoot());
147     }
148     
149     /**
150      * Return the IJavaProject corresponding to the project name in the project name
151      * text field, or null if the text does not match a project name.
152      */

153     protected IJavaProject getJavaProject() {
154         String JavaDoc projectName = fProjText.getText().trim();
155         if (projectName.length() < 1) {
156             return null;
157         }
158         return getJavaModel().getJavaProject(projectName);
159     }
160
161     /**
162      * Convenience method to get the workspace root.
163      */

164     protected IWorkspaceRoot getWorkspaceRoot() {
165         return ResourcesPlugin.getWorkspace().getRoot();
166     }
167
168     /**
169      * Show a dialog that lets the user select a project. This in turn provides
170      * context for the main type, allowing the user to key a main type name, or
171      * constraining the search for main types to the specified project.
172      */

173     protected void handleProjectButtonSelected() {
174         IJavaProject project = chooseJavaProject();
175         if (project == null) {
176             return;
177         }
178         String JavaDoc projectName = project.getElementName();
179         fProjText.setText(projectName);
180     }
181     
182     /* (non-Javadoc)
183      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#initializeFrom(org.eclipse.debug.core.ILaunchConfiguration)
184      */

185     public void initializeFrom(ILaunchConfiguration config) {
186         updateProjectFromConfig(config);
187         super.initializeFrom(config);
188     }
189     
190     /**
191      * updates the project text field form the configuration
192      * @param config the configuration we are editing
193      */

194     private void updateProjectFromConfig(ILaunchConfiguration config) {
195         String JavaDoc projectName = EMPTY_STRING;
196         try {
197             projectName = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, EMPTY_STRING);
198         }
199         catch (CoreException ce) {
200             setErrorMessage(ce.getStatus().getMessage());
201         }
202         fProjText.setText(projectName);
203     }
204     
205     /**
206      * Maps the config to associated java resource
207      *
208      * @param config
209      */

210     protected void mapResources(ILaunchConfigurationWorkingCopy config) {
211         try {
212         //CONTEXTLAUNCHING
213
IJavaProject javaProject = getJavaProject();
214             if (javaProject != null && javaProject.exists() && javaProject.isOpen()) {
215                 JavaMigrationDelegate.updateResourceMapping(config);
216             }
217         } catch(CoreException ce) {
218             setErrorMessage(ce.getStatus().getMessage());
219         }
220     }
221     
222 }
223
Popular Tags