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