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