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.model.ExternalToolsPlugin;
14 import net.sourceforge.phpdt.externaltools.internal.registry.ExternalToolVariable;
15 import net.sourceforge.phpdt.externaltools.model.IExternalToolConstants;
16 import net.sourceforge.phpdt.externaltools.model.ToolUtil;
17 import net.sourceforge.phpdt.externaltools.variable.ExpandVariableContext;
18 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.resources.ResourcesPlugin;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IPath;
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.core.runtime.MultiStatus;
26 import org.eclipse.debug.core.ILaunchConfiguration;
27 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
28 import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
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 tool
106 * the composite to create the controls in
108 protected void createLocationComponent(Composite parent) {
109 Font font = parent.getFont();
111 Composite composite = new Composite(parent, SWT.NONE);
112 GridLayout layout = new GridLayout();
113 layout.marginWidth = 0;
114 layout.marginHeight = 0;
115 layout.numColumns = 1;
116 GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
117 composite.setLayout(layout);
118 composite.setLayoutData(gridData);
120 Label label = new Label(composite, SWT.NONE);
121 label.setText(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.&Location___2")); //$NON-NLS-1$
124 final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
125 locationField = new Combo(composite, SWT.DROP_DOWN | SWT.BORDER);
126 GridData data = new GridData(GridData.FILL_HORIZONTAL);
127 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
128 locationField.setLayoutData(data);
129 locationField.setFont(font);
130 locationField.add(store.getString(PHPeclipsePlugin.PHP_RUN_PREF), 0);
131 locationField.add(store.getString(PHPeclipsePlugin.APACHE_RUN_PREF), 1);
132 locationField.add(store.getString(PHPeclipsePlugin.MYSQL_RUN_PREF), 2);
133 locationField.add(store.getString(PHPeclipsePlugin.XAMPP_START_PREF), 3);
134 locationField.add(store.getString(PHPeclipsePlugin.XAMPP_STOP_PREF), 4);
135 Composite buttonComposite = new Composite(parent, SWT.NONE);
136 layout = new GridLayout();
137 layout.marginWidth = 0;
138 layout.marginHeight = 0;
139 layout.numColumns = 1;
140 gridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
141 buttonComposite.setLayout(layout);
142 buttonComposite.setLayoutData(gridData);
143 buttonComposite.setFont(font);
145 createVerticalSpacer(buttonComposite, 1);
147 workspaceLocationButton = createPushButton(buttonComposite, ExternalToolsLaunchConfigurationMessages
148 .getString("ExternalToolsMainTab.&Browse_Workspace..._3"), null); //$NON-NLS-1$
149 workspaceLocationButton.addSelectionListener(new SelectionAdapter() {
150 public void widgetSelected(SelectionEvent evt) {
151 handleWorkspaceLocationButtonSelected();
154 fileLocationButton = createPushButton(buttonComposite, ExternalToolsLaunchConfigurationMessages
155 .getString("ExternalToolsMainTab.Brows&e_File_System..._4"), null); //$NON-NLS-1$
156 fileLocationButton.addSelectionListener(new SelectionAdapter() {
157 public void widgetSelected(SelectionEvent evt) {
158 handleLocationButtonSelected();
164 * Creates the controls needed to edit the working directory attribute of an external tool
167 * the composite to create the controls in
169 protected void createWorkDirectoryComponent(Composite parent) {
170 Font font = parent.getFont();
172 Composite composite = new Composite(parent, SWT.NONE);
173 GridLayout layout = new GridLayout();
174 layout.marginWidth = 0;
175 layout.marginHeight = 0;
176 layout.numColumns = 1;
177 GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
178 composite.setLayout(layout);
179 composite.setLayoutData(gridData);
181 Label label = new Label(composite, SWT.NONE);
182 label.setText(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.Working_&Directory__5")); //$NON-NLS-1$
185 workDirectoryField = new Text(composite, SWT.BORDER);
186 GridData data = new GridData(GridData.FILL_HORIZONTAL);
187 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
188 workDirectoryField.setLayoutData(data);
189 workDirectoryField.setFont(font);
191 Composite buttonComposite = new Composite(parent, SWT.NONE);
192 layout = new GridLayout();
193 layout.marginWidth = 0;
194 layout.marginHeight = 0;
195 layout.numColumns = 1;
196 gridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
197 buttonComposite.setLayout(layout);
198 buttonComposite.setLayoutData(gridData);
199 buttonComposite.setFont(font);
201 createVerticalSpacer(buttonComposite, 1);
202 workspaceWorkingDirectoryButton = createPushButton(buttonComposite, ExternalToolsLaunchConfigurationMessages
203 .getString("ExternalToolsMainTab.Browse_Wor&kspace..._6"), null); //$NON-NLS-1$
204 workspaceWorkingDirectoryButton.addSelectionListener(new SelectionAdapter() {
205 public void widgetSelected(SelectionEvent evt) {
206 handleWorkspaceWorkingDirectoryButtonSelected();
209 fileWorkingDirectoryButton = createPushButton(buttonComposite, ExternalToolsLaunchConfigurationMessages
210 .getString("ExternalToolsMainTab.Browse_F&ile_System..._7"), null); //$NON-NLS-1$
211 fileWorkingDirectoryButton.addSelectionListener(new SelectionAdapter() {
212 public void widgetSelected(SelectionEvent evt) {
213 handleFileWorkingDirectoryButtonSelected();
219 * Creates the controls needed to edit the argument and prompt for argument attributes of an external tool
222 * the composite to create the controls in
224 protected void createArgumentComponent(Composite parent) {
225 Font font = parent.getFont();
227 Label label = new Label(parent, SWT.NONE);
228 label.setText(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsOptionTab.&Arguments___1")); //$NON-NLS-1$
229 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
230 data.horizontalSpan = 2;
231 label.setLayoutData(data);
234 argumentField = new Text(parent, SWT.BORDER);
235 data = new GridData(GridData.FILL_HORIZONTAL);
236 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
237 argumentField.setLayoutData(data);
238 argumentField.setFont(font);
239 argumentField.addModifyListener(new ModifyListener() {
240 public void modifyText(ModifyEvent e) {
241 updateLaunchConfigurationDialog();
245 variableButton = createPushButton(parent, ExternalToolsLaunchConfigurationMessages
246 .getString("ExternalToolsOptionTab.Varia&bles..._2"), null); //$NON-NLS-1$
247 variableButton.addSelectionListener(new SelectionAdapter() {
248 public void widgetSelected(SelectionEvent e) {
249 VariableSelectionDialog dialog = new VariableSelectionDialog(getShell());
250 if (dialog.open() == SelectionDialog.OK) {
251 argumentField.insert(dialog.getForm().getSelectedVariable());
256 Label instruction = new Label(parent, SWT.NONE);
258 .setText(ExternalToolsLaunchConfigurationMessages
259 .getString("ExternalToolsOptionTab.Note__Enclose_an_argument_containing_spaces_using_double-quotes_(__)._Not_applicable_for_variables._3")); //$NON-NLS-1$
260 data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
261 data.horizontalSpan = 2;
262 instruction.setLayoutData(data);
263 instruction.setFont(font);
267 * Creates the controls needed to edit the run in background attribute of an external tool
270 * the composite to create the controls in
272 protected void createRunBackgroundComponent(Composite parent) {
273 runBackgroundButton = new Button(parent, SWT.CHECK);
274 runBackgroundButton.setText(ExternalToolsLaunchConfigurationMessages
275 .getString("ExternalToolsOptionTab.Run_tool_in_bac&kground_4")); //$NON-NLS-1$
276 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
277 runBackgroundButton.setLayoutData(data);
278 runBackgroundButton.setFont(parent.getFont());
279 runBackgroundButton.addSelectionListener(getSelectionAdapter());
283 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#setDefaults(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
285 public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
286 configuration.setAttribute(IExternalToolConstants.ATTR_RUN_IN_BACKGROUND, false);
290 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#initializeFrom(org.eclipse.debug.core.ILaunchConfiguration)
292 public void initializeFrom(ILaunchConfiguration configuration) {
293 updateLocation(configuration);
294 updateWorkingDirectory(configuration);
295 updateArgument(configuration);
296 updateRunBackground(configuration);
299 protected void updateWorkingDirectory(ILaunchConfiguration configuration) {
300 String workingDir = ""; //$NON-NLS-1$
302 workingDir = configuration.getAttribute(IExternalToolConstants.ATTR_WORKING_DIRECTORY, ""); //$NON-NLS-1$
303 } catch (CoreException ce) {
304 ExternalToolsPlugin.getDefault().log(
305 ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.Error_reading_configuration_10"), ce); //$NON-NLS-1$
307 workDirectoryField.setText(workingDir);
308 workDirectoryField.addModifyListener(modifyListener);
312 protected void updateLocation(ILaunchConfiguration configuration) {
313 String location = ""; //$NON-NLS-1$
315 location = configuration.getAttribute(IExternalToolConstants.ATTR_LOCATION, ""); //$NON-NLS-1$
316 } catch (CoreException ce) {
317 ExternalToolsPlugin.getDefault().log(
318 ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.Error_reading_configuration_10"), ce); //$NON-NLS-1$
320 locationField.setText(location);
321 locationField.addModifyListener(modifyListener);
324 protected void updateArgument(ILaunchConfiguration configuration) {
325 String arguments = ""; //$NON-NLS-1$
327 arguments = configuration.getAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS, ""); //$NON-NLS-1$
328 } catch (CoreException ce) {
329 ExternalToolsPlugin.getDefault().log(
330 ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsOptionTab.Error_reading_configuration_7"), ce); //$NON-NLS-1$
332 argumentField.setText(arguments);
335 protected void updateRunBackground(ILaunchConfiguration configuration) {
336 boolean runInBackgroud = true;
338 runInBackgroud = configuration.getAttribute(IExternalToolConstants.ATTR_RUN_IN_BACKGROUND, false);
339 } catch (CoreException ce) {
340 ExternalToolsPlugin.getDefault().log(
341 ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsOptionTab.Error_reading_configuration_7"), ce); //$NON-NLS-1$
343 runBackgroundButton.setSelection(runInBackgroud);
347 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#performApply(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
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);
354 configuration.setAttribute(IExternalToolConstants.ATTR_LOCATION, location);
357 String workingDirectory = workDirectoryField.getText().trim();
358 if (workingDirectory.length() == 0) {
359 configuration.setAttribute(IExternalToolConstants.ATTR_WORKING_DIRECTORY, (String) null);
361 configuration.setAttribute(IExternalToolConstants.ATTR_WORKING_DIRECTORY, workingDirectory);
364 setAttribute(IExternalToolConstants.ATTR_RUN_IN_BACKGROUND, configuration, runBackgroundButton.getSelection(), false);
366 String arguments = argumentField.getText().trim();
367 if (arguments.length() == 0) {
368 configuration.setAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS, (String) null);
370 configuration.setAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS, arguments);
375 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getName()
377 public String getName() {
378 return ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.&Main_17"); //$NON-NLS-1$
382 * @see ILaunchConfigurationTab#isValid(org.eclipse.debug.core.ILaunchConfiguration)
384 public boolean isValid(ILaunchConfiguration launchConfig) {
385 setErrorMessage(null);
387 return validateLocation() && validateWorkDirectory();
391 * Validates the content of the location field.
393 protected boolean validateLocation() {
394 String value = locationField.getText().trim();
395 if (value.length() < 1) {
396 setErrorMessage(ExternalToolsLaunchConfigurationMessages
397 .getString("ExternalToolsMainTab.External_tool_location_cannot_be_empty_18")); //$NON-NLS-1$
402 // Translate field contents to the actual file location so we
403 // can check to ensure the file actually exists.
404 MultiStatus multiStatus = new MultiStatus(IExternalToolConstants.PLUGIN_ID, 0, "", null); //$NON-NLS-1$
405 value = ToolUtil.expandFileLocation(value, ExpandVariableContext.EMPTY_CONTEXT, multiStatus);
406 if (!multiStatus.isOK()) {
407 IStatus[] children = multiStatus.getChildren();
408 if (children.length > 0) {
409 setErrorMessage(children[0].getMessage());
415 File file = new File(value);
416 if (!file.exists()) { // The file does not exist.
417 setErrorMessage(ExternalToolsLaunchConfigurationMessages
418 .getString("ExternalToolsMainTab.External_tool_location_does_not_exist_19")); //$NON-NLS-1$
421 if (!file.isFile()) {
422 setErrorMessage(ExternalToolsLaunchConfigurationMessages
423 .getString("ExternalToolsMainTab.External_tool_location_specified_is_not_a_file_20")); //$NON-NLS-1$
430 * Validates the content of the working directory field.
432 protected boolean validateWorkDirectory() {
434 String value = workDirectoryField.getText().trim();
435 if (value.length() > 0) {
436 // Translate field contents to the actual directory location so we
437 // can check to ensure the directory actually exists.
438 MultiStatus multiStatus = new MultiStatus(IExternalToolConstants.PLUGIN_ID, 0, "", null); //$NON-NLS-1$
439 value = ToolUtil.expandDirectoryLocation(value, ExpandVariableContext.EMPTY_CONTEXT, multiStatus);
440 if (!multiStatus.isOK()) {
441 IStatus[] children = multiStatus.getChildren();
442 if (children.length > 0) {
443 setErrorMessage(children[0].getMessage());
448 File file = new File(value);
449 if (!file.exists()) { // The directory does not exist.
450 setErrorMessage(ExternalToolsLaunchConfigurationMessages
451 .getString("ExternalToolsMainTab.External_tool_working_directory_does_not_exist_or_is_invalid_21")); //$NON-NLS-1$
458 protected void handleLocationButtonSelected() {
459 FileDialog fileDialog = new FileDialog(getShell(), SWT.NONE);
460 fileDialog.setFileName(locationField.getText());
461 String text = fileDialog.open();
463 locationField.setText(text);
468 * Prompts the user for a workspace location within the workspace and sets the location as a String containing the workspace_loc
469 * variable or <code>null</code> if no location was obtained from the user.
471 protected void handleWorkspaceLocationButtonSelected() {
472 ResourceSelectionDialog dialog;
473 dialog = new ResourceSelectionDialog(getShell(), ResourcesPlugin.getWorkspace().getRoot(),
474 ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.Select_a_resource_22")); //$NON-NLS-1$
476 Object[] results = dialog.getResult();
477 if (results == null || results.length < 1) {
480 IResource resource = (IResource) results[0];
481 StringBuffer buf = new StringBuffer();
482 ToolUtil.buildVariableTag(IExternalToolConstants.VAR_WORKSPACE_LOC, resource.getFullPath().toString(), buf);
483 String text = buf.toString();
485 locationField.setText(text);
490 * Prompts the user for a working directory location within the workspace and sets the working directory as a String containing
491 * the workspace_loc variable or <code>null</code> if no location was obtained from the user.
493 protected void handleWorkspaceWorkingDirectoryButtonSelected() {
494 ContainerSelectionDialog containerDialog;
495 containerDialog = new ContainerSelectionDialog(getShell(), ResourcesPlugin.getWorkspace().getRoot(), false,
496 ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.&Select_a_directory__23")); //$NON-NLS-1$
497 containerDialog.open();
498 Object[] resource = containerDialog.getResult();
500 if (resource != null && resource.length > 0) {
501 text = ToolUtil.buildVariableTag(IExternalToolConstants.VAR_RESOURCE_LOC, ((IPath) resource[0]).toString());
504 workDirectoryField.setText(text);
508 protected void handleFileWorkingDirectoryButtonSelected() {
509 DirectoryDialog dialog = new DirectoryDialog(getShell(), SWT.SAVE);
510 dialog.setMessage(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.&Select_a_directory__23")); //$NON-NLS-1$
511 dialog.setFilterPath(workDirectoryField.getText());
512 String text = dialog.open();
514 workDirectoryField.setText(text);
519 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getImage()
521 public Image getImage() {
522 return ExternalToolsImages.getImage(IExternalToolConstants.IMG_TAB_MAIN);
526 * Method getSelectionAdapter.
528 * @return SelectionListener
530 protected SelectionListener getSelectionAdapter() {
531 if (selectionAdapter == null) {
532 selectionAdapter = new SelectionAdapter() {
533 public void widgetSelected(SelectionEvent e) {
534 updateLaunchConfigurationDialog();
538 return selectionAdapter;
541 private class VariableSelectionDialog extends SelectionDialog {
542 private ExternalToolVariableForm form;
544 private VariableSelectionDialog(Shell parent) {
546 setTitle(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsOptionTab.Select_variable_10")); //$NON-NLS-1$
549 protected Control createDialogArea(Composite parent) {
550 // Create the dialog area
551 Composite composite = (Composite) super.createDialogArea(parent);
552 ExternalToolVariable[] variables = ExternalToolsPlugin.getDefault().getArgumentVariableRegistry().getArgumentVariables();
553 form = new ExternalToolVariableForm(ExternalToolsLaunchConfigurationMessages
554 .getString("ExternalToolsOptionTab.&Choose_a_variable__11"), variables); //$NON-NLS-1$
555 form.createContents(composite, new IGroupDialogPage() {
556 public GridData setButtonGridData(Button button) {
557 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
558 data.heightHint = convertVerticalDLUsToPixels(IDialogConstants.BUTTON_HEIGHT);
559 int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
560 data.widthHint = Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
561 button.setLayoutData(data);
565 public void setMessage(String newMessage, int newType) {
566 VariableSelectionDialog.this.setMessage(newMessage);
569 public void updateValidState() {
572 public int convertHeightHint(int chars) {
573 return convertHeightInCharsToPixels(chars);
576 public String getMessage() {
577 if (!form.isValid()) {
578 return ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsOptionTab.Invalid_selection_12"); //$NON-NLS-1$
583 public int getMessageType() {
584 if (!form.isValid()) {
585 return IMessageProvider.ERROR;
593 private ExternalToolVariableForm getForm() {