KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > launcher > JUnitArgumentsTab


1 package org.eclipse.jdt.internal.junit.launcher;
2
3 /*
4  * (c) Copyright IBM Corp. 2000, 2001.
5  * All Rights Reserved.
6  */

7  
8 import java.io.File JavaDoc;
9
10 import org.eclipse.core.resources.IWorkspaceRoot;
11 import org.eclipse.core.resources.ResourcesPlugin;
12 import org.eclipse.core.runtime.CoreException;
13 import org.eclipse.debug.core.ILaunch;
14 import org.eclipse.debug.core.ILaunchConfiguration;
15 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
16 import org.eclipse.debug.ui.ILaunchConfigurationDialog;
17 import org.eclipse.debug.ui.ILaunchConfigurationTab;
18 import org.eclipse.jdt.core.IJavaModel;
19 import org.eclipse.jdt.core.JavaCore;
20 import org.eclipse.jdt.debug.ui.JavaDebugUI;
21 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.events.ModifyEvent;
24 import org.eclipse.swt.events.ModifyListener;
25 import org.eclipse.swt.events.SelectionAdapter;
26 import org.eclipse.swt.events.SelectionEvent;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Button;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.DirectoryDialog;
33 import org.eclipse.swt.widgets.Label;
34 import org.eclipse.swt.widgets.Shell;
35 import org.eclipse.swt.widgets.TabItem;
36 import org.eclipse.swt.widgets.Text;
37
38 /**
39  * This tab appears for local java launch configurations and allows the user to edit
40  * program arguments, VM arguments, and the working directory attributes.
41  */

42 public class JUnitArgumentsTab extends JUnitLaunchConfigurationTab {
43         
44     // VM arguments UI widgets
45
private Label fVMArgumentsLabel;
46     private Text fVMArgumentsText;
47     
48     // Working directory UI widgets
49
private Label fWorkingDirLabel;
50     private Text fWorkingDirText;
51     private Button fWorkingDirBrowseButton;
52     
53     private static final String JavaDoc EMPTY_STRING = "";
54         
55     /**
56      * @see ILaunchConfigurationTab#createControl(Composite)
57      */

58     public void createControl(Composite parent) {
59         
60         Composite comp = new Composite(parent, SWT.NONE);
61         setControl(comp);
62         GridLayout topLayout = new GridLayout();
63         comp.setLayout(topLayout);
64         GridData gd;
65         
66         createVerticalSpacer(comp);
67                 
68         Composite workingDirComp = new Composite(comp, SWT.NONE);
69         GridLayout workingDirLayout = new GridLayout();
70         workingDirLayout.numColumns = 2;
71         workingDirLayout.marginHeight = 0;
72         workingDirLayout.marginWidth = 0;
73         workingDirComp.setLayout(workingDirLayout);
74         gd = new GridData(GridData.FILL_HORIZONTAL);
75         workingDirComp.setLayoutData(gd);
76         
77         fWorkingDirLabel = new Label(workingDirComp, SWT.NONE);
78         fWorkingDirLabel.setText("Wor&king directory:");
79         gd = new GridData();
80         gd.horizontalSpan = 2;
81         fWorkingDirLabel.setLayoutData(gd);
82         
83         fWorkingDirText = new Text(workingDirComp, SWT.SINGLE | SWT.BORDER);
84         gd = new GridData(GridData.FILL_HORIZONTAL);
85         fWorkingDirText.setLayoutData(gd);
86         fWorkingDirText.addModifyListener(new ModifyListener() {
87             public void modifyText(ModifyEvent evt) {
88                 refreshStatus();
89             }
90         });
91         
92         fWorkingDirBrowseButton = new Button(workingDirComp, SWT.PUSH);
93         fWorkingDirBrowseButton.setText("&Browse...");
94         fWorkingDirBrowseButton.addSelectionListener(new SelectionAdapter() {
95             public void widgetSelected(SelectionEvent evt) {
96                 handleWorkingDirBrowseButtonSelected();
97             }
98         });
99         
100         createVerticalSpacer(comp);
101         
102         fVMArgumentsLabel = new Label(comp, SWT.NONE);
103         fVMArgumentsLabel.setText("VM ar&guments:");
104         
105         fVMArgumentsText = new Text(comp, SWT.MULTI | SWT.WRAP| SWT.BORDER | SWT.V_SCROLL);
106         gd = new GridData(GridData.FILL_HORIZONTAL);
107         gd.heightHint = 40;
108         fVMArgumentsText.setLayoutData(gd);
109         fVMArgumentsText.addModifyListener(new ModifyListener() {
110             public void modifyText(ModifyEvent evt) {
111                 refreshStatus();
112             }
113         });
114         
115     }
116         
117     /**
118      * @see JavaLaunchConfigurationTab#updateWidgetsFromConfig(ILaunchConfiguration)
119      */

120     protected void updateWidgetsFromConfig(ILaunchConfiguration config) {
121         updateVMArgsFromConfig(config);
122         updateWorkingDirectoryFromConfig(config);
123     }
124     
125     protected void updateVMArgsFromConfig(ILaunchConfiguration config) {
126         try {
127             String JavaDoc vmArgs = EMPTY_STRING;
128             if (config != null) {
129                 vmArgs = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, EMPTY_STRING);
130             }
131             fVMArgumentsText.setText(vmArgs);
132         } catch (CoreException ce) {
133         }
134     }
135     
136     protected void updateWorkingDirectoryFromConfig(ILaunchConfiguration config) {
137         try {
138             String JavaDoc workingDir = EMPTY_STRING;
139             if (config != null) {
140                 workingDir = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY, EMPTY_STRING);
141             }
142             fWorkingDirText.setText(workingDir);
143         } catch (CoreException ce) {
144         }
145     }
146     
147     /**
148      * @see ILaunchConfigurationTab#dispose()
149      */

