03e5e3ea0be1a1415547d33f8dc2630f5053504a
[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 net.sourceforge.phpdt.internal.ui.PHPUiImages;
4 import net.sourceforge.phpdt.internal.ui.util.PHPFileSelector;
5 import net.sourceforge.phpdt.internal.ui.util.PHPProjectSelector;
6 import net.sourceforge.phpeclipse.xdebug.core.XDebugCorePlugin;
7 import net.sourceforge.phpeclipse.xdebug.php.launching.IXDebugConstants;
8
9 import org.eclipse.core.resources.IFile;
10 import org.eclipse.core.resources.IProject;
11 import org.eclipse.core.resources.IResource;
12 import org.eclipse.core.runtime.CoreException;
13 import org.eclipse.debug.core.ILaunchConfiguration;
14 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
15 import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
16 import org.eclipse.jface.viewers.ISelection;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.ModifyEvent;
20 import org.eclipse.swt.events.ModifyListener;
21 import org.eclipse.swt.events.SelectionAdapter;
22 import org.eclipse.swt.events.SelectionEvent;
23 import org.eclipse.swt.graphics.Font;
24 import org.eclipse.swt.graphics.Image;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.layout.GridLayout;
27 import org.eclipse.swt.widgets.Button;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.FileDialog;
30 import org.eclipse.swt.widgets.Group;
31 import org.eclipse.swt.widgets.Label;
32 import org.eclipse.swt.widgets.Text;
33 import org.eclipse.ui.IEditorInput;
34 import org.eclipse.ui.IEditorPart;
35 import org.eclipse.ui.IWorkbenchPage;
36
37 public class PHPMainTab extends AbstractLaunchConfigurationTab {
38
39         // Project UI widgets
40         protected Text fProjText;
41         protected Button fProjButton;
42
43         // Main class UI widgets
44         protected Text fMainText;
45         protected Button fSearchButton;
46         protected PHPProjectSelector projectSelector;
47         protected PHPFileSelector fileSelector;
48         private Button fUseDefaultInterpreterButton;
49         private Button fInterpreterButton;
50         private Text fInterpreterText;
51         private Label fInterpreterLabel;
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 //              fInterpreterLabel= new Label(interpreterGroup, SWT.NONE);
145 //              fInterpreterLabel.setText("Interpreter: "); 
146 //              gd= new GridData();
147 //              gd.horizontalSpan = 2;
148 //              fInterpreterLabel.setLayoutData(gd);
149 //              fInterpreterLabel.setFont(font);
150                 
151                 fInterpreterText= new Text(interpreterGroup, SWT.SINGLE | SWT.BORDER);
152                 gd= new GridData(GridData.FILL_HORIZONTAL);
153                 fInterpreterText.setLayoutData(gd);
154                 fInterpreterText.setFont(font);
155                 fInterpreterText.addModifyListener(new ModifyListener() {
156                         public void modifyText(ModifyEvent evt) {
157                                 updateLaunchConfigurationDialog();
158                         }
159                 });
160
161                 
162                 fInterpreterButton= createPushButton(interpreterGroup,"Browse..", null);
163                 fInterpreterButton.addSelectionListener(new SelectionAdapter() {
164                         public void widgetSelected(SelectionEvent event) {
165                                 handleBrowseSellected(event);
166                         }
167                 });
168                 
169                 fUseDefaultInterpreterButton = new Button(interpreterGroup,SWT.CHECK);
170                 fUseDefaultInterpreterButton.setText("Use default interpreter");
171                 gd = new GridData(GridData.FILL_HORIZONTAL);
172                 fUseDefaultInterpreterButton.setLayoutData(gd);
173                 fUseDefaultInterpreterButton.setFont(font);
174                 fUseDefaultInterpreterButton.addSelectionListener(new SelectionAdapter() {
175                         public void widgetSelected(SelectionEvent event) {
176                                 handleDefaultSellected(event);
177                         }
178                 });
179
180         }
181         
182         /**
183          * Set the appropriate enabled state for the appletviewqer text widget.
184          */
185         protected void setInterpreterTextEnabledState() {
186                 if (isDefaultInterpreter()) {
187                         fInterpreterText.setEnabled(false);
188                         fInterpreterButton.setEnabled(false);
189                 } else {
190                         fInterpreterText.setEnabled(true);
191                         fInterpreterButton.setEnabled(true);
192                 }
193         }
194         
195         /**
196          * Returns whether the default appletviewer is to be used
197          */
198         protected boolean isDefaultInterpreter() {
199                 return fUseDefaultInterpreterButton.getSelection();
200         }
201
202
203
204         protected void handleDefaultSellected(SelectionEvent event) {
205                 setInterpreterTextEnabledState();
206                 updateLaunchConfigurationDialog();
207 //              if (isDefaultInterpreter()) {
208 //                      fInterpreterText.setText("default Interpreter");
209 //              }
210
211         }
212
213         protected void handleBrowseSellected(SelectionEvent event) {
214                 FileDialog dlg=new FileDialog(getShell(),SWT.OPEN);
215                 String fileName=dlg.open();
216                 if (fileName!=null) {
217                         fInterpreterText.setText(fileName);
218                         updateLaunchConfigurationDialog();
219                 }
220         }
221
222         protected IProject getContext() {
223                 IWorkbenchPage page= XDebugCorePlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
224                 if (page != null) {
225                         ISelection selection = page.getSelection();
226                         if (selection instanceof IStructuredSelection) {
227                                 IStructuredSelection ss = (IStructuredSelection) selection;
228                                 if (!ss.isEmpty()) {
229                                         Object obj = ss.getFirstElement();
230                                         if (obj instanceof IResource)
231                                                 return ((IResource) obj).getProject();
232                                 }
233                         }
234                         IEditorPart part = page.getActiveEditor();
235                         if (part != null) {
236                                 IEditorInput input = part.getEditorInput();
237                                 IResource file = (IResource) input.getAdapter(IResource.class);
238                                 if (file != null) {
239                                         return file.getProject();
240                                 }
241                         }
242                 }
243                 return null;
244         }
245
246         public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
247                 IProject project = getContext();
248                 if (project != null)
249                         configuration.setAttribute(IXDebugConstants.ATTR_PHP_PROJECT, project.getName());
250         }
251
252
253         public void initializeFrom(ILaunchConfiguration configuration) {
254                 try {
255                         String project = configuration.getAttribute(IXDebugConstants.ATTR_PHP_PROJECT, (String)null);
256                         if (project != null) {
257                         projectSelector.setSelectionText(project);
258                         }
259                         String file = configuration.getAttribute(IXDebugConstants.ATTR_PHP_FILE, (String)null);
260                         if (file != null) {
261                                 fileSelector.setSelectionText(file);
262                         }
263                         
264                         String interpreterFile=configuration.getAttribute(IXDebugConstants.ATTR_PHP_INTERPRETER, (String) null);
265                         if(interpreterFile!=null)
266                                 fInterpreterText.setText(interpreterFile);
267                         boolean selection=configuration.getAttribute(IXDebugConstants.ATTR_PHP_DEFAULT_INTERPRETER, true);
268                         fUseDefaultInterpreterButton.setSelection(selection);
269                         setInterpreterTextEnabledState();
270
271                 } catch (CoreException e) {
272                         setErrorMessage(e.getMessage());
273                 }
274         }
275
276         public void performApply(ILaunchConfigurationWorkingCopy configuration) {
277                 String project = projectSelector.getSelectionText().trim();
278                 if (project.length() == 0) {
279                         project = null;
280                 }
281                 configuration.setAttribute(IXDebugConstants.ATTR_PHP_PROJECT, project);
282
283                 IFile file = fileSelector.getSelection();
284                 configuration.setAttribute(IXDebugConstants.ATTR_PHP_FILE, file == null ? "" : file.getProjectRelativePath()
285                                 .toString());
286                 configuration.setAttribute(IXDebugConstants.ATTR_PHP_DEFAULT_INTERPRETER, this.fUseDefaultInterpreterButton.getSelection());
287                 configuration.setAttribute(IXDebugConstants.ATTR_PHP_INTERPRETER, this.fInterpreterText.getText());
288
289         }
290         
291         /* (non-Javadoc)
292          * @see org.eclipse.debug.ui.ILaunchConfigurationTab#isValid(org.eclipse.debug.core.ILaunchConfiguration)
293          */
294         public boolean isValid(ILaunchConfiguration launchConfig) {
295                 try {
296
297                         String projectName = launchConfig.getAttribute(IXDebugConstants.ATTR_PHP_PROJECT, "");
298                         if (projectName.length() == 0) {
299                                 setErrorMessage("Iinvalid Project");
300                                 return false;
301                         }
302
303                         String fileName = launchConfig.getAttribute(IXDebugConstants.ATTR_PHP_FILE, "");
304                         if (fileName.length() == 0) {
305                                 setErrorMessage("Invalid File");
306                                 return false;
307                         }
308                 } catch (CoreException e) {
309 //                      XDebugCorePlugin.log(e);
310                 }
311
312                 setErrorMessage(null);
313                 return super.isValid(launchConfig);
314         }
315         public Image getImage() {
316                 return PHPUiImages.get(PHPUiImages.IMG_CTOOLS_PHP_PAGE);
317         }
318         
319         public String getName() {
320                 return "Main";
321         }
322
323 }