Refactory: removed unnecessary local variables and imports.
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / ui / editor / BrowserUtil.java
index a70b636..fa3be6f 100644 (file)
@@ -1,16 +1,28 @@
 package net.sourceforge.phpeclipse.ui.editor;
 
+import java.net.MalformedURLException;
+import java.net.URL;
+
 import net.sourceforge.phpeclipse.ui.IPreferenceConstants;
 import net.sourceforge.phpeclipse.ui.WebUI;
+import net.sourceforge.phpeclipse.ui.internal.WebUIMessages;
 import net.sourceforge.phpeclipse.ui.overlaypages.ProjectPrefUtil;
 import net.sourceforge.phpeclipse.webbrowser.views.BrowserView;
 import net.sourceforge.phpeclipse.webbrowser.views.ShowInContextBrowser;
 
 import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.jface.dialogs.ErrorDialog;
+import org.eclipse.swt.widgets.Shell;
 import org.eclipse.ui.IViewPart;
 import org.eclipse.ui.IViewReference;
 import org.eclipse.ui.IWorkbenchPage;
 import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.browser.IWebBrowser;
+import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
 import org.eclipse.ui.internal.Perspective;
 import org.eclipse.ui.internal.WorkbenchPage;
 import org.eclipse.ui.part.IShowInTarget;
