1 package net.sourceforge.phpdt.externaltools.launchConfigurations;
3 /**********************************************************************
4 Copyright (c) 2000, 2002 IBM Corp. All rights reserved.
5 This file is made available under the terms of the Common Public License v1.0
6 which accompanies this distribution, and is available at
7 http://www.eclipse.org/legal/cpl-v10.html
8 **********************************************************************/
12 import net.sourceforge.phpdt.externaltools.group.IGroupDialogPage;
13 import net.sourceforge.phpdt.externaltools.internal.dialog.ExternalToolVariableForm;
14 import net.sourceforge.phpdt.externaltools.internal.model.ExternalToolsImages;
15 import net.sourceforge.phpdt.externaltools.internal.model.ExternalToolsPlugin;
16 import net.sourceforge.phpdt.externaltools.internal.registry.ExternalToolVariable;
17 import net.sourceforge.phpdt.externaltools.model.IExternalToolConstants;
18 import net.sourceforge.phpdt.externaltools.model.ToolUtil;
19 import net.sourceforge.phpdt.externaltools.variable.ExpandVariableContext;
20 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.resources.ResourcesPlugin;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.core.runtime.MultiStatus;
28 import org.eclipse.debug.core.ILaunchConfiguration;
29 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
30 import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
31 import org.eclipse.jface.dialogs.IDialogConstants;
32 import org.eclipse.jface.dialogs.IMessageProvider;
33 import org.eclipse.jface.preference.IPreferenceStore;
34 import org.eclipse.swt.SWT;
35 import org.eclipse.swt.events.ModifyEvent;
36 import org.eclipse.swt.events.ModifyListener;
37 import org.eclipse.swt.events.SelectionAdapter;
38 import org.eclipse.swt.events.SelectionEvent;
39 import org.eclipse.swt.events.SelectionListener;
40 import org.eclipse.swt.graphics.Font;
41 import org.eclipse.swt.graphics.Image;
42 import org.eclipse.swt.layout.GridData;
43 import org.eclipse.swt.layout.GridLayout;
44 import org.eclipse.swt.widgets.Button;
45 import org.eclipse.swt.widgets.Combo;
46 import org.eclipse.swt.widgets.Composite;
47 import org.eclipse.swt.widgets.Control;
48 import org.eclipse.swt.widgets.DirectoryDialog;
49 import org.eclipse.swt.widgets.FileDialog;
50 import org.eclipse.swt.widgets.Label;
51 import org.eclipse.swt.widgets.Shell;
52 import org.eclipse.swt.widgets.Text;
53 import org.eclipse.ui.dialogs.ContainerSelectionDialog;
54 import org.eclipse.ui.dialogs.ResourceSelectionDialog;
55 import org.eclipse.ui.dialogs.SelectionDialog;
57 public class ExternalToolsMainTab extends AbstractLaunchConfigurationTab {
59 protected Combo locationField;
60 protected Text workDirectoryField;
61 protected Button fileLocationButton;
62 protected Button workspaceLocationButton;
63 protected Button fileWorkingDirectoryButton;
64 protected Button workspaceWorkingDirectoryButton;
66 protected Button runBackgroundButton;
67 protected Text argumentField;
68 protected Button variableButton;
70 protected SelectionAdapter selectionAdapter;
72 protected ModifyListener modifyListener = new ModifyListener() {
73 public void modifyText(ModifyEvent e) {
74 updateLaunchConfigurationDialog();
79 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#createControl(org.eclipse.swt.widgets.Composite)
81 public void createControl(Composite parent) {
82 Composite mainComposite = new Composite(parent, SWT.NONE);
83 setControl(mainComposite);
84 GridLayout layout = new GridLayout();
85 layout.numColumns = 2;
86 GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
87 mainComposite.setLayout(layout);
88 mainComposite.setLayoutData(gridData);
89 mainComposite.setFont(parent.getFont());
90 createLocationComponent(mainComposite);
91 createWorkDirectoryComponent(mainComposite);
92 createArgumentComponent(mainComposite);
93 createVerticalSpacer(mainComposite, 2);
94 createRunBackgroundComponent(mainComposite);
98 * Creates the controls needed to edit the location
99 * attribute of an external tool
101 * @param parent the composite to create the controls in
103 protected void createLocationComponent(Composite parent) {
104 Font font = parent.getFont();
106 Composite composite = new Composite(parent, SWT.NONE);
107 GridLayout layout = new GridLayout();
108 layout.marginWidth = 0;
109 layout.marginHeight = 0;
110 layout.numColumns = 1;
111 GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
112 composite.setLayout(layout);
113 composite.setLayoutData(gridData);
115 Label label = new Label(composite, SWT.NONE);
116 label.setText(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.&Location___2")); //$NON-NLS-1$
119 final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
120 locationField = new Combo(composite, SWT.DROP_DOWN | SWT.BORDER);
121 GridData data = new GridData(GridData.FILL_HORIZONTAL);
122 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
123 locationField.setLayoutData(data);
124 locationField.setFont(font);
125 locationField.add( store.getString(PHPeclipsePlugin.PHP_RUN_PREF), 0);
126 locationField.add( store.getString(PHPeclipsePlugin.APACHE_RUN_PREF), 1);
127 locationField.add( store.getString(PHPeclipsePlugin.MYSQL_RUN_PREF), 2);
129 Composite buttonComposite = new Composite(parent, SWT.NONE);
130 layout = new GridLayout();
131 layout.marginWidth = 0;
132 layout.marginHeight = 0;
133 layout.numColumns = 1;
134 gridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
135 buttonComposite.setLayout(layout);
136 buttonComposite.setLayoutData(gridData);
137 buttonComposite.setFont(font);
139 createVerticalSpacer(buttonComposite, 1);
141 workspaceLocationButton = createPushButton(buttonComposite, ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.&Browse_Workspace..._3"), null); //$NON-NLS-1$
142 workspaceLocationButton.addSelectionListener(new SelectionAdapter() {
143 public void widgetSelected(SelectionEvent evt) {
144 handleWorkspaceLocationButtonSelected();
147 fileLocationButton = createPushButton(buttonComposite, ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.Brows&e_File_System..._4"), null); //$NON-NLS-1$
148 fileLocationButton.addSelectionListener(new SelectionAdapter() {
149 public void widgetSelected(SelectionEvent evt) {
150 handleLocationButtonSelected();
156 * Creates the controls needed to edit the working directory
157 * attribute of an external tool
159 * @param parent the composite to create the controls in
161 protected void createWorkDirectoryComponent(Composite parent) {
162 Font font = parent.getFont();
164 Composite composite = new Composite(parent, SWT.NONE);
165 GridLayout layout = new GridLayout();
166 layout.marginWidth = 0;
167 layout.marginHeight = 0;
168 layout.numColumns = 1;
169 GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
170 composite.setLayout(layout);
171 composite.setLayoutData(gridData);
173 Label label = new Label(composite, SWT.NONE);
174 label.setText(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.Working_&Directory__5")); //$NON-NLS-1$
177 workDirectoryField = new Text(composite, SWT.BORDER);
178 GridData data = new GridData(GridData.FILL_HORIZONTAL);
179 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
180 workDirectoryField.setLayoutData(data);
181 workDirectoryField.setFont(font);
183 Composite buttonComposite = new Composite(parent, SWT.NONE);
184 layout = new GridLayout();
185 layout.marginWidth = 0;
186 layout.marginHeight = 0;
187 layout.numColumns = 1;
188 gridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
189 buttonComposite.setLayout(layout);
190 buttonComposite.setLayoutData(gridData);
191 buttonComposite.setFont(font);
193 createVerticalSpacer(buttonComposite, 1);
194 workspaceWorkingDirectoryButton = createPushButton(buttonComposite, ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.Browse_Wor&kspace..._6"), null); //$NON-NLS-1$
195 workspaceWorkingDirectoryButton.addSelectionListener(new SelectionAdapter() {
196 public void widgetSelected(SelectionEvent evt) {
197 handleWorkspaceWorkingDirectoryButtonSelected();
200 fileWorkingDirectoryButton = createPushButton(buttonComposite, ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.Browse_F&ile_System..._7"), null); //$NON-NLS-1$
201 fileWorkingDirectoryButton.addSelectionListener(new SelectionAdapter() {
202 public void widgetSelected(SelectionEvent evt) {
203 handleFileWorkingDirectoryButtonSelected();
209 * Creates the controls needed to edit the argument and
210 * prompt for argument attributes of an external tool
212 * @param parent the composite to create the controls in
214 protected void createArgumentComponent(Composite parent) {
215 Font font = parent.getFont();
217 Label label = new Label(parent, SWT.NONE);
218 label.setText(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsOptionTab.&Arguments___1")); //$NON-NLS-1$
219 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
220 data.horizontalSpan = 2;
221 label.setLayoutData(data);
224 argumentField = new Text(parent, SWT.BORDER);
225 data = new GridData(GridData.FILL_HORIZONTAL);
226 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
227 argumentField.setLayoutData(data);
228 argumentField.setFont(font);
229 argumentField.addModifyListener(new ModifyListener() {
230 public void modifyText(ModifyEvent e) {
231 updateLaunchConfigurationDialog();
235 variableButton = createPushButton(parent, ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsOptionTab.Varia&bles..._2"), null); //$NON-NLS-1$
236 variableButton.addSelectionListener(new SelectionAdapter() {
237 public void widgetSelected(SelectionEvent e) {
238 VariableSelectionDialog dialog = new VariableSelectionDialog(getShell());
239 if (dialog.open() == SelectionDialog.OK) {
240 argumentField.insert(dialog.getForm().getSelectedVariable());
245 Label instruction = new Label(parent, SWT.NONE);
246 instruction.setText(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsOptionTab.Note__Enclose_an_argument_containing_spaces_using_double-quotes_(__)._Not_applicable_for_variables._3")); //$NON-NLS-1$
247 data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
248 data.horizontalSpan = 2;
249 instruction.setLayoutData(data);
250 instruction.setFont(font);
254 * Creates the controls needed to edit the run in background
255 * attribute of an external tool
257 * @param parent the composite to create the controls in
259 protected void createRunBackgroundComponent(Composite parent) {
260 runBackgroundButton = new Button(parent, SWT.CHECK);
261 runBackgroundButton.setText(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsOptionTab.Run_tool_in_bac&kground_4")); //$NON-NLS-1$
262 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
263 runBackgroundButton.setLayoutData(data);
264 runBackgroundButton.setFont(parent.getFont());
265 runBackgroundButton.addSelectionListener(getSelectionAdapter());
269 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#setDefaults(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
271 public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
272 configuration.setAttribute(IExternalToolConstants.ATTR_RUN_IN_BACKGROUND, false);
276 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#initializeFrom(org.eclipse.debug.core.ILaunchConfiguration)
278 public void initializeFrom(ILaunchConfiguration configuration) {
279 updateLocation(configuration);
280 updateWorkingDirectory(configuration);
281 updateArgument(configuration);
282 updateRunBackground(configuration);
285 protected void updateWorkingDirectory(ILaunchConfiguration configuration) {
286 String workingDir = ""; //$NON-NLS-1$
288 workingDir = configuration.getAttribute(IExternalToolConstants.ATTR_WORKING_DIRECTORY, ""); //$NON-NLS-1$
289 } catch (CoreException ce) {
290 ExternalToolsPlugin.getDefault().log(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.Error_reading_configuration_10"), ce); //$NON-NLS-1$
292 workDirectoryField.setText(workingDir);
293 workDirectoryField.addModifyListener(modifyListener);
297 protected void updateLocation(ILaunchConfiguration configuration) {
298 String location = ""; //$NON-NLS-1$
300 location = configuration.getAttribute(IExternalToolConstants.ATTR_LOCATION, ""); //$NON-NLS-1$
301 } catch (CoreException ce) {
302 ExternalToolsPlugin.getDefault().log(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.Error_reading_configuration_10"), ce); //$NON-NLS-1$
304 locationField.setText(location);
305 locationField.addModifyListener(modifyListener);
308 protected void updateArgument(ILaunchConfiguration configuration) {
309 String arguments = ""; //$NON-NLS-1$
311 arguments = configuration.getAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS, ""); //$NON-NLS-1$
312 } catch (CoreException ce) {
313 ExternalToolsPlugin.getDefault().log(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsOptionTab.Error_reading_configuration_7"), ce); //$NON-NLS-1$
315 argumentField.setText(arguments);
318 protected void updateRunBackground(ILaunchConfiguration configuration) {
319 boolean runInBackgroud = true;
321 runInBackgroud = configuration.getAttribute(IExternalToolConstants.ATTR_RUN_IN_BACKGROUND, false);
322 } catch (CoreException ce) {
323 ExternalToolsPlugin.getDefault().log(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsOptionTab.Error_reading_configuration_7"), ce); //$NON-NLS-1$
325 runBackgroundButton.setSelection(runInBackgroud);
329 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#performApply(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
331 public void performApply(ILaunchConfigurationWorkingCopy configuration) {
332 String location = locationField.getText().trim();
333 if (location.length() == 0) {
334 configuration.setAttribute(IExternalToolConstants.ATTR_LOCATION, (String) null);
336 configuration.setAttribute(IExternalToolConstants.ATTR_LOCATION, location);
339 String workingDirectory = workDirectoryField.getText().trim();
340 if (workingDirectory.length() == 0) {
341 configuration.setAttribute(IExternalToolConstants.ATTR_WORKING_DIRECTORY, (String) null);
343 configuration.setAttribute(IExternalToolConstants.ATTR_WORKING_DIRECTORY, workingDirectory);
346 setAttribute(IExternalToolConstants.ATTR_RUN_IN_BACKGROUND, configuration, runBackgroundButton.getSelection(), false);
348 String arguments = argumentField.getText().trim();
349 if (arguments.length() == 0) {
350 configuration.setAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS, (String) null);
352 configuration.setAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS, arguments);
357 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getName()
359 public String getName() {
360 return ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.&Main_17"); //$NON-NLS-1$
364 * @see ILaunchConfigurationTab#isValid(org.eclipse.debug.core.ILaunchConfiguration)
366 public boolean isValid(ILaunchConfiguration launchConfig) {
367 setErrorMessage(null);
369 return validateLocation() && validateWorkDirectory();
373 * Validates the content of the location field.
375 protected boolean validateLocation() {
376 String value = locationField.getText().trim();
377 if (value.length() < 1) {
378 setErrorMessage(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.External_tool_location_cannot_be_empty_18")); //$NON-NLS-1$
383 // Translate field contents to the actual file location so we
384 // can check to ensure the file actually exists.
385 MultiStatus multiStatus = new MultiStatus(IExternalToolConstants.PLUGIN_ID, 0, "", null); //$NON-NLS-1$
386 value = ToolUtil.expandFileLocation(value, ExpandVariableContext.EMPTY_CONTEXT, multiStatus);
387 if (!multiStatus.isOK()) {
388 IStatus[] children = multiStatus.getChildren();
389 if (children.length > 0) {
390 setErrorMessage(children[0].getMessage());
396 File file = new File(value);
397 if (!file.exists()) { // The file does not exist.
398 setErrorMessage(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.External_tool_location_does_not_exist_19")); //$NON-NLS-1$
401 if (!file.isFile()) {
402 setErrorMessage(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.External_tool_location_specified_is_not_a_file_20")); //$NON-NLS-1$
409 * Validates the content of the working directory field.
411 protected boolean validateWorkDirectory() {
413 String value = workDirectoryField.getText().trim();
414 if (value.length() > 0) {
415 // Translate field contents to the actual directory location so we
416 // can check to ensure the directory actually exists.
417 MultiStatus multiStatus = new MultiStatus(IExternalToolConstants.PLUGIN_ID, 0, "", null); //$NON-NLS-1$
418 value = ToolUtil.expandDirectoryLocation(value, ExpandVariableContext.EMPTY_CONTEXT, multiStatus);
419 if (!multiStatus.isOK()) {
420 IStatus[] children = multiStatus.getChildren();
421 if (children.length > 0) {
422 setErrorMessage(children[0].getMessage());
427 File file = new File(value);
428 if (!file.exists()) { // The directory does not exist.
429 setErrorMessage(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.External_tool_working_directory_does_not_exist_or_is_invalid_21")); //$NON-NLS-1$
436 protected void handleLocationButtonSelected() {
437 FileDialog fileDialog = new FileDialog(getShell(), SWT.NONE);
438 fileDialog.setFileName(locationField.getText());
439 String text = fileDialog.open();
441 locationField.setText(text);
446 * Prompts the user for a workspace location within the workspace and sets
447 * the location as a String containing the workspace_loc variable or
448 * <code>null</code> if no location was obtained from the user.
450 protected void handleWorkspaceLocationButtonSelected() {
451 ResourceSelectionDialog dialog;
452 dialog = new ResourceSelectionDialog(getShell(), ResourcesPlugin.getWorkspace().getRoot(), ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.Select_a_resource_22")); //$NON-NLS-1$
454 Object[] results = dialog.getResult();
455 if (results == null || results.length < 1) {
458 IResource resource = (IResource) results[0];
459 StringBuffer buf = new StringBuffer();
460 ToolUtil.buildVariableTag(IExternalToolConstants.VAR_WORKSPACE_LOC, resource.getFullPath().toString(), buf);
461 String text = buf.toString();
463 locationField.setText(text);
468 * Prompts the user for a working directory location within the workspace
469 * and sets the working directory as a String containing the workspace_loc
470 * variable or <code>null</code> if no location was obtained from the user.
472 protected void handleWorkspaceWorkingDirectoryButtonSelected() {
473 ContainerSelectionDialog containerDialog;
474 containerDialog = new ContainerSelectionDialog(getShell(), ResourcesPlugin.getWorkspace().getRoot(), false, ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.&Select_a_directory__23")); //$NON-NLS-1$
475 containerDialog.open();
476 Object[] resource = containerDialog.getResult();
478 if (resource != null && resource.length > 0) {
479 text = ToolUtil.buildVariableTag(IExternalToolConstants.VAR_RESOURCE_LOC, ((IPath) resource[0]).toString());
482 workDirectoryField.setText(text);
486 protected void handleFileWorkingDirectoryButtonSelected() {
487 DirectoryDialog dialog = new DirectoryDialog(getShell(), SWT.SAVE);
488 dialog.setMessage(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.&Select_a_directory__23")); //$NON-NLS-1$
489 dialog.setFilterPath(workDirectoryField.getText());
490 String text = dialog.open();
492 workDirectoryField.setText(text);
497 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getImage()
499 public Image getImage() {
500 return ExternalToolsImages.getImage(IExternalToolConstants.IMG_TAB_MAIN);
504 * Method getSelectionAdapter.
505 * @return SelectionListener
507 protected SelectionListener getSelectionAdapter() {
508 if (selectionAdapter == null) {
509 selectionAdapter = new SelectionAdapter() {
510 public void widgetSelected(SelectionEvent e) {
511 updateLaunchConfigurationDialog();
515 return selectionAdapter;
518 private class VariableSelectionDialog extends SelectionDialog {
519 private ExternalToolVariableForm form;
520 private VariableSelectionDialog(Shell parent) {
522 setTitle(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsOptionTab.Select_variable_10")); //$NON-NLS-1$
524 protected Control createDialogArea(Composite parent) {
525 // Create the dialog area
526 Composite composite = (Composite) super.createDialogArea(parent);
527 ExternalToolVariable[] variables = ExternalToolsPlugin.getDefault().getArgumentVariableRegistry().getArgumentVariables();
528 form = new ExternalToolVariableForm(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsOptionTab.&Choose_a_variable__11"), variables); //$NON-NLS-1$
529 form.createContents(composite, new IGroupDialogPage() {
530 public GridData setButtonGridData(Button button) {
531 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
532 data.heightHint = convertVerticalDLUsToPixels(IDialogConstants.BUTTON_HEIGHT);
533 int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
534 data.widthHint = Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
535 button.setLayoutData(data);
539 public void setMessage(String newMessage, int newType) {
540 VariableSelectionDialog.this.setMessage(newMessage);
543 public void updateValidState() {
546 public int convertHeightHint(int chars) {
547 return convertHeightInCharsToPixels(chars);
550 public String getMessage() {
551 if (!form.isValid()) {
552 return ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsOptionTab.Invalid_selection_12"); //$NON-NLS-1$
557 public int getMessageType() {
558 if (!form.isValid()) {
559 return IMessageProvider.ERROR;
567 private ExternalToolVariableForm getForm() {