150     public void dispose() {
151     }
152     
153     /**
154      * Create some empty space
155      */

156     protected void createVerticalSpacer(Composite comp) {
157         new Label(comp, SWT.NONE);
158     }
159     
160     /**
161      * Show a dialog that lets the user select a working directory
162      */

163     protected void handleWorkingDirBrowseButtonSelected() {
164         DirectoryDialog dialog = new DirectoryDialog(getShell());
165         dialog.setMessage("Select a working directory for the launch configuration");
166         String JavaDoc currentWorkingDir = fWorkingDirText.getText();
167         if (!currentWorkingDir.trim().equals("")) {
168             File JavaDoc path = new File JavaDoc(currentWorkingDir);
169             if (path.exists()) {
170                 dialog.setFilterPath(currentWorkingDir);
171             }
172         }
173         
174         String JavaDoc selectedDirectory = dialog.open();
175         if (selectedDirectory != null) {
176             fWorkingDirText.setText(selectedDirectory);
177         }
178     }
179     
180     /**
181      * Convenience method to get the shell. It is important that the shell be the one
182      * associated with the launch configuration dialog, and not the active workbench
183      * window.
184      */

185     private Shell getShell() {
186         return fWorkingDirLabel.getShell();
187     }
188
189     /**
190      * @see ILaunchConfigurationTab#isPageComplete()
191      */

192     public boolean isValid() {
193         
194         setErrorMessage(null);
195         setMessage(null);
196         
197         String JavaDoc workingDirPath = fWorkingDirText.getText().trim();
198         if (workingDirPath.length() > 0) {
199             File JavaDoc dir = new File JavaDoc(workingDirPath);
200             if (!dir.exists()) {
201                 setErrorMessage("Working directory does not exist.");
202                 return false;
203             }
204             if (!dir.isDirectory()) {
205                 setErrorMessage("Working directory is not a directory.");
206                 return false;
207             }
208         }
209         
210         return true;
211     }
212
213     /**
214      * Defaults are empty.
215      *
216      * @see ILaunchConfigurationTab#setDefaults(ILaunchConfigurationWorkingCopy)
217      */

218     public void setDefaults(ILaunchConfigurationWorkingCopy config) {
219         config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, (String JavaDoc)null);
220         config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, (String JavaDoc)null);
221         config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY, (String JavaDoc)null);
222     }
223
224     /**
225      * @see ILaunchConfigurationTab#initializeFrom(ILaunchConfiguration)
226      */

227     public void initializeFrom(ILaunchConfiguration configuration) {
228         try {
229             fWorkingDirText.setText(configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY, ""));
230             fVMArgumentsText.setText(configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, ""));
231         } catch (CoreException e) {
232             setErrorMessage("Exception occurred reading configuration: " + e.getStatus().getMessage());
233         }
234     }
235
236     /**
237      * @see ILaunchConfigurationTab#performApply(ILaunchConfigurationWorkingCopy)
238      */

239     public void performApply(ILaunchConfigurationWorkingCopy configuration) {
240         configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, getAttributeValueFrom(fVMArgumentsText));
241         configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY, getAttributeValueFrom(fWorkingDirText));
242     }
243
244     /**
245      * Retuns the string in the text widget, or <code>null</code> if empty.
246      *
247      * @return text or <code>null</code>
248      */

249     protected String JavaDoc getAttributeValueFrom(Text text) {
250         String JavaDoc content = text.getText().trim();
251         if (content.length() > 0) {
252             return content;
253         }
254         return null;
255     }
256 }
257
258
Popular Tags