1 11 package org.eclipse.ui.externaltools.internal.launchConfigurations; 12 13 14 import java.io.File ; 15 import org.eclipse.core.resources.IResource; 16 import org.eclipse.core.resources.ResourcesPlugin; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IPath; 19 import org.eclipse.core.variables.IStringVariableManager; 20 import org.eclipse.core.variables.VariablesPlugin; 21 import org.eclipse.debug.core.ILaunchConfiguration; 22 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 23 import org.eclipse.debug.ui.AbstractLaunchConfigurationTab; 24 import org.eclipse.debug.ui.StringVariableSelectionDialog; 25 import org.eclipse.jface.dialogs.Dialog; 26 import org.eclipse.jface.dialogs.IDialogConstants; 27 import org.eclipse.swt.SWT; 28 import org.eclipse.swt.accessibility.AccessibleAdapter; 29 import org.eclipse.swt.accessibility.AccessibleEvent; 30 import org.eclipse.swt.events.ModifyEvent; 31 import org.eclipse.swt.events.ModifyListener; 32 import org.eclipse.swt.events.SelectionAdapter; 33 import org.eclipse.swt.events.SelectionEvent; 34 import org.eclipse.swt.graphics.Image; 35 import org.eclipse.swt.layout.GridData; 36 import org.eclipse.swt.layout.GridLayout; 37 import org.eclipse.swt.widgets.Button; 38 import org.eclipse.swt.widgets.Composite; 39 import org.eclipse.swt.widgets.Control; 40 import org.eclipse.swt.widgets.DirectoryDialog; 41 import org.eclipse.swt.widgets.FileDialog; 42 import org.eclipse.swt.widgets.Group; 43 import org.eclipse.swt.widgets.Label; 44 import org.eclipse.swt.widgets.Text; 45 import org.eclipse.ui.dialogs.ContainerSelectionDialog; 46 import org.eclipse.ui.dialogs.ResourceSelectionDialog; 47 import org.eclipse.ui.externaltools.internal.model.ExternalToolsImages; 48 import org.eclipse.ui.externaltools.internal.model.ExternalToolsPlugin; 49 import org.eclipse.ui.externaltools.internal.model.IExternalToolConstants; 50 51 56 public abstract class ExternalToolsMainTab extends AbstractLaunchConfigurationTab { 57 public final static String FIRST_EDIT = "editedByExternalToolsMainTab"; 59 protected Text locationField; 60 protected Text workDirectoryField; 61 protected Button fileLocationButton; 62 protected Button workspaceLocationButton; 63 protected Button variablesLocationButton; 64 protected Button fileWorkingDirectoryButton; 65 protected Button workspaceWorkingDirectoryButton; 66 protected Button variablesWorkingDirectoryButton; 67 68 protected Text argumentField; 69 protected Button argumentVariablesButton; 70 71 protected SelectionAdapter selectionAdapter; 72 73 protected boolean fInitializing= false; 74 private boolean userEdited= false; 75 76 protected WidgetListener fListener= new WidgetListener(); 77 78 81 protected class WidgetListener extends SelectionAdapter implements ModifyListener { 82 public void modifyText(ModifyEvent e) { 83 if (!fInitializing) { 84 setDirty(true); 85 userEdited= true; 86 updateLaunchConfigurationDialog(); 87 } 88 } 89 public void widgetSelected(SelectionEvent e) { 90 setDirty(true); 91 Object source= e.getSource(); 92 if (source == workspaceLocationButton) { 93 handleWorkspaceLocationButtonSelected(); 94 } else if (source == fileLocationButton) { 95 handleFileLocationButtonSelected(); 96 } else if (source == workspaceWorkingDirectoryButton) { 97 handleWorkspaceWorkingDirectoryButtonSelected(); 98 } else if (source == fileWorkingDirectoryButton) { 99 handleFileWorkingDirectoryButtonSelected(); 100 } else if (source == argumentVariablesButton) { 101 handleVariablesButtonSelected(argumentField); 102 } else if (source == variablesLocationButton) { 103 handleVariablesButtonSelected(locationField); 104 } else if (source == variablesWorkingDirectoryButton) { 105 handleVariablesButtonSelected(workDirectoryField); 106 } 107 } 108 109 } 110 111 114 public void createControl(Composite parent) { 115 Composite mainComposite = new Composite(parent, SWT.NONE); 116 setControl(mainComposite); 117 mainComposite.setFont(parent.getFont()); 118 GridLayout layout = new GridLayout(); 119 layout.numColumns = 1; 120 GridData gridData = new GridData(GridData.FILL_HORIZONTAL); 121 mainComposite.setLayout(layout); 122 mainComposite.setLayoutData(gridData); 123 124 createLocationComponent(mainComposite); 125 createWorkDirectoryComponent(mainComposite); 126 createArgumentComponent(mainComposite); 127 createVerticalSpacer(mainComposite, 1); 128 129 Dialog.applyDialogFont(parent); 130 } 131 132 138 protected void createLocationComponent(Composite parent) { 139 Group group = new Group(parent, SWT.NONE); 140 String locationLabel = getLocationLabel(); 141 group.setText(locationLabel); 142 GridLayout layout = new GridLayout(); 143 layout.numColumns = 1; 144 GridData gridData = new GridData(GridData.FILL_HORIZONTAL); 145 group.setLayout(layout); 146 group.setLayoutData(gridData); 147 148 locationField = new Text(group, SWT.BORDER); 149 gridData = new GridData(GridData.FILL_HORIZONTAL); 150 gridData.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH; 151 locationField.setLayoutData(gridData); 152 locationField.addModifyListener(fListener); 153 addControlAccessibleListener(locationField, group.getText()); 154 155 Composite buttonComposite = new Composite(group, SWT.NONE); 156 layout = new GridLayout(); 157 layout.marginHeight = 0; 158 layout.marginWidth = 0; 159 layout.numColumns = 3; 160 gridData = new GridData(GridData.HORIZONTAL_ALIGN_END); 161 buttonComposite.setLayout(layout); 162 buttonComposite.setLayoutData(gridData); 163 buttonComposite.setFont(parent.getFont()); 164 165 workspaceLocationButton= createPushButton(buttonComposite, ExternalToolsLaunchConfigurationMessages.ExternalToolsMainTab__Browse_Workspace____3, null); 166 workspaceLocationButton.addSelectionListener(fListener); 167 addControlAccessibleListener(workspaceLocationButton, group.getText() + " " + workspaceLocationButton.getText()); 169 fileLocationButton= createPushButton(buttonComposite, ExternalToolsLaunchConfigurationMessages.ExternalToolsMainTab_Brows_e_File_System____4, null); 170 fileLocationButton.addSelectionListener(fListener); 171 addControlAccessibleListener(fileLocationButton, group.getText() + " " + fileLocationButton.getText()); 173 variablesLocationButton = createPushButton(buttonComposite, ExternalToolsLaunchConfigurationMessages.ExternalToolsMainTab_31, null); 174 variablesLocationButton.addSelectionListener(fListener); 175 addControlAccessibleListener(variablesLocationButton, group.getText() + " " + variablesLocationButton.getText()); } 177 178 181 protected String getLocationLabel() { 182 return ExternalToolsLaunchConfigurationMessages.ExternalToolsMainTab__Location___2; 183 } 184 185 191 protected void createWorkDirectoryComponent(Composite parent) { 192 Group group = new Group(parent, SWT.NONE); 193 String groupName = getWorkingDirectoryLabel(); 194 group.setText(groupName); 195 GridLayout layout = new GridLayout(); 196 layout.numColumns = 1; 197 GridData gridData = new GridData(GridData.FILL_HORIZONTAL); 198 group.setLayout(layout); 199 group.setLayoutData(gridData); 200 201 workDirectoryField = new Text(group, SWT.BORDER); 202 GridData data = new GridData(GridData.FILL_HORIZONTAL); 203 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH; 204 workDirectoryField.setLayoutData(data); 205 workDirectoryField.addModifyListener(fListener); 206 addControlAccessibleListener(workDirectoryField,group.getText()); 207 208 Composite buttonComposite = new Composite(group, SWT.NONE); 209 layout = new GridLayout(); 210 layout.marginWidth = 0; 211 layout.marginHeight = 0; 212 layout.numColumns = 3; 213 gridData = new GridData(GridData.HORIZONTAL_ALIGN_END); 214 buttonComposite.setLayout(layout); 215 buttonComposite.setLayoutData(gridData); 216 buttonComposite.setFont(parent.getFont()); 217 218 workspaceWorkingDirectoryButton= createPushButton(buttonComposite, ExternalToolsLaunchConfigurationMessages.ExternalToolsMainTab_Browse_Wor_kspace____6, null); 219 workspaceWorkingDirectoryButton.addSelectionListener(fListener); 220 addControlAccessibleListener(workspaceWorkingDirectoryButton, group.getText() + " " + workspaceWorkingDirectoryButton.getText()); 222 fileWorkingDirectoryButton= createPushButton(buttonComposite, ExternalToolsLaunchConfigurationMessages.ExternalToolsMainTab_Browse_F_ile_System____7, null); 223 fileWorkingDirectoryButton.addSelectionListener(fListener); 224 addControlAccessibleListener(fileWorkingDirectoryButton, group.getText() + " " + fileLocationButton.getText()); 226 variablesWorkingDirectoryButton = createPushButton(buttonComposite, ExternalToolsLaunchConfigurationMessages.ExternalToolsMainTab_32, null); 227 variablesWorkingDirectoryButton.addSelectionListener(fListener); 228 addControlAccessibleListener(variablesWorkingDirectoryButton, group.getText() + " " + variablesWorkingDirectoryButton.getText()); } 230 231 235 protected String getWorkingDirectoryLabel() { 236 return ExternalToolsLaunchConfigurationMessages.ExternalToolsMainTab_Working__Directory__5; 237 } 238 239 245 protected void createArgumentComponent(Composite parent) { 246 Group group = new Group(parent, SWT.NONE); 247 String groupName = ExternalToolsLaunchConfigurationMessages.ExternalToolsMainTab__Arguments___1; 248 group.setText(groupName); 249 GridLayout layout = new GridLayout(); 250 layout.numColumns = 1; 251 GridData gridData = new GridData(GridData.FILL_BOTH); 252 group.setLayout(layout); 253 group.setLayoutData(gridData); 254 group.setFont(parent.getFont()); 255 256 argumentField = new Text(group, SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.V_SCROLL); 257 gridData = new GridData(GridData.FILL_BOTH); 258 gridData.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH; 259 gridData.heightHint = 30; 260 argumentField.setLayoutData(gridData); 261 argumentField.addModifyListener(fListener); 262 addControlAccessibleListener(argumentField, group.getText()); 263 264 Composite composite = new Composite(group, SWT.NONE); 265 layout = new GridLayout(); 266 layout.numColumns= 1; 267 layout.marginHeight= 0; 268 layout.marginWidth= 0; 269 gridData = new GridData(GridData.HORIZONTAL_ALIGN_END); 270 composite.setLayout(layout); 271 composite.setLayoutData(gridData); 272 composite.setFont(parent.getFont()); 273 274 argumentVariablesButton= createPushButton(composite, ExternalToolsLaunchConfigurationMessages.ExternalToolsMainTab_Varia_bles____2, null); 275 argumentVariablesButton.addSelectionListener(fListener); 276 addControlAccessibleListener(argumentVariablesButton, argumentVariablesButton.getText()); 278 Label instruction = new Label(group, SWT.NONE); 279 instruction.setText(ExternalToolsLaunchConfigurationMessages.ExternalToolsMainTab_3); 280 gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL); 281 gridData.horizontalSpan = 2; 282 instruction.setLayoutData(gridData); 283 } 284 285 288 public void setDefaults(ILaunchConfigurationWorkingCopy configuration) { 289 configuration.setAttribute(FIRST_EDIT, true); 290 } 291 292 295 public void initializeFrom(ILaunchConfiguration configuration) { 296 fInitializing= true; 297 updateLocation(configuration); 298 updateWorkingDirectory(configuration); 299 updateArgument(configuration); 300 fInitializing= false; 301 setDirty(false); 302 } 303 304 308 protected void updateWorkingDirectory(ILaunchConfiguration configuration) { 309 String workingDir= ""; try { 311 workingDir= configuration.getAttribute(IExternalToolConstants.ATTR_WORKING_DIRECTORY, ""); } catch (CoreException ce) { 313 ExternalToolsPlugin.getDefault().log(ExternalToolsLaunchConfigurationMessages.ExternalToolsMainTab_Error_reading_configuration_10, ce); 314 } 315 workDirectoryField.setText(workingDir); 316 } 317 318 322 protected void updateLocation(ILaunchConfiguration configuration) { 323 String location= ""; try { 325 location= configuration.getAttribute(IExternalToolConstants.ATTR_LOCATION, ""); } catch (CoreException ce) { 327 ExternalToolsPlugin.getDefault().log(ExternalToolsLaunchConfigurationMessages.ExternalToolsMainTab_Error_reading_configuration_10, ce); 328 } 329 locationField.setText(location); 330 } 331 332 336 protected void updateArgument(ILaunchConfiguration configuration) { 337 String arguments= ""; try { 339 arguments= configuration.getAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS, ""); } catch (CoreException ce) { 341 ExternalToolsPlugin.getDefault().log(ExternalToolsLaunchConfigurationMessages.ExternalToolsMainTab_Error_reading_configuration_7, ce); 342 } 343 argumentField.setText(arguments); 344 } 345 346 349 public void performApply(ILaunchConfigurationWorkingCopy configuration) { 350 String location= locationField.getText().trim(); 351 if (location.length() == 0) { 352 configuration.setAttribute(IExternalToolConstants.ATTR_LOCATION, (String )null); 353 } else { 354 configuration.setAttribute(IExternalToolConstants.ATTR_LOCATION, location); 355 } 356 357 String workingDirectory= workDirectoryField.getText().trim(); 358 if (workingDirectory.length() == 0) { 359 configuration.setAttribute(IExternalToolConstants.ATTR_WORKING_DIRECTORY, (String )null); 360 } else { 361 configuration.setAttribute(IExternalToolConstants.ATTR_WORKING_DIRECTORY, workingDirectory); 362 } 363 364 String arguments= argumentField.getText().trim(); 365 if (arguments.length() == 0) { 366 configuration.setAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS, (String )null); 367 } else { 368 configuration.setAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS, arguments); 369 } 370 371 if(userEdited) { 372 configuration.setAttribute(FIRST_EDIT, (String )null); 373 } 374 } 375 376 379 public String getName() { 380 return ExternalToolsLaunchConfigurationMessages.ExternalToolsMainTab__Main_17; 381 } 382 383 386 public boolean isValid(ILaunchConfiguration launchConfig) { 387 setErrorMessage(null); 388 setMessage(null); 389 boolean newConfig = false; 390 try { 391 newConfig = launchConfig.getAttribute(FIRST_EDIT, false); 392 } catch (CoreException e) { 393 } 395 return validateLocation(newConfig) && validateWorkDirectory(); 396 } 397 398 401 protected boolean validateLocation(boolean newConfig) { 402 String location = locationField.getText().trim(); 403 if (location.length() < 1) { 404 if (newConfig) { 405 setErrorMessage(null); 406 setMessage(ExternalToolsLaunchConfigurationMessages.ExternalToolsMainTab_30); 407 } else { 408 setErrorMessage(ExternalToolsLaunchConfigurationMessages.ExternalToolsMainTab_External_tool_location_cannot_be_empty_18); 409 setMessage(null); 410 } 411 return false; 412 } 413 414 String expandedLocation= null; 415 try { 416 expandedLocation= resolveValue(location); 417 if (expandedLocation == null) { return true; 419 } 420 } catch (CoreException e) { 421 setErrorMessage(e.getStatus().getMessage()); 422 return false; 423 } 424 425 File file = new File (expandedLocation); 426 if (!file.exists()) { if (!newConfig) { 428 setErrorMessage(ExternalToolsLaunchConfigurationMessages.ExternalToolsMainTab_External_tool_location_does_not_exist_19); 429 } 430 return false; 431 } 432 if (!file.isFile()) { 433 if (!newConfig) { 434 setErrorMessage(ExternalToolsLaunchConfigurationMessages.ExternalToolsMainTab_External_tool_location_specified_is_not_a_file_20); 435 } 436 return false; 437 } 438 return true; 439 } 440 441 447 private void validateVaribles(String expression) throws CoreException { 448 IStringVariableManager manager = VariablesPlugin.getDefault().getStringVariableManager(); 449 manager.validateStringVariables(expression); 450 } 451 452 private String resolveValue(String expression) throws CoreException { 453 String expanded= null; 454 try { 455 expanded= getValue(expression); 456 } catch (CoreException e) { validateVaribles(expression); 458 return null; 459 } 460 return expanded; 461 } 462 463 470 private String getValue(String expression) throws CoreException { 471 IStringVariableManager manager = VariablesPlugin.getDefault().getStringVariableManager(); 472 return manager.performStringSubstitution(expression); 473 } 474 475 478 protected boolean validateWorkDirectory() { 479 String dir = workDirectoryField.getText().trim(); 480 if (dir.length() <= 0) { 481 return true; 482 } 483 484 String expandedDir= null; 485 try { 486 expandedDir= resolveValue(dir); 487 if (expandedDir == null) { return true; 489 } 490 } catch (CoreException e) { 491 setErrorMessage(e.getStatus().getMessage()); 492 return false; 493 } 494 495 File file = new File (expandedDir); 496 if (!file.exists()) { setErrorMessage(ExternalToolsLaunchConfigurationMessages.ExternalToolsMainTab_External_tool_working_directory_does_not_exist_or_is_invalid_21); 498 return false; 499 } 500 if (!file.isDirectory()) { 501 setErrorMessage(ExternalToolsLaunchConfigurationMessages.ExternalToolsMainTab_Not_a_directory); 502 return false; 503 } 504 return true; 505 } 506 507 511 protected void handleFileLocationButtonSelected() { 512 FileDialog fileDialog = new FileDialog(getShell(), SWT.NONE); 513 fileDialog.setFileName(locationField.getText()); 514 String text= fileDialog.open(); 515 if (text != null) { 516 locationField.setText(text); 517 } 518 } 519 520 525 protected void handleWorkspaceLocationButtonSelected() { 526 ResourceSelectionDialog dialog; 527 dialog = new ResourceSelectionDialog(getShell(), ResourcesPlugin.getWorkspace().getRoot(), ExternalToolsLaunchConfigurationMessages.ExternalToolsMainTab_Select_a_resource_22); 528 dialog.open(); 529 Object [] results = dialog.getResult(); 530 if (results == null || results.length < 1) { 531 return; 532 } 533 IResource resource = (IResource)results[0]; 534 locationField.setText(newVariableExpression("workspace_loc", resource.getFullPath().toString())); } 536 537 542 protected void handleWorkspaceWorkingDirectoryButtonSelected() { 543 ContainerSelectionDialog containerDialog; 544 containerDialog = new ContainerSelectionDialog( 545 getShell(), 546 ResourcesPlugin.getWorkspace().getRoot(), 547 false, 548 ExternalToolsLaunchConfigurationMessages.ExternalToolsMainTab_23); 549 containerDialog.open(); 550 Object [] resource = containerDialog.getResult(); 551 String text= null; 552 if (resource != null && resource.length > 0) { 553 text= newVariableExpression("workspace_loc", ((IPath)resource[0]).toString()); } 555 if (text != null) { 556 workDirectoryField.setText(text); 557 } 558 } 559 560 564 protected String newVariableExpression(String varName, String arg) { 565 return VariablesPlugin.getDefault().getStringVariableManager().generateVariableExpression(varName, arg); 566 } 567 568 571 protected void handleFileWorkingDirectoryButtonSelected() { 572 DirectoryDialog dialog = new DirectoryDialog(getShell(), SWT.SAVE); 573 dialog.setMessage(ExternalToolsLaunchConfigurationMessages.ExternalToolsMainTab_23); 574 dialog.setFilterPath(workDirectoryField.getText()); 575 String text= dialog.open(); 576 if (text != null) { 577 workDirectoryField.setText(text); 578 } 579 } 580 581 586 private void handleVariablesButtonSelected(Text textField) { 587 String variable = getVariable(); 588 if (variable != null) { 589 textField.insert(variable); 590 } 591 } 592 593 597 private String getVariable() { 598 StringVariableSelectionDialog dialog = new StringVariableSelectionDialog(getShell()); 599 dialog.open(); 600 return dialog.getVariableExpression(); 601 } 602 603 606 public Image getImage() { 607 return ExternalToolsImages.getImage(IExternalToolConstants.IMG_TAB_MAIN); 608 } 609 610 613 public void deactivated(ILaunchConfigurationWorkingCopy workingCopy) { 614 } 615 616 619 public void activated(ILaunchConfigurationWorkingCopy workingCopy) { 620 } 621 622 625 public void addControlAccessibleListener(Control control, String controlName) { 626 String [] strs = controlName.split("&"); StringBuffer stripped = new StringBuffer (); 629 for (int i = 0; i < strs.length; i++) { 630 stripped.append(strs[i]); 631 } 632 control.getAccessible().addAccessibleListener(new ControlAccessibleListener(stripped.toString())); 633 } 634 635 private class ControlAccessibleListener extends AccessibleAdapter { 636 private String controlName; 637 ControlAccessibleListener(String name) { 638 controlName = name; 639 } 640 public void getName(AccessibleEvent e) { 641 e.result = controlName; 642 } 643 644 } 645 } 646 | Popular Tags |