KickJava   Java API By Example, From Geeks To Geeks.

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


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.internal.debug.ui.launcher;
12
13 import java.io.File JavaDoc;
14
15 import org.eclipse.core.resources.IContainer;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.resources.IWorkspaceRoot;
18 import org.eclipse.core.resources.ResourcesPlugin;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.core.variables.IStringVariableManager;
23 import org.eclipse.core.variables.VariablesPlugin;
24 import org.eclipse.debug.core.ILaunchConfiguration;
25 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
26 import org.eclipse.debug.ui.StringVariableSelectionDialog;
27 import org.eclipse.jdt.core.IJavaProject;
28 import org.eclipse.jdt.debug.ui.launchConfigurations.JavaLaunchTab;
29 import org.eclipse.jdt.internal.debug.ui.IJavaDebugHelpContextIds;
30 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
31 import org.eclipse.jdt.internal.debug.ui.SWTFactory;
32 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
33 import org.eclipse.jdt.launching.JavaRuntime;
34 import org.eclipse.swt.events.ModifyEvent;
35 import org.eclipse.swt.events.ModifyListener;
36 import org.eclipse.swt.events.SelectionAdapter;
37 import org.eclipse.swt.events.SelectionEvent;
38 import org.eclipse.swt.graphics.Font;
39 import org.eclipse.swt.layout.GridData;
40 import org.eclipse.swt.layout.GridLayout;
41 import org.eclipse.swt.widgets.Button;
42 import org.eclipse.swt.widgets.Composite;
43 import org.eclipse.swt.widgets.DirectoryDialog;
44 import org.eclipse.swt.widgets.Group;
45 import org.eclipse.swt.widgets.Text;
46 import org.eclipse.ui.PlatformUI;
47 import org.eclipse.ui.dialogs.ContainerSelectionDialog;
48
49 /**
50  * A control for setting the working directory associated with a launch
51  * configuration.
52  */

