1 11 package org.eclipse.jdt.internal.debug.ui.launcher; 12 13 import java.io.File ; 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 53 public class WorkingDirectoryBlock extends JavaLaunchTab { 54 55 private Button fWorkspaceButton; 57 private Button fFileSystemButton; 58 private Button fVariablesButton; 59 60 private Button fUseDefaultDirButton = null; 62 private Button fUseOtherDirButton = null; 63 private Text fOtherWorkingText = null; 64 private Text fWorkingDirText; 65 66 69 private ILaunchConfiguration fLaunchConfiguration; 70 71 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 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 if(fUseDefaultDirButton.getSelection()) { 92 setDefaultWorkingDir(); 93 } 94 } 95 else if(source == fUseOtherDirButton) { 96 if(fUseOtherDirButton.getSelection()) { 98 handleUseOtherWorkingDirButtonSelected(); 99 } 100 } 101 } 102 } 103 104 private WidgetListener fListener = new WidgetListener(); 105 106 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 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 fUseOtherDirButton = SWTFactory.createRadioButton(comp, LauncherMessages.WorkingDirectoryBlock_19); 123 fUseOtherDirButton.addSelectionListener(fListener); 124 fOtherWorkingText = SWTFactory.createSingleText(comp, 1); 125 fOtherWorkingText.addModifyListener(fListener); 126 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 142 private void handleWorkingDirBrowseButtonSelected() { 143 DirectoryDialog dialog = new DirectoryDialog(getShell()); 144 dialog.setMessage(LauncherMessages.WorkingDirectoryBlock_7); 145 String currentWorkingDir = getWorkingDirectoryText(); 146 if (!currentWorkingDir.trim().equals("")) { File path = new File (currentWorkingDir); 148 if (path.exists()) { 149 dialog.setFilterPath(currentWorkingDir); 150 } 151 } 152 String selectedDirectory = dialog.open(); 153 if (selectedDirectory != null) { 154 fOtherWorkingText.setText(selectedDirectory); 155 } 156 } 157 158 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 [] results = dialog.getResult(); 171 if ((results != null) && (results.length > 0) && (results[0] instanceof IPath)) { 172 IPath path = (IPath)results[0]; 173 String containerName = path.makeRelative().toString(); 174 setOtherWorkingDirectoryText("${workspace_loc:" + containerName + "}"); } 176 } 177 178 181 protected IContainer getContainer() { 182 String path = getWorkingDirectoryText(); 183 if (path.length() > 0) { 184 IResource res = null; 185 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); 186 if (path.startsWith("${workspace_loc:")) { 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 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 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 234 private void handleWorkingDirVariablesButtonSelected() { 235 StringVariableSelectionDialog dialog = new StringVariableSelectionDialog(getShell()); 236 dialog.open(); 237 String variableText = dialog.getVariableExpression(); 238 if (variableText != null) { 239 fOtherWorkingText.insert(variableText); 240 } 241 } 242 243 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() + "}"); return; 254 } 255 } 256 } 257 catch (CoreException ce) {} 258 setDefaultWorkingDirectoryText(System.getProperty("user.dir")); } 260 261 264 public boolean isValid(ILaunchConfiguration config) { 265 setErrorMessage(null); 266 setMessage(null); 267 String workingDirPath = getWorkingDirectoryText(); 269 if (workingDirPath.indexOf("${") >= 0) { 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 dir = new File (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 301 public void setDefaults(ILaunchConfigurationWorkingCopy config) { 302 config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY, (String )null); 303 } 304 305 308 public void initializeFrom(ILaunchConfiguration configuration) { 309 setLaunchConfiguration(configuration); 310 try { 311 String wd = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY, (String )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 326 public void performApply(ILaunchConfigurationWorkingCopy configuration) { 327 if(fUseDefaultDirButton.getSelection()) { 328 configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY, (String )null); 329 } 330 else { 331 configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY, getWorkingDirectoryText()); 332 } 333 } 334 335 338 public String getName() { 339 return LauncherMessages.WorkingDirectoryBlock_Working_Directory_8; 340 } 341 342 347 protected String getWorkingDirectoryText() { 348 if(fUseDefaultDirButton.getSelection()) { 349 return fWorkingDirText.getText().trim(); 350 } 351 return fOtherWorkingText.getText().trim(); 352 } 353 354 359 protected void setDefaultWorkingDirectoryText(String dir) { 360 if(dir != null) { 361 fWorkingDirText.setText(dir); 362 fUseDefaultDirButton.setSelection(true); 363 handleUseDefaultWorkingDirButtonSelected(); 364 } 365 } 366 367 372 protected void setOtherWorkingDirectoryText(String dir) { 373 if(dir != null) { 374 fOtherWorkingText.setText(dir); 375 fUseDefaultDirButton.setSelection(false); 376 fUseOtherDirButton.setSelection(true); 377 handleUseOtherWorkingDirButtonSelected(); 378 } 379 } 380 381 385 protected void setLaunchConfiguration(ILaunchConfiguration config) { 386 fLaunchConfiguration = config; 387 } 388 389 392 protected ILaunchConfiguration getLaunchConfiguration() { 393 return fLaunchConfiguration; 394 } 395 396 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 if(fUseOtherDirButton.getSelection() && enabled == true) { 411 fOtherWorkingText.setEnabled(enabled); 412 } 413 } 414 415 } 416 417 | Popular Tags |