ed5f1b073ac00146c9f921e066d07f06a881e6c7
[phpeclipse.git] / net.sourceforge.phpeclipse.xdebug.ui / src / net / sourceforge / phpeclipse / xdebug / ui / php / launching / PHPMainTab.java
1 package net.sourceforge.phpeclipse.xdebug.ui.php.launching;
2
3 import java.io.File;
4
5 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
6 import net.sourceforge.phpdt.internal.ui.util.PHPFileSelector;
7 import net.sourceforge.phpdt.internal.ui.util.PHPProjectSelector;
8 import net.sourceforge.phpeclipse.xdebug.core.XDebugCorePlugin;
9 import net.sourceforge.phpeclipse.xdebug.php.launching.IXDebugConstants;
10
11 import org.eclipse.core.resources.IFile;
12 import org.eclipse.core.resources.IProject;
13 import org.eclipse.core.resources.IResource;
14 import org.eclipse.core.resources.ResourcesPlugin;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.debug.core.ILaunchConfiguration;
17 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
18 import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
19 import org.eclipse.jface.viewers.ISelection;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.ModifyEvent;
23 import org.eclipse.swt.events.ModifyListener;
24 import org.eclipse.swt.events.SelectionAdapter;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.graphics.Font;
27 import org.eclipse.swt.graphics.Image;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Button;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.FileDialog;
33 import org.eclipse.swt.widgets.Group;
34 import org.eclipse.swt.widgets.Text;
35 import org.eclipse.ui.IEditorInput;
36 import org.eclipse.ui.IEditorPart;
37 import org.eclipse.ui.IWorkbenchPage;
38
39 public class PHPMainTab extends AbstractLaunchConfigurationTab {
40
41         // Project UI widgets
42         protected Text fProjText;
43
44         protected Button fProjButton;
45
46         // Main class UI widgets
47         protected Text fMainText;
48
49         protected Button fSearchButton;
50
51         protected PHPProjectSelector projectSelector;
52
53         protected PHPFileSelector fileSelector;
54
55         private Button fUseDefaultInterpreterButton;
56
57         private Button fInterpreterButton;
58
59         private Text fInterpreterText;
60
61         public PHPMainTab() {
62                 super();
63         }
64
65         public void createControl(Composite parent) {
66                 Font font = parent.getFont();
67
68                 Composite comp = new Composite(parent, SWT.NONE);
69                 setControl(comp);
70                 // PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(),
71                 // IJavaDebugHelpContextIds.LAUNCH_CONFIGURATION_DIALOG_MAIN_TAB);
72                 GridLayout topLayout = new GridLayout();
73                 topLayout.verticalSpacing = 0;
74                 comp.setLayout(topLayout);
75                 comp.setFont(font);
76
77                 createProjectEditor(comp);
78                 createVerticalSpacer(comp, 1);
79                 createMainTypeEditor(comp);
80                 createVerticalSpacer(comp, 1);
81                 createInterpreterEditor(comp);
82         }
83
84         /**
85          * Creates the widgets for specifying a main type.
86          * 
87          * @param parent
88          *            the parent composite
89          */
90         private void createProjectEditor(Composite parent) {
91                 Font font = parent.getFont();
92                 Group group = new Group(parent, SWT.NONE);
93                 group.setText("Project:");
94                 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
95                 group.setLayoutData(gd);
96                 GridLayout layout = new GridLayout();
97                 layout.numColumns = 2;
98                 group.setLayout(layout);
99                 group.setFont(font);
100
101                 projectSelector = new PHPProjectSelector(group);
102                 projectSelector
103                                 .setBrowseDialogMessage("Choose the project containing the application entry point:");
104                 projectSelector.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
105                 projectSelector.addModifyListener(new ModifyListener() {
106                         public void modifyText(ModifyEvent evt) {
107                                 updateLaunchConfigurationDialog();
108                         }
109                 });
110         }
111
112         /**
113          * Creates the widgets for specifying a php file.
114          * 
115          * @param parent
116          *            the parent composite
117          */
118         private void createMainTypeEditor(Composite parent) {
119                 Font font = parent.getFont();
120                 Group mainGroup = new Group(parent, SWT.NONE);
121                 mainGroup.setText("File: ");
122                 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
123                 mainGroup.setLayoutData(gd);
124                 GridLayout layout = new GridLayout();
125                 layout.numColumns = 2;
126                 mainGroup.setLayout(layout);
127                 mainGroup.setFont(font);
128
129                 fileSelector = new PHPFileSelector(mainGroup, projectSelector);
130                 fileSelector
131                                 .setBrowseDialogMessage("Choose the PHP file that represents the application entry point:");
132                 fileSelector.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
133                 fileSelector.addModifyListener(new ModifyListener() {
134                         public void modifyText(ModifyEvent evt) {
135                                 updateLaunchConfigurationDialog();
136                         }
137                 });
138         }
139
140         /**
141          * Creates the widgets for specifying debug settings.
142          * 
143          * @param parent
144          *            the parent composite
145          */
146         private void createInterpreterEditor(Composite parent) {
147                 Font font = parent.getFont();
148                 Group interpreterGroup = new Group(parent, SWT.NONE);
149                 interpreterGroup.setText("Interpreter: ");
150                 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
151                 interpreterGroup.setLayoutData(gd);
152                 GridLayout layout = new GridLayout();
153                 layout.numColumns = 2;
154                 interpreterGroup.setLayout(layout);
155                 interpreterGroup.setFont(font);
156
157                 fInterpreterText = new Text(interpreterGroup, SWT.SINGLE | SWT.BORDER);
158                 gd = new GridData(GridData.FILL_HORIZONTAL);
159                 fInterpreterText.setLayoutData(gd);
160                 fInterpreterText.setFont(font);
161                 fInterpreterText.addModifyListener(new ModifyListener() {
162                         public void modifyText(ModifyEvent evt) {
163                                 updateLaunchConfigurationDialog();
164                         }
165                 });
166
167                 fInterpreterButton = createPushButton(interpreterGroup, "Browse..",
168                                 null);
169                 fInterpreterButton.addSelectionListener(new SelectionAdapter() {
170                         public void widgetSelected(SelectionEvent event) {
171                                 handleBrowseSellected(event);
172                         }
173                 });
174
175                 fUseDefaultInterpreterButton = new Button(interpreterGroup, SWT.CHECK);
176                 fUseDefaultInterpreterButton.setText("Use default interpreter");
177                 gd = new GridData(GridData.FILL_HORIZONTAL);
178                 fUseDefaultInterpreterButton.setLayoutData(gd);
179                 fUseDefaultInterpreterButton.setFont(font);
180                 fUseDefaultInterpreterButton
181                                 .addSelectionListener(new SelectionAdapter() {
182                                         public void widgetSelected(SelectionEvent event) {
183                                                 handleDefaultSellected(event);
184                                         }
185                                 });
186
187         }
188
189         /**
190          * Set the appropriate enabled state for the appletviewqer text widget.
191          */
192         protected void setInterpreterTextEnabledState() {
193                 if (isDefaultInterpreter()) {
194                         fInterpreterText.setEnabled(false);
195                         fInterpreterButton.setEnabled(false);
196                 } else {
197                         fInterpreterText.setEnabled(true);
198                         fInterpreterButton.setEnabled(true);
199                 }
200         }
201
202         /**
203          * Returns whether the default appletviewer is to be used
204          */
205         protected boolean isDefaultInterpreter() {
206                 return fUseDefaultInterpreterButton.getSelection();
207         }
208
209         protected void handleDefaultSellected(SelectionEvent event) {
210                 setInterpreterTextEnabledState();
211                 updateLaunchConfigurationDialog();
212                 // if (isDefaultInterpreter()) {
213                 // fInterpreterText.setText("default Interpreter");
214                 // }
215
216         }
217
218         protected void handleBrowseSellected(SelectionEvent event) {
219                 FileDialog dlg = new FileDialog(getShell(), SWT.OPEN);
220                 String fileName = dlg.open();
221                 if (fileName != null) {
222                         fInterpreterText.setText(fileName);
223                         updateLaunchConfigurationDialog();
224                 }
225         }
226
227         protected IProject getContext() {
228                 IWorkbenchPage page = XDebugCorePlugin.getDefault().getWorkbench()
229                                 .getActiveWorkbenchWindow().getActivePage();
230                 if (page != null) {
231                         ISelection selection = page.getSelection();
232                         if (selection instanceof IStructuredSelection) {
233                                 IStructuredSelection ss = (IStructuredSelection) selection;
234                                 if (!ss.isEmpty()) {
235                                         Object obj = ss.getFirstElement();
236                                         if (obj instanceof IResource)
237                                                 return ((IResource) obj).getProject();
238                                 }
239                         }
240                         IEditorPart part = page.getActiveEditor();
241                         if (part != null) {
242                                 IEditorInput input = part.getEditorInput();
243                                 IResource file = (IResource) input.getAdapter(IResource.class);
244                                 if (file != null) {
245                                         return file.getProject();
246                                 }
247                         }
248                 }
249                 return null;
250         }
251
252         public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
253                 IProject project = getContext();
254                 if (project != null)
255                         configuration.setAttribute(IXDebugConstants.ATTR_PHP_PROJECT,
256                                         project.getName());
257         }
258
259         public void initializeFrom(ILaunchConfiguration configuration) {
260                 try {
261                         String project = configuration.getAttribute(
262                                         IXDebugConstants.ATTR_PHP_PROJECT, (String) null);
263                         if (project != null) {
264                                 projectSelector.setSelectionText(project);
265                         }
266                         String file = configuration.getAttribute(
267                                         IXDebugConstants.ATTR_PHP_FILE, (String) null);
268                         if (file != null) {
269                                 fileSelector.setSelectionText(file);
270                         }
271
272                         String interpreterFile = configuration.getAttribute(
273                                         IXDebugConstants.ATTR_PHP_INTERPRETER, (String) null);
274                         if (interpreterFile != null)
275                                 fInterpreterText.setText(interpreterFile);
276                         boolean selection = configuration.getAttribute(
277                                         IXDebugConstants.ATTR_PHP_DEFAULT_INTERPRETER, true);
278                         fUseDefaultInterpreterButton.setSelection(selection);
279                         setInterpreterTextEnabledState();
280
281                 } catch (CoreException e) {
282                         setErrorMessage(e.getMessage());
283                 }
284         }
285
286         public void performApply(ILaunchConfigurationWorkingCopy configuration) {
287                 String project = projectSelector.getSelectionText().trim();
288                 if (project.length() == 0) {
289                         project = null;
290                 }
291                 configuration.setAttribute(IXDebugConstants.ATTR_PHP_PROJECT, project);
292
293                 IFile file = fileSelector.getSelection();
294                 configuration.setAttribute(IXDebugConstants.ATTR_PHP_FILE,
295                                 file == null ? "" : file.getProjectRelativePath().toString());
296                 configuration.setAttribute(
297                                 IXDebugConstants.ATTR_PHP_DEFAULT_INTERPRETER,
298                                 this.fUseDefaultInterpreterButton.getSelection());
299                 configuration.setAttribute(IXDebugConstants.ATTR_PHP_INTERPRETER,
300                                 this.fInterpreterText.getText());
301
302         }
303
304         /*
305          * (non-Javadoc)
306          * 
307          * @see org.eclipse.debug.ui.ILaunchConfigurationTab#isValid(org.eclipse.debug.core.ILaunchConfiguration)
308          */
309         public boolean isValid(ILaunchConfiguration launchConfig) {
310                 setErrorMessage(null);
311                 String projectName = projectSelector.getSelectionText().trim();
312                 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(
313                                 projectName);
314                 if (!project.exists()) {
315                         setErrorMessage("Project does not exist");
316                         return false;
317                 }
318                 String fileName = fileSelector.getSelectionText().trim();
319                 if (fileName.equals("")) {
320                         setErrorMessage("No file selected.");
321                         return false;
322                 }
323                 IFile file = project.getFile(fileName);
324                 if (!file.exists()) {
325                         setErrorMessage("File does not exist");
326                         return false;
327                 }
328                 if (!fUseDefaultInterpreterButton.getSelection()) {
329                         File exe = new File(fInterpreterText.getText());
330                         System.out.println(exe.toString());
331                         if (!exe.exists()) {
332                                 setErrorMessage("Invalid Interpreter");
333                                 return false;
334                         }
335                 }
336                 return true;
337         }
338
339         public Image getImage() {
340                 return PHPUiImages.get(PHPUiImages.IMG_CTOOLS_PHP_PAGE);
341         }
342
343         public String getName() {
344                 return "Main";
345         }
346
347 }