53 public class WorkingDirectoryBlock extends JavaLaunchTab {
54             
55     // Local directory
56
private Button fWorkspaceButton;
57     private Button fFileSystemButton;
58     private Button fVariablesButton;
59     
60     //bug 29565 fix
61
private Button fUseDefaultDirButton = null;
62     private Button fUseOtherDirButton = null;
63     private Text fOtherWorkingText = null;
64     private Text fWorkingDirText;
65     
66     /**
67      * The last launch config this tab was initialized from
68      */

69     private ILaunchConfiguration fLaunchConfiguration;
70     
71     /**
72      * A listener to update for text changes and widget selection
73      */

74     private class WidgetListener extends SelectionAdapter implements ModifyListener {
75         public void modifyText(ModifyEvent e) {
76             updateLaunchConfigurationDialog();
77         }
78         public void widgetSelected(SelectionEvent e) {
79             Object JavaDoc source= e.getSource();
80             if (source == fWorkspaceButton) {
81                 handleWorkspaceDirBrowseButtonSelected();
82             }
83             else if (source == fFileSystemButton) {
84                 handleWorkingDirBrowseButtonSelected();
85             }
86             else if (source == fVariablesButton) {
87                 handleWorkingDirVariablesButtonSelected();
88             }
89             else if(source == fUseDefaultDirButton) {
90                 //only perform the action if this is the button that was selected
91
if(fUseDefaultDirButton.getSelection()) {
92                     setDefaultWorkingDir();
93                 }
94             }
95             else if(source == fUseOtherDirButton) {
96                 //only perform the action if this is the button that was selected
97
if(fUseOtherDirButton.getSelection()) {
98                     handleUseOtherWorkingDirButtonSelected();
99                 }
100             }
101         }
102     }
103     
104     private WidgetListener fListener = new WidgetListener();
105     
106     /* (non-Javadoc)
107      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#createControl(org.eclipse.swt.widgets.Composite)
108      */

109     public void createControl(Composite parent) {
110         Font font = parent.getFont();
111         Group group = SWTFactory.createGroup(parent, LauncherMessages.WorkingDirectoryBlock_12, 2, 1, GridData.FILL_HORIZONTAL);
112         setControl(group);
113         PlatformUI.getWorkbench().getHelpSystem().setHelp(group, IJavaDebugHelpContextIds.WORKING_DIRECTORY_BLOCK);
114     //default choice
115
Composite comp = SWTFactory.createComposite(group, font, 2, 2, GridData.FILL_BOTH, 0, 0);
116         fUseDefaultDirButton = SWTFactory.createRadioButton(comp, LauncherMessages.WorkingDirectoryBlock_18);
117         fUseDefaultDirButton.addSelectionListener(fListener);
118         fWorkingDirText = SWTFactory.createSingleText(comp, 1);
119         fWorkingDirText.addModifyListener(fListener);
120         fWorkingDirText.setEnabled(false);
121     //user enter choice
122
fUseOtherDirButton = SWTFactory.createRadioButton(comp, LauncherMessages.WorkingDirectoryBlock_19);
123         fUseOtherDirButton.addSelectionListener(fListener);
124         fOtherWorkingText = SWTFactory.createSingleText(comp, 1);
125         fOtherWorkingText.addModifyListener(fListener);
126     //buttons
127
Composite buttonComp = SWTFactory.createComposite(comp, font, 3, 2, GridData.HORIZONTAL_ALIGN_END);
128         GridLayout ld = (GridLayout)buttonComp.getLayout();
129         ld.marginHeight = 1;
130         ld.marginWidth = 0;
131         fWorkspaceButton = createPushButton(buttonComp, LauncherMessages.WorkingDirectoryBlock_0, null);
132         fWorkspaceButton.addSelectionListener(fListener);
133         fFileSystemButton = createPushButton(buttonComp, LauncherMessages.WorkingDirectoryBlock_1, null);
134         fFileSystemButton.addSelectionListener(fListener);
135         fVariablesButton = createPushButton(buttonComp, LauncherMessages.WorkingDirectoryBlock_17, null);
136         fVariablesButton.addSelectionListener(fListener);
137     }
138         
139     /**
140      * Show a dialog that lets the user select a working directory
141      */

142     private void handleWorkingDirBrowseButtonSelected() {
143         DirectoryDialog dialog = new DirectoryDialog(getShell());
144         dialog.setMessage(LauncherMessages.WorkingDirectoryBlock_7);
145         String JavaDoc currentWorkingDir = getWorkingDirectoryText();
146         if (!currentWorkingDir.trim().equals("")) { //$NON-NLS-1$
147
File JavaDoc path = new File JavaDoc(currentWorkingDir);
148             if (path.exists()) {
149                 dialog.setFilterPath(currentWorkingDir);
150             }
151         }
152         String JavaDoc selectedDirectory = dialog.open();
153         if (selectedDirectory != null) {
154             fOtherWorkingText.setText(selectedDirectory);
155         }
156     }
157
158     /**
159      * Show a dialog that lets the user select a working directory from
160      * the workspace
161      */

162     private void handleWorkspaceDirBrowseButtonSelected() {
163         IContainer currentContainer= getContainer();
164         if (currentContainer == null) {
165             currentContainer = ResourcesPlugin.getWorkspace().getRoot();
166         }
167         ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(), currentContainer, false, LauncherMessages.WorkingDirectoryBlock_4);
168         dialog.showClosedProjects(false);
169         dialog.open();
170         Object JavaDoc[] results = dialog.getResult();
171         if ((results != null) && (results.length > 0) && (results[0] instanceof IPath)) {
172             IPath path = (IPath)results[0];
173             String JavaDoc containerName = path.makeRelative().toString();
174             setOtherWorkingDirectoryText("${workspace_loc:" + containerName + "}"); //$NON-NLS-1$ //$NON-NLS-2$
175
}
176     }
177     
178     /**
179      * Returns the selected workspace container,or <code>null</code>
180      */

