c0aca3a7adbf8004a09ffcef46ad845de37778f5
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.ui / src / net / sourceforge / phpdt / internal / debug / ui / launcher / PHPArgumentsTab.java
1 package net.sourceforge.phpdt.internal.debug.ui.launcher;
2
3 import net.sourceforge.phpdt.debug.ui.PHPDebugUiConstants;
4 import net.sourceforge.phpdt.debug.ui.PHPDebugUiImages;
5 import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiMessages;
6 import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiPlugin;
7 import net.sourceforge.phpdt.internal.launching.PHPLaunchConfigurationAttribute;
8 import net.sourceforge.phpdt.internal.ui.util.DirectorySelector;
9
10 import org.eclipse.core.runtime.CoreException;
11 import org.eclipse.debug.core.ILaunchConfiguration;
12 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
13 import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.ModifyEvent;
16 import org.eclipse.swt.events.ModifyListener;
17 import org.eclipse.swt.events.SelectionAdapter;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.graphics.Image;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Button;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Label;
25 import org.eclipse.swt.widgets.Text;
26
27 public class PHPArgumentsTab extends AbstractLaunchConfigurationTab {
28         protected Text interpreterArgsText, programArgsText;
29         protected DirectorySelector workingDirectorySelector;
30         protected Button useDefaultWorkingDirectoryButton;
31
32         public PHPArgumentsTab() {
33                 super();
34         }
35
36         public void createControl(Composite parent) {
37                 Composite composite = createPageRoot(parent);
38
39                 new Label(composite, SWT.NONE).setText(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPArguments.working_dir"));
40                 workingDirectorySelector = new DirectorySelector(composite);
41                 workingDirectorySelector.setBrowseDialogMessage(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPArguments.working_dir_browser_message"));
42                 workingDirectorySelector.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
43                 workingDirectorySelector.addModifyListener(new ModifyListener() {
44                         public void modifyText(ModifyEvent e) {
45                                 updateLaunchConfigurationDialog();
46                         }
47                 });
48
49                 Composite defaultWorkingDirectoryComposite = new Composite(composite, SWT.NONE);
50                 GridLayout layout = new GridLayout();
51                 layout.numColumns = 2;
52                 defaultWorkingDirectoryComposite.setLayout(layout);
53                 useDefaultWorkingDirectoryButton = new Button(defaultWorkingDirectoryComposite, SWT.CHECK);
54                 useDefaultWorkingDirectoryButton.addSelectionListener(new SelectionAdapter() {
55                         public void widgetSelected(SelectionEvent e) {
56                                 setUseDefaultWorkingDirectory(((Button) e.getSource()).getSelection());
57                         }
58                 });
59                 new Label(defaultWorkingDirectoryComposite, SWT.NONE).setText(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPArguments.working_dir_use_default_message"));
60                 defaultWorkingDirectoryComposite.pack();
61
62                 Label verticalSpacer = new Label(composite, SWT.NONE);
63
64                 new Label(composite, SWT.NONE).setText(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPArguments.interpreter_args_box_title"));
65                 interpreterArgsText = new Text(composite, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
66                 interpreterArgsText.setLayoutData(new GridData(GridData.FILL_BOTH));
67
68                 new Label(composite, SWT.NONE).setText(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPArguments.program_args_box_title"));
69                 programArgsText = new Text(composite, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
70                 programArgsText.setLayoutData(new GridData(GridData.FILL_BOTH));
71         }
72
73         protected void setUseDefaultWorkingDirectory(boolean useDefault) {
74                 if (!useDefaultWorkingDirectoryButton.getSelection() == useDefault)
75                         useDefaultWorkingDirectoryButton.setSelection(useDefault);
76                 if (useDefault)
77                         workingDirectorySelector.setSelectionText(PHPDebugUiConstants.DEFAULT_WORKING_DIRECTORY);
78                 workingDirectorySelector.setEnabled(!useDefault);
79         }
80
81         public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
82                 setUseDefaultWorkingDirectory(true);
83                 configuration.setAttribute(PHPLaunchConfigurationAttribute.WORKING_DIRECTORY, PHPDebugUiConstants.DEFAULT_WORKING_DIRECTORY);
84                 // set hidden attribute
85                 //configuration.setAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_ID, "net.sourceforge.phpdt.internal.debug.ui.PHPSourceLocator") ;
86         }
87
88         public void initializeFrom(ILaunchConfiguration configuration) {
89                 String workingDirectory = "", interpreterArgs = "", programArgs = "";
90                 boolean useDefaultWorkDir = true;
91                 try {
92                         workingDirectory = configuration.getAttribute(PHPLaunchConfigurationAttribute.WORKING_DIRECTORY, "");
93                         interpreterArgs = configuration.getAttribute(PHPLaunchConfigurationAttribute.INTERPRETER_ARGUMENTS, "");
94                         programArgs = configuration.getAttribute(PHPLaunchConfigurationAttribute.PROGRAM_ARGUMENTS, "");
95                         useDefaultWorkDir = configuration.getAttribute(PHPLaunchConfigurationAttribute.USE_DEFAULT_WORKING_DIRECTORY, true);
96                 } catch (CoreException e) {
97                         log(e);
98                 }
99
100                 workingDirectorySelector.setSelectionText(workingDirectory);
101                 interpreterArgsText.setText(interpreterArgs);
102                 programArgsText.setText(programArgs);
103                 setUseDefaultWorkingDirectory(useDefaultWorkDir);
104         }
105
106         public void performApply(ILaunchConfigurationWorkingCopy configuration) {
107                 configuration.setAttribute(PHPLaunchConfigurationAttribute.WORKING_DIRECTORY, workingDirectorySelector.getValidatedSelectionText());
108                 configuration.setAttribute(PHPLaunchConfigurationAttribute.INTERPRETER_ARGUMENTS, interpreterArgsText.getText());
109                 configuration.setAttribute(PHPLaunchConfigurationAttribute.PROGRAM_ARGUMENTS, programArgsText.getText());
110                 configuration.setAttribute(PHPLaunchConfigurationAttribute.USE_DEFAULT_WORKING_DIRECTORY, useDefaultWorkingDirectoryButton.getSelection());
111         }
112
113         protected Composite createPageRoot(Composite parent) {
114                 Composite composite = new Composite(parent, SWT.NONE);
115                 GridLayout compositeLayout = new GridLayout();
116                 compositeLayout.marginWidth = 0;
117                 compositeLayout.numColumns = 1;
118                 composite.setLayout(compositeLayout);
119
120                 setControl(composite);
121                 return composite;
122         }
123
124         public String getName() {
125                 return PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPArguments.name");
126         }
127
128         public boolean isValid(ILaunchConfiguration launchConfig) {
129                 try {
130                         String workingDirectory = launchConfig.getAttribute(PHPLaunchConfigurationAttribute.WORKING_DIRECTORY, "");
131                         if (workingDirectory.length() == 0) {
132                                 setErrorMessage(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPArguments.working_dir_error_message"));
133                                 return false;
134                         }
135                 } catch (CoreException e) {
136                         log(e);
137                 }
138
139                 setErrorMessage(null);
140                 return true;
141         }
142         
143         protected void log(Throwable t) {
144                 PHPDebugUiPlugin.getDefault().log(t);
145         }
146
147         public Image getImage() {
148                 return PHPDebugUiImages.get(PHPDebugUiImages.IMG_EVIEW_ARGUMENTS_TAB);
149         }
150
151 }