1 package net.sourceforge.phpdt.externaltools.launchConfigurations;
3 /***********************************************************************************************************************************
4 * Copyright (c) 2000, 2002 IBM Corp. All rights reserved. This file is made available under the terms of the Common Public License
5 * v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/cpl-v10.html
6 **********************************************************************************************************************************/
10 import net.sourceforge.phpdt.externaltools.group.IGroupDialogPage;
11 import net.sourceforge.phpdt.externaltools.internal.dialog.ExternalToolVariableForm;
12 import net.sourceforge.phpdt.externaltools.internal.model.ExternalToolsImages;
13 import net.sourceforge.phpdt.externaltools.internal.registry.ExternalToolVariable;
14 import net.sourceforge.phpdt.externaltools.model.IExternalToolConstants;
15 import net.sourceforge.phpdt.externaltools.model.ToolUtil;
16 import net.sourceforge.phpdt.externaltools.variable.ExpandVariableContext;
17 import net.sourceforge.phpeclipse.externaltools.ExternalToolsPlugin;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.resources.ResourcesPlugin;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IPath;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.core.runtime.MultiStatus;
25 import org.eclipse.debug.core.ILaunchConfiguration;
26 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
27 import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
28 import org.eclipse.debug.ui.ILaunchConfigurationTab;
29 import org.eclipse.jface.dialogs.IDialogConstants;
30 import org.eclipse.jface.dialogs.IMessageProvider;
31 import org.eclipse.jface.preference.IPreferenceStore;
32 import org.eclipse.swt.SWT;
33 import org.eclipse.swt.events.ModifyEvent;
34 import org.eclipse.swt.events.ModifyListener;
35 import org.eclipse.swt.events.SelectionAdapter;
36 import org.eclipse.swt.events.SelectionEvent;
37 import org.eclipse.swt.events.SelectionListener;
38 import org.eclipse.swt.graphics.Font;
39 import org.eclipse.swt.graphics.Image;
40 import org.eclipse.swt.layout.GridData;
41 import org.eclipse.swt.layout.GridLayout;
42 import org.eclipse.swt.widgets.Button;
43 import org.eclipse.swt.widgets.Combo;
44 import org.eclipse.swt.widgets.Composite;
45 import org.eclipse.swt.widgets.Control;
46 import org.eclipse.swt.widgets.DirectoryDialog;
47 import org.eclipse.swt.widgets.FileDialog;
48 import org.eclipse.swt.widgets.Label;
49 import org.eclipse.swt.widgets.Shell;
50 import org.eclipse.swt.widgets.Text;
51 import org.eclipse.ui.dialogs.ContainerSelectionDialog;
52 import org.eclipse.ui.dialogs.ResourceSelectionDialog;
53 import org.eclipse.ui.dialogs.SelectionDialog;
55 public class ExternalToolsMainTab extends AbstractLaunchConfigurationTab {
57 protected Combo locationField;
59 protected Text workDirectoryField;
61 protected Button fileLocationButton;
63 protected Button workspaceLocationButton;
65 protected Button fileWorkingDirectoryButton;
67 protected Button workspaceWorkingDirectoryButton;
69 protected Button runBackgroundButton;
71 protected Text argumentField;
73 protected Button variableButton;
75 protected SelectionAdapter selectionAdapter;
77 protected ModifyListener modifyListener = new ModifyListener() {
78 public void modifyText(ModifyEvent e) {
79 updateLaunchConfigurationDialog();
84 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#createControl(org.eclipse.swt.widgets.Composite)
86 public void createControl(Composite parent) {
87 Composite mainComposite = new Composite(parent, SWT.NONE);
88 setControl(mainComposite);
89 GridLayout layout = new GridLayout();
90 layout.numColumns = 2;
91 GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
92 mainComposite.setLayout(layout);
93 mainComposite.setLayoutData(gridData);
94 mainComposite.setFont(parent.getFont());
95 createLocationComponent(mainComposite);
96 createWorkDirectoryComponent(mainComposite);
97 createArgumentComponent(mainComposite);
98 createVerticalSpacer(mainComposite, 2);
99 createRunBackgroundComponent(mainComposite);
103 * Creates the controls needed to edit the location attribute of an external
107 * the composite to create the controls in
109 protected void createLocationComponent(Composite parent) {
110 Font font = parent.getFont();
112 Composite composite = new Composite(parent, SWT.NONE);
113 GridLayout layout = new GridLayout();
114 layout.marginWidth = 0;
115 layout.marginHeight = 0;
116 layout.numColumns = 1;
117 GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
118 composite.setLayout(layout);
119 composite.setLayoutData(gridData);
121 Label label = new Label(composite, SWT.NONE);
122 label.setText(ExternalToolsLaunchConfigurationMessages
123 .getString("ExternalToolsMainTab.&Location___2")); //$NON-NLS-1$
126 final IPreferenceStore store = ExternalToolsPlugin.getDefault()
127 .getPreferenceStore();
128 locationField = new Combo(composite, SWT.DROP_DOWN | SWT.BORDER);
129 GridData data = new GridData(GridData.FILL_HORIZONTAL);
130 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
131 locationField.setLayoutData(data);
132 locationField.setFont(font);
133 locationField.add(store.getString(ExternalToolsPlugin.PHP_RUN_PREF), 0);
134 locationField.add(store.getString(ExternalToolsPlugin.APACHE_RUN_PREF),
136 locationField.add(store.getString(ExternalToolsPlugin.MYSQL_RUN_PREF),
139 store.getString(ExternalToolsPlugin.XAMPP_START_PREF), 3);
140 locationField.add(store.getString(ExternalToolsPlugin.XAMPP_STOP_PREF),
142 Composite buttonComposite = new Composite(parent, SWT.NONE);
143 layout = new GridLayout();
144 layout.marginWidth = 0;
145 layout.marginHeight = 0;
146 layout.numColumns = 1;
147 gridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
148 buttonComposite.setLayout(layout);
149 buttonComposite.setLayoutData(gridData);
150 buttonComposite.setFont(font);
152 createVerticalSpacer(buttonComposite, 1);
154 workspaceLocationButton = createPushButton(
156 ExternalToolsLaunchConfigurationMessages
157 .getString("ExternalToolsMainTab.&Browse_Workspace..._3"), null); //$NON-NLS-1$
158 workspaceLocationButton.addSelectionListener(new SelectionAdapter() {
159 public void widgetSelected(SelectionEvent evt) {
160 handleWorkspaceLocationButtonSelected();
163 fileLocationButton = createPushButton(
165 ExternalToolsLaunchConfigurationMessages
166 .getString("ExternalToolsMainTab.Brows&e_File_System..._4"), null); //$NON-NLS-1$
167 fileLocationButton.addSelectionListener(new SelectionAdapter() {
168 public void widgetSelected(SelectionEvent evt) {
169 handleLocationButtonSelected();
175 * Creates the controls needed to edit the working directory attribute of an
179 * the composite to create the controls in
181 protected void createWorkDirectoryComponent(Composite parent) {
182 Font font = parent.getFont();
184 Composite composite = new Composite(parent, SWT.NONE);
185 GridLayout layout = new GridLayout();
186 layout.marginWidth = 0;
187 layout.marginHeight = 0;
188 layout.numColumns = 1;
189 GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
190 composite.setLayout(layout);
191 composite.setLayoutData(gridData);
193 Label label = new Label(composite, SWT.NONE);
194 label.setText(ExternalToolsLaunchConfigurationMessages
195 .getString("ExternalToolsMainTab.Working_&Directory__5")); //$NON-NLS-1$
198 workDirectoryField = new Text(composite, SWT.BORDER);
199 GridData data = new GridData(GridData.FILL_HORIZONTAL);
200 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
201 workDirectoryField.setLayoutData(data);
202 workDirectoryField.setFont(font);
204 Composite buttonComposite = new Composite(parent, SWT.NONE);
205 layout = new GridLayout();
206 layout.marginWidth = 0;
207 layout.marginHeight = 0;
208 layout.numColumns = 1;
209 gridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
210 buttonComposite.setLayout(layout);
211 buttonComposite.setLayoutData(gridData);
212 buttonComposite.setFont(font);
214 createVerticalSpacer(buttonComposite, 1);
215 workspaceWorkingDirectoryButton = createPushButton(
217 ExternalToolsLaunchConfigurationMessages
218 .getString("ExternalToolsMainTab.Browse_Wor&kspace..._6"), null); //$NON-NLS-1$
219 workspaceWorkingDirectoryButton
220 .addSelectionListener(new SelectionAdapter() {
221 public void widgetSelected(SelectionEvent evt) {
222 handleWorkspaceWorkingDirectoryButtonSelected();
225 fileWorkingDirectoryButton = createPushButton(
227 ExternalToolsLaunchConfigurationMessages
228 .getString("ExternalToolsMainTab.Browse_F&ile_System..._7"), null); //$NON-NLS-1$
229 fileWorkingDirectoryButton.addSelectionListener(new SelectionAdapter() {
230 public void widgetSelected(SelectionEvent evt) {
231 handleFileWorkingDirectoryButtonSelected();
237 * Creates the controls needed to edit the argument and prompt for argument
238 * attributes of an external tool
241 * the composite to create the controls in
243 protected void createArgumentComponent(Composite parent) {
244 Font font = parent.getFont();
246 Label label = new Label(parent, SWT.NONE);
247 label.setText(ExternalToolsLaunchConfigurationMessages
248 .getString("ExternalToolsOptionTab.&Arguments___1")); //$NON-NLS-1$
249 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
250 data.horizontalSpan = 2;
251 label.setLayoutData(data);
254 argumentField = new Text(parent, SWT.BORDER);
255 data = new GridData(GridData.FILL_HORIZONTAL);
256 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
257 argumentField.setLayoutData(data);
258 argumentField.setFont(font);
259 argumentField.addModifyListener(new ModifyListener() {
260 public void modifyText(ModifyEvent e) {
261 updateLaunchConfigurationDialog();
265 variableButton = createPushButton(
267 ExternalToolsLaunchConfigurationMessages
268 .getString("ExternalToolsOptionTab.Varia&bles..._2"), null); //$NON-NLS-1$
269 variableButton.addSelectionListener(new SelectionAdapter() {
270 public void widgetSelected(SelectionEvent e) {
271 VariableSelectionDialog dialog = new VariableSelectionDialog(
273 if (dialog.open() == SelectionDialog.OK) {
275 .insert(dialog.getForm().getSelectedVariable());
280 Label instruction = new Label(parent, SWT.NONE);
282 .setText(ExternalToolsLaunchConfigurationMessages
283 .getString("ExternalToolsOptionTab.Note__Enclose_an_argument_containing_spaces_using_double-quotes_(__)._Not_applicable_for_variables._3")); //$NON-NLS-1$
284 data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
285 data.horizontalSpan = 2;
286 instruction.setLayoutData(data);
287 instruction.setFont(font);
291 * Creates the controls needed to edit the run in background attribute of an
295 * the composite to create the controls in
297 protected void createRunBackgroundComponent(Composite parent) {
298 runBackgroundButton = new Button(parent, SWT.CHECK);
299 runBackgroundButton.setText(ExternalToolsLaunchConfigurationMessages
300 .getString("ExternalToolsOptionTab.Run_tool_in_bac&kground_4")); //$NON-NLS-1$
301 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
302 runBackgroundButton.setLayoutData(data);
303 runBackgroundButton.setFont(parent.getFont());
304 runBackgroundButton.addSelectionListener(getSelectionAdapter());
308 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#setDefaults(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
310 public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
311 configuration.setAttribute(
312 IExternalToolConstants.ATTR_RUN_IN_BACKGROUND, false);
316 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#initializeFrom(org.eclipse.debug.core.ILaunchConfiguration)
318 public void initializeFrom(ILaunchConfiguration configuration) {
319 updateLocation(configuration);
320 updateWorkingDirectory(configuration);
321 updateArgument(configuration);
322 updateRunBackground(configuration);
325 protected void updateWorkingDirectory(ILaunchConfiguration configuration) {
326 String workingDir = ""; //$NON-NLS-1$
328 workingDir = configuration.getAttribute(
329 IExternalToolConstants.ATTR_WORKING_DIRECTORY, ""); //$NON-NLS-1$
330 } catch (CoreException ce) {
334 ExternalToolsLaunchConfigurationMessages
335 .getString("ExternalToolsMainTab.Error_reading_configuration_10"), ce); //$NON-NLS-1$
337 workDirectoryField.setText(workingDir);
338 workDirectoryField.addModifyListener(modifyListener);
342 protected void updateLocation(ILaunchConfiguration configuration) {
343 String location = ""; //$NON-NLS-1$
345 location = configuration.getAttribute(
346 IExternalToolConstants.ATTR_LOCATION, ""); //$NON-NLS-1$
347 } catch (CoreException ce) {
351 ExternalToolsLaunchConfigurationMessages
352 .getString("ExternalToolsMainTab.Error_reading_configuration_10"), ce); //$NON-NLS-1$
354 locationField.setText(location);
355 locationField.addModifyListener(modifyListener);
358 protected void updateArgument(ILaunchConfiguration configuration) {
359 String arguments = ""; //$NON-NLS-1$
361 arguments = configuration.getAttribute(
362 IExternalToolConstants.ATTR_TOOL_ARGUMENTS, ""); //$NON-NLS-1$
363 } catch (CoreException ce) {
367 ExternalToolsLaunchConfigurationMessages
368 .getString("ExternalToolsOptionTab.Error_reading_configuration_7"), ce); //$NON-NLS-1$
370 argumentField.setText(arguments);
373 protected void updateRunBackground(ILaunchConfiguration configuration) {
374 boolean runInBackgroud = true;
376 runInBackgroud = configuration.getAttribute(
377 IExternalToolConstants.ATTR_RUN_IN_BACKGROUND, false);
378 } catch (CoreException ce) {
382 ExternalToolsLaunchConfigurationMessages
383 .getString("ExternalToolsOptionTab.Error_reading_configuration_7"), ce); //$NON-NLS-1$
385 runBackgroundButton.setSelection(runInBackgroud);
389 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#performApply(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
391 public void performApply(ILaunchConfigurationWorkingCopy configuration) {
392 String location = locationField.getText().trim();
393 if (location.length() == 0) {
394 configuration.setAttribute(IExternalToolConstants.ATTR_LOCATION,
397 configuration.setAttribute(IExternalToolConstants.ATTR_LOCATION,
401 String workingDirectory = workDirectoryField.getText().trim();
402 if (workingDirectory.length() == 0) {
403 configuration.setAttribute(
404 IExternalToolConstants.ATTR_WORKING_DIRECTORY,
407 configuration.setAttribute(
408 IExternalToolConstants.ATTR_WORKING_DIRECTORY,
412 setAttribute(IExternalToolConstants.ATTR_RUN_IN_BACKGROUND,
413 configuration, runBackgroundButton.getSelection(), false);
415 String arguments = argumentField.getText().trim();
416 if (arguments.length() == 0) {
417 configuration.setAttribute(
418 IExternalToolConstants.ATTR_TOOL_ARGUMENTS, (String) null);
420 configuration.setAttribute(
421 IExternalToolConstants.ATTR_TOOL_ARGUMENTS, arguments);
426 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getName()
428 public String getName() {
429 return ExternalToolsLaunchConfigurationMessages
430 .getString("ExternalToolsMainTab.&Main_17"); //$NON-NLS-1$
434 * @see ILaunchConfigurationTab#isValid(org.eclipse.debug.core.ILaunchConfiguration)
436 public boolean isValid(ILaunchConfiguration launchConfig) {
437 setErrorMessage(null);
439 return validateLocation() && validateWorkDirectory();
443 * Validates the content of the location field.
445 protected boolean validateLocation() {
446 String value = locationField.getText().trim();
447 if (value.length() < 1) {
448 setErrorMessage(ExternalToolsLaunchConfigurationMessages
449 .getString("ExternalToolsMainTab.External_tool_location_cannot_be_empty_18")); //$NON-NLS-1$
454 // Translate field contents to the actual file location so we
455 // can check to ensure the file actually exists.
456 MultiStatus multiStatus = new MultiStatus(
457 IExternalToolConstants.PLUGIN_ID, 0, "", null); //$NON-NLS-1$
458 value = ToolUtil.expandFileLocation(value,
459 ExpandVariableContext.EMPTY_CONTEXT, multiStatus);
460 if (!multiStatus.isOK()) {
461 IStatus[] children = multiStatus.getChildren();
462 if (children.length > 0) {
463 setErrorMessage(children[0].getMessage());
469 File file = new File(value);
470 if (!file.exists()) { // The file does not exist.
471 setErrorMessage(ExternalToolsLaunchConfigurationMessages
472 .getString("ExternalToolsMainTab.External_tool_location_does_not_exist_19")); //$NON-NLS-1$
475 if (!file.isFile()) {
476 setErrorMessage(ExternalToolsLaunchConfigurationMessages
477 .getString("ExternalToolsMainTab.External_tool_location_specified_is_not_a_file_20")); //$NON-NLS-1$
484 * Validates the content of the working directory field.
486 protected boolean validateWorkDirectory() {
488 String value = workDirectoryField.getText().trim();
489 if (value.length() > 0) {
490 // Translate field contents to the actual directory location so we
491 // can check to ensure the directory actually exists.
492 MultiStatus multiStatus = new MultiStatus(
493 IExternalToolConstants.PLUGIN_ID, 0, "", null); //$NON-NLS-1$
494 value = ToolUtil.expandDirectoryLocation(value,
495 ExpandVariableContext.EMPTY_CONTEXT, multiStatus);
496 if (!multiStatus.isOK()) {
497 IStatus[] children = multiStatus.getChildren();
498 if (children.length > 0) {
499 setErrorMessage(children[0].getMessage());
504 File file = new File(value);
505 if (!file.exists()) { // The directory does not exist.
506 setErrorMessage(ExternalToolsLaunchConfigurationMessages
507 .getString("ExternalToolsMainTab.External_tool_working_directory_does_not_exist_or_is_invalid_21")); //$NON-NLS-1$
514 protected void handleLocationButtonSelected() {
515 FileDialog fileDialog = new FileDialog(getShell(), SWT.NONE);
516 fileDialog.setFileName(locationField.getText());
517 String text = fileDialog.open();
519 locationField.setText(text);
524 * Prompts the user for a workspace location within the workspace and sets
525 * the location as a String containing the workspace_loc variable or
526 * <code>null</code> if no location was obtained from the user.
528 protected void handleWorkspaceLocationButtonSelected() {
529 ResourceSelectionDialog dialog;
530 dialog = new ResourceSelectionDialog(getShell(), ResourcesPlugin
531 .getWorkspace().getRoot(),
532 ExternalToolsLaunchConfigurationMessages
533 .getString("ExternalToolsMainTab.Select_a_resource_22")); //$NON-NLS-1$
535 Object[] results = dialog.getResult();
536 if (results == null || results.length < 1) {
539 IResource resource = (IResource) results[0];
540 StringBuffer buf = new StringBuffer();
541 ToolUtil.buildVariableTag(IExternalToolConstants.VAR_WORKSPACE_LOC,
542 resource.getFullPath().toString(), buf);
543 String text = buf.toString();
545 locationField.setText(text);
550 * Prompts the user for a working directory location within the workspace
551 * and sets the working directory as a String containing the workspace_loc
552 * variable or <code>null</code> if no location was obtained from the
555 protected void handleWorkspaceWorkingDirectoryButtonSelected() {
556 ContainerSelectionDialog containerDialog;
557 containerDialog = new ContainerSelectionDialog(
559 ResourcesPlugin.getWorkspace().getRoot(),
561 ExternalToolsLaunchConfigurationMessages
562 .getString("ExternalToolsMainTab.&Select_a_directory__23")); //$NON-NLS-1$
563 containerDialog.open();
564 Object[] resource = containerDialog.getResult();
566 if (resource != null && resource.length > 0) {
567 text = ToolUtil.buildVariableTag(
568 IExternalToolConstants.VAR_RESOURCE_LOC,
569 ((IPath) resource[0]).toString());
572 workDirectoryField.setText(text);
576 protected void handleFileWorkingDirectoryButtonSelected() {
577 DirectoryDialog dialog = new DirectoryDialog(getShell(), SWT.SAVE);
578 dialog.setMessage(ExternalToolsLaunchConfigurationMessages
579 .getString("ExternalToolsMainTab.&Select_a_directory__23")); //$NON-NLS-1$
580 dialog.setFilterPath(workDirectoryField.getText());
581 String text = dialog.open();
583 workDirectoryField.setText(text);
588 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getImage()
590 public Image getImage() {
591 return ExternalToolsImages
592 .getImage(IExternalToolConstants.IMG_TAB_MAIN);
596 * Method getSelectionAdapter.
598 * @return SelectionListener
600 protected SelectionListener getSelectionAdapter() {
601 if (selectionAdapter == null) {
602 selectionAdapter = new SelectionAdapter() {
603 public void widgetSelected(SelectionEvent e) {
604 updateLaunchConfigurationDialog();
608 return selectionAdapter;
611 private class VariableSelectionDialog extends SelectionDialog {
612 private ExternalToolVariableForm form;
614 private VariableSelectionDialog(Shell parent) {
616 setTitle(ExternalToolsLaunchConfigurationMessages
617 .getString("ExternalToolsOptionTab.Select_variable_10")); //$NON-NLS-1$
620 protected Control createDialogArea(Composite parent) {
621 // Create the dialog area
622 Composite composite = (Composite) super.createDialogArea(parent);
623 ExternalToolVariable[] variables = ExternalToolsPlugin.getDefault()
624 .getArgumentVariableRegistry().getArgumentVariables();
625 form = new ExternalToolVariableForm(
626 ExternalToolsLaunchConfigurationMessages
627 .getString("ExternalToolsOptionTab.&Choose_a_variable__11"), variables); //$NON-NLS-1$
628 form.createContents(composite, new IGroupDialogPage() {
629 public GridData setButtonGridData(Button button) {
630 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
631 data.heightHint = convertVerticalDLUsToPixels(IDialogConstants.BUTTON_HEIGHT);
632 int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
633 data.widthHint = Math.max(widthHint, button.computeSize(
634 SWT.DEFAULT, SWT.DEFAULT, true).x);
635 button.setLayoutData(data);
639 public void setMessage(String newMessage, int newType) {
640 VariableSelectionDialog.this.setMessage(newMessage);
643 public void updateValidState() {
646 public int convertHeightHint(int chars) {
647 return convertHeightInCharsToPixels(chars);
650 public String getMessage() {
651 if (!form.isValid()) {
652 return ExternalToolsLaunchConfigurationMessages
653 .getString("ExternalToolsOptionTab.Invalid_selection_12"); //$NON-NLS-1$
658 public int getMessageType() {
659 if (!form.isValid()) {
660 return IMessageProvider.ERROR;
668 private ExternalToolVariableForm getForm() {