181     protected IContainer getContainer() {
182         String JavaDoc path = getWorkingDirectoryText();
183         if (path.length() > 0) {
184             IResource res = null;
185             IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
186             if (path.startsWith("${workspace_loc:")) { //$NON-NLS-1$
187
IStringVariableManager manager = VariablesPlugin.getDefault().getStringVariableManager();
188                 try {
189                     path = manager.performStringSubstitution(path, false);
190                     IContainer[] containers = root.findContainersForLocation(new Path(path));
191                     if (containers.length > 0) {
192                         res = containers[0];
193                     }
194                 }
195                 catch (CoreException e) {}
196             }
197             else {
198                 res = root.findMember(path);
199             }
200             if (res instanceof IContainer) {
201                 return (IContainer)res;
202             }
203         }
204         return null;
205     }
206         
207     /**
208      * The default working dir radio button has been selected.
209      */

210     private void handleUseDefaultWorkingDirButtonSelected() {
211         fWorkspaceButton.setEnabled(false);
212         fOtherWorkingText.setEnabled(false);
213         fVariablesButton.setEnabled(false);
214         fFileSystemButton.setEnabled(false);
215         fUseOtherDirButton.setSelection(false);
216     }
217
218     /**
219      * The other working dir radio button has been selected
220      *
221      * @since 3.2
222      */

223     private void handleUseOtherWorkingDirButtonSelected() {
224         fOtherWorkingText.setEnabled(true);
225         fWorkspaceButton.setEnabled(true);
226         fVariablesButton.setEnabled(true);
227         fFileSystemButton.setEnabled(true);
228         updateLaunchConfigurationDialog();
229     }
230
231     /**
232      * The working dir variables button has been selected
233      */

234     private void handleWorkingDirVariablesButtonSelected() {
235         StringVariableSelectionDialog dialog = new StringVariableSelectionDialog(getShell());
236         dialog.open();
237         String JavaDoc variableText = dialog.getVariableExpression();
238         if (variableText != null) {
239             fOtherWorkingText.insert(variableText);
240         }
241     }
242     
243     /**
244      * Sets the default working directory
245      */

246     protected void setDefaultWorkingDir() {
247         try {
248             ILaunchConfiguration config = getLaunchConfiguration();
249             if (config != null) {
250                 IJavaProject javaProject = JavaRuntime.getJavaProject(config);
251                 if (javaProject != null) {
252                     setDefaultWorkingDirectoryText("${workspace_loc:" + javaProject.getPath().makeRelative().toOSString() + "}"); //$NON-NLS-1$//$NON-NLS-2$
253
return;
254                 }
255             }
256         }
257         catch (CoreException ce) {}
258         setDefaultWorkingDirectoryText(System.getProperty("user.dir")); //$NON-NLS-1$
259
}
260
261     /* (non-Javadoc)
262      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#isValid(org.eclipse.debug.core.ILaunchConfiguration)
263      */

264     public boolean isValid(ILaunchConfiguration config) {
265         setErrorMessage(null);
266         setMessage(null);
267         // if variables are present, we cannot resolve the directory
268
String JavaDoc workingDirPath = getWorkingDirectoryText();
269         if (workingDirPath.indexOf("${") >= 0) { //$NON-NLS-1$
270
IStringVariableManager manager = VariablesPlugin.getDefault().getStringVariableManager();
271             try {
272                 manager.validateStringVariables(workingDirPath);
273             }
274             catch (CoreException e) {
275                 setErrorMessage(e.getMessage());
276                 return false;
277             }
278         }
279         else if (workingDirPath.length() > 0) {
280             IContainer container = getContainer();
281             if (container == null) {
282                 File JavaDoc dir = new File JavaDoc(workingDirPath);
283                 if (dir.isDirectory()) {
284                     return true;
285                 }
286                 setErrorMessage(LauncherMessages.WorkingDirectoryBlock_10);
287                 return false;
288             }
289         } else if (workingDirPath.length() == 0) {
290             setErrorMessage(LauncherMessages.WorkingDirectoryBlock_20);
291             return false;
292         }
293         return true;
294     }
295
296     /**
297      * Defaults are empty.
298      *
299      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#setDefaults(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
300      */

