Modified: 1596135 - debugger: open browser when debugging starts
[phpeclipse.git] / net.sourceforge.phpeclipse.xdebug.ui / src / net / sourceforge / phpeclipse / xdebug / ui / php / launching / PHPMainTab.java
index 03e5e3e..ed5f1b0 100644 (file)
@@ -1,5 +1,7 @@
 package net.sourceforge.phpeclipse.xdebug.ui.php.launching;
 
+import java.io.File;
+
 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
 import net.sourceforge.phpdt.internal.ui.util.PHPFileSelector;
 import net.sourceforge.phpdt.internal.ui.util.PHPProjectSelector;
@@ -9,6 +11,7 @@ import net.sourceforge.phpeclipse.xdebug.php.launching.IXDebugConstants;
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.debug.core.ILaunchConfiguration;
 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
@@ -28,7 +31,6 @@ import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.FileDialog;
 import org.eclipse.swt.widgets.Group;
-import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Text;
 import org.eclipse.ui.IEditorInput;
 import org.eclipse.ui.IEditorPart;
@@ -38,17 +40,23 @@ public class PHPMainTab extends AbstractLaunchConfigurationTab {
 
        // Project UI widgets
        protected Text fProjText;
+
        protected Button fProjButton;
 
        // Main class UI widgets
        protected Text fMainText;
+
        protected Button fSearchButton;
+
        protected PHPProjectSelector projectSelector;
+
        protected PHPFileSelector fileSelector;
+
        private Button fUseDefaultInterpreterButton;
+
        private Button fInterpreterButton;
+
        private Text fInterpreterText;
-       private Label fInterpreterLabel;
 
        public PHPMainTab() {
                super();
@@ -56,30 +64,32 @@ public class PHPMainTab extends AbstractLaunchConfigurationTab {
 
        public void createControl(Composite parent) {
                Font font = parent.getFont();
-               
+
                Composite comp = new Composite(parent, SWT.NONE);
                setControl(comp);
-//             PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaDebugHelpContextIds.LAUNCH_CONFIGURATION_DIALOG_MAIN_TAB);
+               // PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(),
+               // IJavaDebugHelpContextIds.LAUNCH_CONFIGURATION_DIALOG_MAIN_TAB);
                GridLayout topLayout = new GridLayout();
                topLayout.verticalSpacing = 0;
                comp.setLayout(topLayout);
                comp.setFont(font);
-               
+
                createProjectEditor(comp);
                createVerticalSpacer(comp, 1);
                createMainTypeEditor(comp);
                createVerticalSpacer(comp, 1);
                createInterpreterEditor(comp);
        }
-       
+
        /**
         * Creates the widgets for specifying a main type.
         * 
-        * @param parent the parent composite
+        * @param parent
+        *            the parent composite
         */
        private void createProjectEditor(Composite parent) {
-               Font font= parent.getFont();
-               Group group= new Group(parent, SWT.NONE);
+               Font font = parent.getFont();
+               Group group = new Group(parent, SWT.NONE);
                group.setText("Project:");
                GridData gd = new GridData(GridData.FILL_HORIZONTAL);
                group.setLayoutData(gd);
@@ -89,25 +99,26 @@ public class PHPMainTab extends AbstractLaunchConfigurationTab {
                group.setFont(font);
 
                projectSelector = new PHPProjectSelector(group);
-               projectSelector.setBrowseDialogMessage("Choose the project containing the application entry point:");
+               projectSelector
+                               .setBrowseDialogMessage("Choose the project containing the application entry point:");
                projectSelector.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
                projectSelector.addModifyListener(new ModifyListener() {
                        public void modifyText(ModifyEvent evt) {
                                updateLaunchConfigurationDialog();
                        }
                });
-       }       
+       }
 
-       
        /**
         * Creates the widgets for specifying a php file.
         * 
-        * @param parent the parent composite
+        * @param parent
+        *            the parent composite
         */
        private void createMainTypeEditor(Composite parent) {
-               Font font= parent.getFont();
-               Group mainGroup= new Group(parent, SWT.NONE);
-               mainGroup.setText("File: "); 
+               Font font = parent.getFont();
+               Group mainGroup = new Group(parent, SWT.NONE);
+               mainGroup.setText("File: ");
                GridData gd = new GridData(GridData.FILL_HORIZONTAL);
                mainGroup.setLayoutData(gd);
                GridLayout layout = new GridLayout();
@@ -116,7 +127,8 @@ public class PHPMainTab extends AbstractLaunchConfigurationTab {
                mainGroup.setFont(font);
 
                fileSelector = new PHPFileSelector(mainGroup, projectSelector);
-               fileSelector.setBrowseDialogMessage("Choose the PHP file that represents the application entry point:");
+               fileSelector
+                               .setBrowseDialogMessage("Choose the PHP file that represents the application entry point:");
                fileSelector.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
                fileSelector.addModifyListener(new ModifyListener() {
                        public void modifyText(ModifyEvent evt) {
@@ -124,32 +136,26 @@ public class PHPMainTab extends AbstractLaunchConfigurationTab {
                        }
                });
        }
-       
+
        /**
         * Creates the widgets for specifying debug settings.
         * 
-        * @param parent the parent composite
+        * @param parent
+        *            the parent composite
         */
        private void createInterpreterEditor(Composite parent) {
-               Font font= parent.getFont();
-               Group interpreterGroup= new Group(parent, SWT.NONE);
-               interpreterGroup.setText("Interpreter: "); 
+               Font font = parent.getFont();
+               Group interpreterGroup = new Group(parent, SWT.NONE);
+               interpreterGroup.setText("Interpreter: ");
                GridData gd = new GridData(GridData.FILL_HORIZONTAL);
                interpreterGroup.setLayoutData(gd);
                GridLayout layout = new GridLayout();
                layout.numColumns = 2;
                interpreterGroup.setLayout(layout);
                interpreterGroup.setFont(font);
-               
-//             fInterpreterLabel= new Label(interpreterGroup, SWT.NONE);
-//             fInterpreterLabel.setText("Interpreter: "); 
-//             gd= new GridData();
-//             gd.horizontalSpan = 2;
-//             fInterpreterLabel.setLayoutData(gd);
-//             fInterpreterLabel.setFont(font);
-               
-               fInterpreterText= new Text(interpreterGroup, SWT.SINGLE | SWT.BORDER);
-               gd= new GridData(GridData.FILL_HORIZONTAL);
+
+               fInterpreterText = new Text(interpreterGroup, SWT.SINGLE | SWT.BORDER);
+               gd = new GridData(GridData.FILL_HORIZONTAL);
                fInterpreterText.setLayoutData(gd);
                fInterpreterText.setFont(font);
                fInterpreterText.addModifyListener(new ModifyListener() {
@@ -158,27 +164,28 @@ public class PHPMainTab extends AbstractLaunchConfigurationTab {
                        }
                });
 
-               
-               fInterpreterButton= createPushButton(interpreterGroup,"Browse..", null);
+               fInterpreterButton = createPushButton(interpreterGroup, "Browse..",
+                               null);
                fInterpreterButton.addSelectionListener(new SelectionAdapter() {
                        public void widgetSelected(SelectionEvent event) {
                                handleBrowseSellected(event);
                        }
                });
-               
-               fUseDefaultInterpreterButton = new Button(interpreterGroup,SWT.CHECK);
+
+               fUseDefaultInterpreterButton = new Button(interpreterGroup, SWT.CHECK);
                fUseDefaultInterpreterButton.setText("Use default interpreter");
                gd = new GridData(GridData.FILL_HORIZONTAL);
                fUseDefaultInterpreterButton.setLayoutData(gd);
                fUseDefaultInterpreterButton.setFont(font);
-               fUseDefaultInterpreterButton.addSelectionListener(new SelectionAdapter() {
-                       public void widgetSelected(SelectionEvent event) {
-                               handleDefaultSellected(event);
-                       }
-               });
+               fUseDefaultInterpreterButton
+                               .addSelectionListener(new SelectionAdapter() {
+                                       public void widgetSelected(SelectionEvent event) {
+                                               handleDefaultSellected(event);
+                                       }
+                               });
 
        }
-       
+
        /**
         * Set the appropriate enabled state for the appletviewqer text widget.
         */
@@ -191,7 +198,7 @@ public class PHPMainTab extends AbstractLaunchConfigurationTab {
                        fInterpreterButton.setEnabled(true);
                }
        }
-       
+
        /**
         * Returns whether the default appletviewer is to be used
         */
@@ -199,28 +206,27 @@ public class PHPMainTab extends AbstractLaunchConfigurationTab {
                return fUseDefaultInterpreterButton.getSelection();
        }
 
-
-
        protected void handleDefaultSellected(SelectionEvent event) {
                setInterpreterTextEnabledState();
                updateLaunchConfigurationDialog();
-//             if (isDefaultInterpreter()) {
-//                     fInterpreterText.setText("default Interpreter");
-//             }
+               // if (isDefaultInterpreter()) {
+               // fInterpreterText.setText("default Interpreter");
+               // }
 
        }
 
        protected void handleBrowseSellected(SelectionEvent event) {
-               FileDialog dlg=new FileDialog(getShell(),SWT.OPEN);
-               String fileName=dlg.open();
-               if (fileName!=null) {
+               FileDialog dlg = new FileDialog(getShell(), SWT.OPEN);
+               String fileName = dlg.open();
+               if (fileName != null) {
                        fInterpreterText.setText(fileName);
                        updateLaunchConfigurationDialog();
                }
        }
 
        protected IProject getContext() {
-               IWorkbenchPage page= XDebugCorePlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
+               IWorkbenchPage page = XDebugCorePlugin.getDefault().getWorkbench()
+                               .getActiveWorkbenchWindow().getActivePage();
                if (page != null) {
                        ISelection selection = page.getSelection();
                        if (selection instanceof IStructuredSelection) {
@@ -246,25 +252,29 @@ public class PHPMainTab extends AbstractLaunchConfigurationTab {
        public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
                IProject project = getContext();
                if (project != null)
-                       configuration.setAttribute(IXDebugConstants.ATTR_PHP_PROJECT, project.getName());
+                       configuration.setAttribute(IXDebugConstants.ATTR_PHP_PROJECT,
+                                       project.getName());
        }
 
-
        public void initializeFrom(ILaunchConfiguration configuration) {
                try {
-                       String project = configuration.getAttribute(IXDebugConstants.ATTR_PHP_PROJECT, (String)null);
+                       String project = configuration.getAttribute(
+                                       IXDebugConstants.ATTR_PHP_PROJECT, (String) null);
                        if (project != null) {
-                       projectSelector.setSelectionText(project);
+                               projectSelector.setSelectionText(project);
                        }
-                       String file = configuration.getAttribute(IXDebugConstants.ATTR_PHP_FILE, (String)null);
+                       String file = configuration.getAttribute(
+                                       IXDebugConstants.ATTR_PHP_FILE, (String) null);
                        if (file != null) {
                                fileSelector.setSelectionText(file);
                        }
-                       
-                       String interpreterFile=configuration.getAttribute(IXDebugConstants.ATTR_PHP_INTERPRETER, (String) null);
-                       if(interpreterFile!=null)
+
+                       String interpreterFile = configuration.getAttribute(
+                                       IXDebugConstants.ATTR_PHP_INTERPRETER, (String) null);
+                       if (interpreterFile != null)
                                fInterpreterText.setText(interpreterFile);
-                       boolean selection=configuration.getAttribute(IXDebugConstants.ATTR_PHP_DEFAULT_INTERPRETER, true);
+                       boolean selection = configuration.getAttribute(
+                                       IXDebugConstants.ATTR_PHP_DEFAULT_INTERPRETER, true);
                        fUseDefaultInterpreterButton.setSelection(selection);
                        setInterpreterTextEnabledState();
 
@@ -281,41 +291,55 @@ public class PHPMainTab extends AbstractLaunchConfigurationTab {
                configuration.setAttribute(IXDebugConstants.ATTR_PHP_PROJECT, project);
 
                IFile file = fileSelector.getSelection();
-               configuration.setAttribute(IXDebugConstants.ATTR_PHP_FILE, file == null ? "" : file.getProjectRelativePath()
-                               .toString());
-               configuration.setAttribute(IXDebugConstants.ATTR_PHP_DEFAULT_INTERPRETER, this.fUseDefaultInterpreterButton.getSelection());
-               configuration.setAttribute(IXDebugConstants.ATTR_PHP_INTERPRETER, this.fInterpreterText.getText());
+               configuration.setAttribute(IXDebugConstants.ATTR_PHP_FILE,
+                               file == null ? "" : file.getProjectRelativePath().toString());
+               configuration.setAttribute(
+                               IXDebugConstants.ATTR_PHP_DEFAULT_INTERPRETER,
+                               this.fUseDefaultInterpreterButton.getSelection());
+               configuration.setAttribute(IXDebugConstants.ATTR_PHP_INTERPRETER,
+                               this.fInterpreterText.getText());
 
        }
-       
-       /* (non-Javadoc)
+
+       /*
+        * (non-Javadoc)
+        * 
         * @see org.eclipse.debug.ui.ILaunchConfigurationTab#isValid(org.eclipse.debug.core.ILaunchConfiguration)
         */
        public boolean isValid(ILaunchConfiguration launchConfig) {
-               try {
-
-                       String projectName = launchConfig.getAttribute(IXDebugConstants.ATTR_PHP_PROJECT, "");
-                       if (projectName.length() == 0) {
-                               setErrorMessage("Iinvalid Project");
-                               return false;
-                       }
-
-                       String fileName = launchConfig.getAttribute(IXDebugConstants.ATTR_PHP_FILE, "");
-                       if (fileName.length() == 0) {
-                               setErrorMessage("Invalid File");
+               setErrorMessage(null);
+               String projectName = projectSelector.getSelectionText().trim();
+               IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(
+                               projectName);
+               if (!project.exists()) {
+                       setErrorMessage("Project does not exist");
+                       return false;
+               }
+               String fileName = fileSelector.getSelectionText().trim();
+               if (fileName.equals("")) {
+                       setErrorMessage("No file selected.");
+                       return false;
+               }
+               IFile file = project.getFile(fileName);
+               if (!file.exists()) {
+                       setErrorMessage("File does not exist");
+                       return false;
+               }
+               if (!fUseDefaultInterpreterButton.getSelection()) {
+                       File exe = new File(fInterpreterText.getText());
+                       System.out.println(exe.toString());
+                       if (!exe.exists()) {
+                               setErrorMessage("Invalid Interpreter");
                                return false;
                        }
-               } catch (CoreException e) {
-//                     XDebugCorePlugin.log(e);
                }
-
-               setErrorMessage(null);
-               return super.isValid(launchConfig);
+               return true;
        }
+
        public Image getImage() {
                return PHPUiImages.get(PHPUiImages.IMG_CTOOLS_PHP_PAGE);
        }
-       
+
        public String getName() {
                return "Main";
        }