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