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);
127 Composite buttonComposite = new Composite(parent, SWT.NONE);
128 layout = new GridLayout();
129 layout.marginWidth = 0;
130 layout.marginHeight = 0;
131 layout.numColumns = 1;
132 gridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
133 buttonComposite.setLayout(layout);
134 buttonComposite.setLayoutData(gridData);
135 buttonComposite.setFont(font);
137 createVerticalSpacer(buttonComposite, 1);
139 workspaceLocationButton = createPushButton(buttonComposite, ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.&Browse_Workspace..._3"), null); //$NON-NLS-1$
140 workspaceLocationButton.addSelectionListener(new SelectionAdapter() {
141 public void widgetSelected(SelectionEvent evt) {
142 handleWorkspaceLocationButtonSelected();
145 fileLocationButton = createPushButton(buttonComposite, ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.Brows&e_File_System..._4"), null); //$NON-NLS-1$
146 fileLocationButton.addSelectionListener(new SelectionAdapter() {
147 public void widgetSelected(SelectionEvent evt) {
148 handleLocationButtonSelected();
154 * Creates the controls needed to edit the working directory
155 * attribute of an external tool
157 * @param parent the composite to create the controls in
159 protected void createWorkDirectoryComponent(Composite parent) {
160 Font font = parent.getFont();
162 Composite composite = new Composite(parent, SWT.NONE);
163 GridLayout layout = new GridLayout();
164 layout.marginWidth = 0;
165 layout.marginHeight = 0;
166 layout.numColumns = 1;
167 GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
168 composite.setLayout(layout);
169 composite.setLayoutData(gridData);
171 Label label = new Label(composite, SWT.NONE);
172 label.setText(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.Working_&Directory__5")); //$NON-NLS-1$
175 workDirectoryField = new Text(composite, SWT.BORDER);
176 GridData data = new GridData(GridData.FILL_HORIZONTAL);
177 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
178 workDirectoryField.setLayoutData(data);
179 workDirectoryField.setFont(font);
181 Composite buttonComposite = new Composite(parent, SWT.NONE);
182 layout = new GridLayout();
183 layout.marginWidth = 0;
184 layout.marginHeight = 0;
185 layout.numColumns = 1;
186 gridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
187 buttonComposite.setLayout(layout);
188 buttonComposite.setLayoutData(gridData);
189 buttonComposite.setFont(font);
191 createVerticalSpacer(buttonComposite, 1);
192 workspaceWorkingDirectoryButton = createPushButton(buttonComposite, ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.Browse_Wor&kspace..._6"), null); //$NON-NLS-1$
193 workspaceWorkingDirectoryButton.addSelectionListener(new SelectionAdapter() {
194 public void widgetSelected(SelectionEvent evt) {
195 handleWorkspaceWorkingDirectoryButtonSelected();
198 fileWorkingDirectoryButton = createPushButton(buttonComposite, ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.Browse_F&ile_System..._7"), null); //$NON-NLS-1$
199 fileWorkingDirectoryButton.addSelectionListener(new SelectionAdapter() {
200 public void widgetSelected(SelectionEvent evt) {
201 handleFileWorkingDirectoryButtonSelected();
207 * Creates the controls needed to edit the argument and
208 * prompt for argument attributes of an external tool
210 * @param parent the composite to create the controls in
212 protected void createArgumentComponent(Composite parent) {
213 Font font = parent.getFont();
215 Label label = new Label(parent, SWT.NONE);
216 label.setText(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsOptionTab.&Arguments___1")); //$NON-NLS-1$
217 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
218 data.horizontalSpan = 2;
219 label.setLayoutData(data);
222 argumentField = new Text(parent, SWT.BORDER);
223 data = new GridData(GridData.FILL_HORIZONTAL);
224 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
225 argumentField.setLayoutData(data);
226 argumentField.setFont(font);
227 argumentField.addModifyListener(new ModifyListener() {
228 public void modifyText(ModifyEvent e) {
229 updateLaunchConfigurationDialog();
233 variableButton = createPushButton(parent, ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsOptionTab.Varia&bles..._2"), null); //$NON-NLS-1$
234 variableButton.addSelectionListener(new SelectionAdapter() {
235 public void widgetSelected(SelectionEvent e) {
236 VariableSelectionDialog dialog = new VariableSelectionDialog(getShell());
237 if (dialog.open() == SelectionDialog.OK) {
238 argumentField.insert(dialog.getForm().getSelectedVariable());
243 Label instruction = new Label(parent, SWT.NONE);
244 instruction.setText(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsOptionTab.Note__Enclose_an_argument_containing_spaces_using_double-quotes_(__)._Not_applicable_for_variables._3")); //$NON-NLS-1$
245 data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
246 data.horizontalSpan = 2;
247 instruction.setLayoutData(data);
248 instruction.setFont(font);
252 * Creates the controls needed to edit the run in background
253 * attribute of an external tool
255 * @param parent the composite to create the controls in
257 protected void createRunBackgroundComponent(Composite parent) {
258 runBackgroundButton = new Button(parent, SWT.CHECK);
259 runBackgroundButton.setText(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsOptionTab.Run_tool_in_bac&kground_4")); //$NON-NLS-1$
260 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
261 runBackgroundButton.setLayoutData(data);
262 runBackgroundButton.setFont(parent.getFont());
263 runBackgroundButton.addSelectionListener(getSelectionAdapter());
267 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#setDefaults(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
269 public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
270 configuration.setAttribute(IExternalToolConstants.ATTR_RUN_IN_BACKGROUND, false);
274 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#initializeFrom(org.eclipse.debug.core.ILaunchConfiguration)
276 public void initializeFrom(ILaunchConfiguration configuration) {
277 updateLocation(configuration);
278 updateWorkingDirectory(configuration);
279 updateArgument(configuration);
280 updateRunBackground(configuration);
283 protected void updateWorkingDirectory(ILaunchConfiguration configuration) {
284 String workingDir = ""; //$NON-NLS-1$
286 workingDir = configuration.getAttribute(IExternalToolConstants.ATTR_WORKING_DIRECTORY, ""); //$NON-NLS-1$
287 } catch (CoreException ce) {
288 ExternalToolsPlugin.getDefault().log(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.Error_reading_configuration_10"), ce); //$NON-NLS-1$
290 workDirectoryField.setText(workingDir);
291 workDirectoryField.addModifyListener(modifyListener);
295 protected void updateLocation(ILaunchConfiguration configuration) {
296 String location = ""; //$NON-NLS-1$
298 location = configuration.getAttribute(IExternalToolConstants.ATTR_LOCATION, ""); //$NON-NLS-1$
299 } catch (CoreException ce) {
300 ExternalToolsPlugin.getDefault().log(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.Error_reading_configuration_10"), ce); //$NON-NLS-1$
302 locationField.setText(location);
303 locationField.addModifyListener(modifyListener);
306 protected void updateArgument(ILaunchConfiguration configuration) {
307 String arguments = ""; //$NON-NLS-1$
309 arguments = configuration.getAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS, ""); //$NON-NLS-1$
310 } catch (CoreException ce) {
311 ExternalToolsPlugin.getDefault().log(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsOptionTab.Error_reading_configuration_7"), ce); //$NON-NLS-1$
313 argumentField.setText(arguments);
316 protected void updateRunBackground(ILaunchConfiguration configuration) {
317 boolean runInBackgroud = true;
319 runInBackgroud = configuration.getAttribute(IExternalToolConstants.ATTR_RUN_IN_BACKGROUND, false);
320 } catch (CoreException ce) {
321 ExternalToolsPlugin.getDefault().log(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsOptionTab.Error_reading_configuration_7"), ce); //$NON-NLS-1$
323 runBackgroundButton.setSelection(runInBackgroud);
327 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#performApply(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
329 public void performApply(ILaunchConfigurationWorkingCopy configuration) {
330 String location = locationField.getText().trim();
331 if (location.length() == 0) {
332 configuration.setAttribute(IExternalToolConstants.ATTR_LOCATION, (String) null);
334 configuration.setAttribute(IExternalToolConstants.ATTR_LOCATION, location);
337 String workingDirectory = workDirectoryField.getText().trim();
338 if (workingDirectory.length() == 0) {
339 configuration.setAttribute(IExternalToolConstants.ATTR_WORKING_DIRECTORY, (String) null);
341 configuration.setAttribute(IExternalToolConstants.ATTR_WORKING_DIRECTORY, workingDirectory);
344 setAttribute(IExternalToolConstants.ATTR_RUN_IN_BACKGROUND, configuration, runBackgroundButton.getSelection(), false);
346 String arguments = argumentField.getText().trim();
347 if (arguments.length() == 0) {
348 configuration.setAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS, (String) null);
350 configuration.setAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS, arguments);
355 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getName()
357 public String getName() {
358 return ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.&Main_17"); //$NON-NLS-1$
362 * @see ILaunchConfigurationTab#isValid(org.eclipse.debug.core.ILaunchConfiguration)
364 public boolean isValid(ILaunchConfiguration launchConfig) {
365 setErrorMessage(null);
367 return validateLocation() && validateWorkDirectory();
371 * Validates the content of the location field.
373 protected boolean validateLocation() {
374 String value = locationField.getText().trim();
375 if (value.length() < 1) {
376 setErrorMessage(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.External_tool_location_cannot_be_empty_18")); //$NON-NLS-1$
381 // Translate field contents to the actual file location so we
382 // can check to ensure the file actually exists.
383 MultiStatus multiStatus = new MultiStatus(IExternalToolConstants.PLUGIN_ID, 0, "", null); //$NON-NLS-1$
384 value = ToolUtil.expandFileLocation(value, ExpandVariableContext.EMPTY_CONTEXT, multiStatus);
385 if (!multiStatus.isOK()) {
386 IStatus[] children = multiStatus.getChildren();
387 if (children.length > 0) {
388 setErrorMessage(children[0].getMessage());
394 File file = new File(value);
395 if (!file.exists()) { // The file does not exist.
396 setErrorMessage(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.External_tool_location_does_not_exist_19")); //$NON-NLS-1$
399 if (!file.isFile()) {
400 setErrorMessage(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.External_tool_location_specified_is_not_a_file_20")); //$NON-NLS-1$
407 * Validates the content of the working directory field.
409 protected boolean validateWorkDirectory() {
411 String value = workDirectoryField.getText().trim();
412 if (value.length() > 0) {
413 // Translate field contents to the actual directory location so we
414 // can check to ensure the directory actually exists.
415 MultiStatus multiStatus = new MultiStatus(IExternalToolConstants.PLUGIN_ID, 0, "", null); //$NON-NLS-1$
416 value = ToolUtil.expandDirectoryLocation(value, ExpandVariableContext.EMPTY_CONTEXT, multiStatus);
417 if (!multiStatus.isOK()) {
418 IStatus[] children = multiStatus.getChildren();
419 if (children.length > 0) {
420 setErrorMessage(children[0].getMessage());
425 File file = new File(value);
426 if (!file.exists()) { // The directory does not exist.
427 setErrorMessage(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.External_tool_working_directory_does_not_exist_or_is_invalid_21")); //$NON-NLS-1$
434 protected void handleLocationButtonSelected() {
435 FileDialog fileDialog = new FileDialog(getShell(), SWT.NONE);
436 fileDialog.setFileName(locationField.getText());
437 String text = fileDialog.open();
439 locationField.setText(text);
444 * Prompts the user for a workspace location within the workspace and sets
445 * the location as a String containing the workspace_loc variable or
446 * <code>null</code> if no location was obtained from the user.
448 protected void handleWorkspaceLocationButtonSelected() {
449 ResourceSelectionDialog dialog;
450 dialog = new ResourceSelectionDialog(getShell(), ResourcesPlugin.getWorkspace().getRoot(), ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.Select_a_resource_22")); //$NON-NLS-1$
452 Object[] results = dialog.getResult();
453 if (results == null || results.length < 1) {
456 IResource resource = (IResource) results[0];
457 StringBuffer buf = new StringBuffer();
458 ToolUtil.buildVariableTag(IExternalToolConstants.VAR_WORKSPACE_LOC, resource.getFullPath().toString(), buf);
459 String text = buf.toString();
461 locationField.setText(text);
466 * Prompts the user for a working directory location within the workspace
467 * and sets the working directory as a String containing the workspace_loc
468 * variable or <code>null</code> if no location was obtained from the user.
470 protected void handleWorkspaceWorkingDirectoryButtonSelected() {
471 ContainerSelectionDialog containerDialog;
472 containerDialog = new ContainerSelectionDialog(getShell(), ResourcesPlugin.getWorkspace().getRoot(), false, ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.&Select_a_directory__23")); //$NON-NLS-1$
473 containerDialog.open();
474 Object[] resource = containerDialog.getResult();
476 if (resource != null && resource.length > 0) {
477 text = ToolUtil.buildVariableTag(IExternalToolConstants.VAR_RESOURCE_LOC, ((IPath) resource[0]).toString());
480 workDirectoryField.setText(text);
484 protected void handleFileWorkingDirectoryButtonSelected() {
485 DirectoryDialog dialog = new DirectoryDialog(getShell(), SWT.SAVE);
486 dialog.setMessage(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsMainTab.&Select_a_directory__23")); //$NON-NLS-1$
487 dialog.setFilterPath(workDirectoryField.getText());
488 String text = dialog.open();
490 workDirectoryField.setText(text);
495 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getImage()
497 public Image getImage() {
498 return ExternalToolsImages.getImage(IExternalToolConstants.IMG_TAB_MAIN);
502 * Method getSelectionAdapter.
503 * @return SelectionListener
505 protected SelectionListener getSelectionAdapter() {
506 if (selectionAdapter == null) {
507 selectionAdapter = new SelectionAdapter() {
508 public void widgetSelected(SelectionEvent e) {
509 updateLaunchConfigurationDialog();
513 return selectionAdapter;
516 private class VariableSelectionDialog extends SelectionDialog {
517 private ExternalToolVariableForm form;
518 private VariableSelectionDialog(Shell parent) {
520 setTitle(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsOptionTab.Select_variable_10")); //$NON-NLS-1$
522 protected Control createDialogArea(Composite parent) {
523 // Create the dialog area
524 Composite composite = (Composite) super.createDialogArea(parent);
525 ExternalToolVariable[] variables = ExternalToolsPlugin.getDefault().getArgumentVariableRegistry().getArgumentVariables();
526 form = new ExternalToolVariableForm(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsOptionTab.&Choose_a_variable__11"), variables); //$NON-NLS-1$
527 form.createContents(composite, new IGroupDialogPage() {
528 public GridData setButtonGridData(Button button) {
529 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
530 data.heightHint = convertVerticalDLUsToPixels(IDialogConstants.BUTTON_HEIGHT);
531 int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
532 data.widthHint = Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
533 button.setLayoutData(data);
537 public void setMessage(String newMessage, int newType) {
538 VariableSelectionDialog.this.setMessage(newMessage);
541 public void updateValidState() {
544 public int convertHeightHint(int chars) {
545 return convertHeightInCharsToPixels(chars);
548 public String getMessage() {
549 if (!form.isValid()) {
550 return ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsOptionTab.Invalid_selection_12"); //$NON-NLS-1$
555 public int getMessageType() {
556 if (!form.isValid()) {
557 return IMessageProvider.ERROR;
565 private ExternalToolVariableForm getForm() {