301     public void setDefaults(ILaunchConfigurationWorkingCopy config) {
302         config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY, (String JavaDoc)null);
303     }
304
305     /* (non-Javadoc)
306      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#initializeFrom(org.eclipse.debug.core.ILaunchConfiguration)
307      */

308     public void initializeFrom(ILaunchConfiguration configuration) {
309         setLaunchConfiguration(configuration);
310         try {
311             String JavaDoc wd = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY, (String JavaDoc)null);
312             setDefaultWorkingDir();
313             if (wd != null) {
314                 setOtherWorkingDirectoryText(wd);
315             }
316         }
317         catch (CoreException e) {
318             setErrorMessage(LauncherMessages.JavaArgumentsTab_Exception_occurred_reading_configuration___15 + e.getStatus().getMessage());
319             JDIDebugUIPlugin.log(e);
320         }
321     }
322
323     /* (non-Javadoc)
324      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#performApply(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
325      */

326     public void performApply(ILaunchConfigurationWorkingCopy configuration) {
327         if(fUseDefaultDirButton.getSelection()) {
328             configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY, (String JavaDoc)null);
329         }
330         else {
331             configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY, getWorkingDirectoryText());
332         }
333     }
334     
335     /* (non-Javadoc)
336      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getName()
337      */

338     public String JavaDoc getName() {
339         return LauncherMessages.WorkingDirectoryBlock_Working_Directory_8;
340     }
341     
342     /**
343      * gets the path from the text box that is selected
344      * @return the working directory the user wishes to use
345      * @since 3.2
346      */

347     protected String JavaDoc getWorkingDirectoryText() {
348         if(fUseDefaultDirButton.getSelection()) {
349             return fWorkingDirText.getText().trim();
350         }
351         return fOtherWorkingText.getText().trim();
352     }
353     
354     /**
355      * sets the default working directory text
356      * @param dir the dir to set the widget to
357      * @since 3.2
358      */

359     protected void setDefaultWorkingDirectoryText(String JavaDoc dir) {
360         if(dir != null) {
361             fWorkingDirText.setText(dir);
362             fUseDefaultDirButton.setSelection(true);
363             handleUseDefaultWorkingDirButtonSelected();
364         }
365     }
366     
367     /**
368      * sets the other dir text
369      * @param dir the new text
370      * @since 3.2
371      */

372     protected void setOtherWorkingDirectoryText(String JavaDoc dir) {
373         if(dir != null) {
374             fOtherWorkingText.setText(dir);
375             fUseDefaultDirButton.setSelection(false);
376             fUseOtherDirButton.setSelection(true);
377             handleUseOtherWorkingDirButtonSelected();
378         }
379     }
380     
381     /**
382      * Sets the java project currently specified by the
383      * given launch config, if any.
384      */

385     protected void setLaunchConfiguration(ILaunchConfiguration config) {
386         fLaunchConfiguration = config;
387     }
388     
389     /**
390      * Returns the current java project context
391      */

392     protected ILaunchConfiguration getLaunchConfiguration() {
393         return fLaunchConfiguration;
394     }
395     
396     /**
397      * Allows this entire block to be enabled/disabled
398      * @param enabled whether to enable it or not
399      */

400     protected void setEnabled(boolean enabled) {
401         fUseDefaultDirButton.setEnabled(enabled);
402         fUseOtherDirButton.setEnabled(enabled);
403         if(fOtherWorkingText.isEnabled()) {
404             fOtherWorkingText.setEnabled(enabled);
405             fWorkspaceButton.setEnabled(enabled);
406             fVariablesButton.setEnabled(enabled);
407             fFileSystemButton.setEnabled(enabled);
408         }
409         // in the case where the 'other' text is selected and we want to enable
410
if(fUseOtherDirButton.getSelection() && enabled == true) {
411             fOtherWorkingText.setEnabled(enabled);
412         }
413     }
414     
415 }
416
417
Popular Tags