Improved "project.index" file for PHPdoc TextHover information
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / actions / PHPEclipseShowAction.java
index f375331..3fbcb11 100644 (file)
@@ -18,6 +18,8 @@ import java.text.MessageFormat;
 import java.util.Iterator;
 
 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
+import net.sourceforge.phpeclipse.views.PHPConsole;
+
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.runtime.IPath;
@@ -28,11 +30,17 @@ import org.eclipse.jface.preference.IPreferenceStore;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.ISelectionProvider;
 import org.eclipse.jface.viewers.StructuredSelection;
-import org.eclipse.swt.program.Program;
+import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.ui.IObjectActionDelegate;
+import org.eclipse.ui.IViewPart;
+import org.eclipse.ui.IWorkbenchPage;
 import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.update.internal.ui.UpdatePerspective;
 import org.eclipse.ui.help.WorkbenchHelp;
+
+import org.eclipse.update.internal.ui.views.IEmbeddedWebBrowser;
 //import org.eclipse.jdt.internal.ui.actions.OpenBrowserUtil;
 // import org.eclipse.help.ui.browser.LaunchURL;
 
@@ -52,21 +60,13 @@ public class PHPEclipseShowAction implements IObjectActionDelegate {
     workbenchPart = targetPart;
   }
 
-  //  public static void open(final URL url, final Shell shell, final String dialogTitle) {
-  //    IHelp help= WorkbenchHelp.getHelpSupport();
-  //    if (help != null) {
-  //      WorkbenchHelp.getHelpSupport().displayHelpResource(url.toExternalForm());
-  //    } else {
-  //      showMessage(shell, dialogTitle, ActionMessages.getString("OpenBrowserUtil.help_not_available"), false); //$NON-NLS-1$
-  //    }
-  //  }
-
   public void run(IAction action) {
     ISelectionProvider selectionProvider = null;
     selectionProvider = workbenchPart.getSite().getSelectionProvider();
 
     StructuredSelection selection = null;
     selection = (StructuredSelection) selectionProvider.getSelection();
+    PHPConsole console = PHPConsole.getInstance();
 
     IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
 
@@ -87,37 +87,27 @@ public class PHPEclipseShowAction implements IObjectActionDelegate {
           case IResource.FILE :
             // single file:
             IFile file = (IFile) resource;
-            IPath path = file.getFullPath();
-
-            String localhostURL = file.getLocation().toString();
-            String lowerCaseFileName = localhostURL.toLowerCase();
-            //       fileName = "http://localhost"+fileName.replaceAll("c:", "");
-            String documentRoot = store.getString(PHPeclipsePlugin.DOCUMENTROOT_PREF);
-            documentRoot = documentRoot.replace('\\', '/');
-            documentRoot = documentRoot.toLowerCase();
-            
-            if (lowerCaseFileName.startsWith(documentRoot)) {
-              localhostURL = localhostURL.substring(documentRoot.length());
-            } else {
-              MessageDialog.openInformation(shell, "Wrong DocumentRoot", "Adjust DocumentRoot: " + documentRoot);
+          String localhostURL;
+            if ((localhostURL=getLocalhostURL(store, (IFile) resource)) == null) {
+              MessageDialog.openInformation(shell, "Couldn't create localhost URL", "Please configure your localhost and documentRoot");
               return;
             }
-
-            localhostURL = store.getString(PHPeclipsePlugin.LOCALHOST_PREF) + localhostURL.replaceAll(documentRoot, "");
-
             try {
               if (store.getBoolean(PHPeclipsePlugin.USE_EXTERNAL_BROWSER_PREF)) {
                 String[] arguments = { localhostURL };
                 MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF));
 
                 Runtime runtime = Runtime.getRuntime();
-                runtime.exec(form.format(arguments));
+                String command = form.format(arguments);
+                console.write("External Browser command: " + command + "\n");
+                runtime.exec(command);
                 //                     runtime.exec(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF) + " " + fileName);
                 //                                                             runtime.exec("command.com /c start iexplore " + fileName);
               } else {
-            //    MessageDialog.openInformation(shell, "localhostURL", "localhostURL: " + localhostURL);
-            //  this doesn't work under win98 ?
-            //     Program.launch(localhostURL);
+                //    MessageDialog.openInformation(shell, "localhostURL", "localhostURL: " + localhostURL);
+                //  this doesn't work under win98 ?
+                //     Program.launch(localhostURL);
+                console.write("Internal Browser URL: " + localhostURL + "\n");
                 open(new URL(localhostURL), shell, localhostURL);
               }
             } catch (MalformedURLException e) {
@@ -129,18 +119,57 @@ public class PHPEclipseShowAction implements IObjectActionDelegate {
         }
       }
     }
-  } /**
-                                                * @see IActionDelegate#selectionChanged(IAction, ISelection)
-                                                */
+  }
+
+  /**
+   * @see IActionDelegate#selectionChanged(IAction, ISelection)
+   */
   public void selectionChanged(IAction action, ISelection selection) {
   }
 
+  public static String getLocalhostURL(IPreferenceStore store, IFile file) {
+    if (store == null) {
+      store = PHPeclipsePlugin.getDefault().getPreferenceStore();
+    }
+
+   // IPath path = file.getFullPath();
+
+    String localhostURL = file.getLocation().toString();
+    String lowerCaseFileName = localhostURL.toLowerCase();
+    String documentRoot = store.getString(PHPeclipsePlugin.DOCUMENTROOT_PREF);
+    documentRoot = documentRoot.replace('\\', '/');
+    documentRoot = documentRoot.toLowerCase();
+
+    if (lowerCaseFileName.startsWith(documentRoot)) {
+      localhostURL = localhostURL.substring(documentRoot.length());
+    } else {
+      return null;
+    }
+
+    return store.getString(PHPeclipsePlugin.LOCALHOST_PREF) + localhostURL; 
+
+  }
+
   public static void open(final URL url, final Shell shell, final String dialogTitle) {
-    IHelp help = WorkbenchHelp.getHelpSupport();
-    if (help != null) {
-      WorkbenchHelp.getHelpSupport().displayHelpResource(url.toExternalForm());
+    if (SWT.getPlatform().equals("win32")) {
+      IWorkbenchPage page = PHPeclipsePlugin.getActivePage();
+      try {
+        IViewPart part = page.findView(UpdatePerspective.ID_BROWSER);
+        if (part == null) {
+          part = page.showView(UpdatePerspective.ID_BROWSER);
+        } else
+          page.bringToTop(part);
+        ((IEmbeddedWebBrowser) part).openTo(url.toExternalForm());
+      } catch (PartInitException e) {
+        PHPeclipsePlugin.log(e);
+      }
     } else {
-      //   showMessage(shell, dialogTitle, ActionMessages.getString("OpenBrowserUtil.help_not_available"), false); //$NON-NLS-1$
+      IHelp help = WorkbenchHelp.getHelpSupport();
+      if (help != null) {
+        WorkbenchHelp.getHelpSupport().displayHelpResource(url.toExternalForm());
+      } else {
+        //   showMessage(shell, dialogTitle, ActionMessages.getString("OpenBrowserUtil.help_not_available"), false); //$NON-NLS-1$
+      }
     }
   }
 }