@@ -18,28 +30,33 @@ import org.eclipse.ui.part.ShowInContext;
 
 public class BrowserUtil {
 
-       public static ShowInContextBrowser getShowInContext(IFile previewFile, boolean forceDBGPreview, String postFix) {
-               String extension = previewFile.getFileExtension().toLowerCase();
+       public static ShowInContextBrowser getShowInContext(IFile previewFile,
+                       boolean forceDBGPreview, String postFix) {
+               //String extension = previewFile.getFileExtension().toLowerCase();
                // boolean showHTMLFilesLocal = false;
                // boolean showXMLFilesLocal = false;
-               boolean isHTMLFileName = false;
-               boolean isXMLFileName = false;
+//             boolean isHTMLFileName = false;
+//             boolean isXMLFileName = false;
                String localhostURL;
                if (!forceDBGPreview) {
                        // showHTMLFilesLocal =
                        // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
                        // IPreferenceConstants.PHP_SHOW_HTML_FILES_LOCAL);
-                       // showXMLFilesLocal = ProjectPrefUtil.getPreviewBooleanValue(previewFile,
+                       // showXMLFilesLocal =
+                       // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
                        // IPreferenceConstants.PHP_SHOW_XML_FILES_LOCAL);
-                       isHTMLFileName = "html".equals(extension) || "htm".equals(extension) || "xhtml".equals(extension);
-                       isXMLFileName = "xml".equals(extension) || "xsd".equals(extension) || "dtd".equals(extension);
+//                     isHTMLFileName = "html".equals(extension)
+//                                     || "htm".equals(extension) || "xhtml".equals(extension);
+//                     isXMLFileName = "xml".equals(extension) || "xsd".equals(extension)
+//                                     || "dtd".equals(extension);
                }
                // if (showHTMLFilesLocal && isHTMLFileName) {
                // localhostURL = previewFile.getLocation().toString();
                // } else if (showXMLFilesLocal && isXMLFileName) {
                // localhostURL = previewFile.getLocation().toString();
                // } else
-               if ((localhostURL = ShowExternalPreviewAction.getLocalhostURL(null, previewFile)) == null) {
+               if ((localhostURL = ShowExternalPreviewAction.getLocalhostURL(null,
+                               previewFile)) == null) {
                        return new ShowInContextBrowser(previewFile, null, null);
                }
                localhostURL += postFix;
@@ -49,9 +66,9 @@ public class BrowserUtil {
        /**
         * Returns the <code>IShowInTarget</code> for the given part, or
         * <code>null</code> if it does not provide one.
-        *
+        * 
         * @param targetPart
-        *          the target part
+        *            the target part
         * @return the <code>IShowInTarget</code> or <code>null</code>
         */
        private static IShowInTarget getShowInTarget(IWorkbenchPart targetPart) {
@@ -65,7 +82,8 @@ public class BrowserUtil {
                return null;
        }
 
-       public static void showPreview(IFile previewFile, boolean forceDBGPreview, String postFix) {
+       public static void showPreview(IFile previewFile, boolean forceDBGPreview,
+                       String postFix) {
                if (previewFile == null) {
                        // should never happen
                        return;
@@ -79,7 +97,9 @@ public class BrowserUtil {
                        // boolean isHTMLFileName = false;
                        // boolean isXMLFileName = false;
                        if (!forceDBGPreview) {
-                               autoPreview = ProjectPrefUtil.getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
+                               autoPreview = ProjectPrefUtil.getPreviewBooleanValue(
+                                               previewFile,
+                                               IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
 
                                // showHTMLFilesLocal =
                                // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
@@ -87,9 +107,11 @@ public class BrowserUtil {
                                // showXMLFilesLocal =
                                // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
                                // IPreferenceConstants.PHP_SHOW_XML_FILES_LOCAL);
-                               // isHTMLFileName = "html".equals(extension) || "htm".equals(extension)
+                               // isHTMLFileName = "html".equals(extension) ||
+                               // "htm".equals(extension)
                                // || "xhtml".equals(extension);
-                               // isXMLFileName = "xml".equals(extension) || "xsd".equals(extension) ||
+                               // isXMLFileName = "xml".equals(extension) ||
+                               // "xsd".equals(extension) ||
                                // "dtd".equals(extension);
                        }
                        if (autoPreview) {
@@ -99,43 +121,68 @@ public class BrowserUtil {
                                // } else if (showXMLFilesLocal && isXMLFileName) {
                                // localhostURL = previewFile.getLocation().toString();
                                // } else if ((localhostURL =
-                               // ShowExternalPreviewAction.getLocalhostURL(null, previewFile)) ==
+                               // ShowExternalPreviewAction.getLocalhostURL(null, previewFile))
+                               // ==
                                // null) {
                                // return;
                                // }
                                // localhostURL += postFix;
-                               ShowInContext context = getShowInContext(previewFile, forceDBGPreview, postFix);
+                               ShowInContext context = getShowInContext(previewFile,
+                                               forceDBGPreview, postFix);
                                IWorkbenchPart sourcePart = page.getActivePart();
                                if (sourcePart == null && context != null) {
                                        return;
                                }
 
                                // try {
-                               Perspective persp = ((WorkbenchPage) page).getActivePerspective();
+                               Perspective persp = ((WorkbenchPage) page)
+                                               .getActivePerspective();
                                if (persp != null) {
 
                                        // If this view is already visible just return.
-                                       IViewReference ref = persp.findView(BrowserView.ID_BROWSER, null);
+                                       IViewReference ref = persp.findView(BrowserView.ID_BROWSER,
+                                                       null);
                                        IViewPart view = null;
                                        if (ref != null) {
                                                view = ref.getView(true);
                                        }
+                                       if (view == null && forceDBGPreview) {
+                                               try {
+                                                       view = persp.showView(BrowserView.ID_BROWSER, null);
+                                                       persp.bringToTop(persp.findView(
+                                                                       BrowserView.ID_BROWSER, null));
+                                               } catch (PartInitException e) {
+                                                       WebUI.log(e);
+                                               }
+                                       }
                                        if (view != null) {
                                                IShowInTarget target = getShowInTarget(view);
-                                               if (target != null && target.show(context)) {
-                                                       // success
+                                               boolean stickyBrowserURL = ProjectPrefUtil.getPreviewBooleanValue(
+                                                               previewFile,
+                                                               IPreferenceConstants.PHP_STICKY_BROWSER_URL_DEFAULT);
+                                               if (target != null) {
+                                                       if (stickyBrowserURL
+                                                                       && ((BrowserView) target).getUrl() != null
+                                                                       && ((BrowserView) target).getUrl().length() > 0) {
+                                                               ((BrowserView) target).refresh();
+                                                       } else {
+                                                               target.show(context);
+                                                       }
                                                }
-                                               ((WorkbenchPage) page).performedShowIn(BrowserView.ID_BROWSER);
+                                               ((WorkbenchPage) page)
+                                                               .performedShowIn(BrowserView.ID_BROWSER);
                                        }
                                }
 
                                // IViewPart view = page.showView(BrowserView.ID_BROWSER);
                                // IShowInTarget target = getShowInTarget(view);
-                               // if (target != null && target.show(new ShowInContext(localhostURL,
+                               // if (target != null && target.show(new
+                               // ShowInContext(localhostURL,
                                // null))) {
                                // // success
                                // }
-                               // ((WorkbenchPage) page).performedShowIn(BrowserView.ID_BROWSER); //
+                               // ((WorkbenchPage)
+                               // page).performedShowIn(BrowserView.ID_BROWSER); //
                                // TODO: move back up
                                // } catch (PartInitException e) {
                                // WorkbenchPlugin.log(
@@ -160,4 +207,77 @@ public class BrowserUtil {
                }
        }
 
+       private static final String BROWSER_ID = "net.sourceforge.phpeclipse.browser";
+
+       /**
+        * convenient method to show browser as Editor
+        * 
+        */
+       public static void showBrowserAsEditor(IFile file, String queryString) {
+               showBrowser(IWorkbenchBrowserSupport.AS_EDITOR, file, queryString);
+       }
+
+       /**
+        * convenient method to show browser as External Web Browser
+        * 
+        */
+       public static void showBrowserAsExternal(IFile file, String queryString) {
+               showBrowser(IWorkbenchBrowserSupport.AS_EXTERNAL, file, queryString);
+       }
+
+       /**
+        * convenient method to show browser as View
+        * 
+        */
+       public static void showBrowserAsView(IFile file, String queryString) {
+               showBrowser(IWorkbenchBrowserSupport.AS_VIEW, file, queryString);
+       }
+
+       /**
+        * Show browser according to General settings
+        * 
+        * See IWorkbenchBrowserSupport and DefaultWorkbenchBrowserSupport.
+        */
+       public static void showBrowser(int style, IFile file, String queryString) {
+               ShowInContextBrowser context = getShowInContext(file, true, queryString);
+               String url = context.getLocalhostUrl();
+               if (url == null) {
+                       String dialogTitle = WebUIMessages
+                                       .getString("BrowserUtil.error.dialog.title");
+                       String message = WebUIMessages
+                                       .getString("BrowserUtil.null.url.message");
+                       String reason = WebUIMessages
+                                       .getString("BrowserUtil.null.url.reason");
+                       IStatus status = new Status(IStatus.ERROR, WebUI.PLUGIN_ID, 0,
+                                       reason, null);
+                       ErrorDialog.openError(new Shell(), dialogTitle, message, status);
+                       return;
+               }
+               String id = BROWSER_ID;
+               switch (style) {
+               case IWorkbenchBrowserSupport.AS_EXTERNAL:
+                       id += ".x";
+                       break;
+               case IWorkbenchBrowserSupport.AS_EDITOR:
+                       id += ".e";
+                       break;
+               case IWorkbenchBrowserSupport.AS_VIEW:
+                       id += ".v";
+                       break;
+               }
+               style |= IWorkbenchBrowserSupport.LOCATION_BAR
+                               | IWorkbenchBrowserSupport.NAVIGATION_BAR
+                               | IWorkbenchBrowserSupport.STATUS;
+               try {
+                       IWebBrowser browser = PlatformUI.getWorkbench().getBrowserSupport()
+                                       .createBrowser(style, id, null, url);
+                       browser.openURL(new URL(url));
+
+               } catch (PartInitException e) {
+                       WebUI.log(e);
+               } catch (MalformedURLException e) {
+                       WebUI.log(e);
+               }
+       }
+
 }