Created a separated 'externaltools' plugin
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / ui / editor / BrowserUtil.java
1 package net.sourceforge.phpeclipse.ui.editor;
2
3 import net.sourceforge.phpeclipse.ui.IPreferenceConstants;
4 import net.sourceforge.phpeclipse.ui.WebUI;
5 import net.sourceforge.phpeclipse.ui.overlaypages.ProjectPrefUtil;
6 import net.sourceforge.phpeclipse.webbrowser.views.BrowserView;
7
8 import org.eclipse.core.resources.IFile;
9 import org.eclipse.ui.IViewPart;
10 import org.eclipse.ui.IWorkbenchPage;
11
12  
13 public class BrowserUtil {
14
15   public static void showPreview(IFile previewFile, boolean forceDBGPreview, String postFix) {
16     if (previewFile == null) {
17       // should never happen
18       return;
19     }
20     String extension = previewFile.getFileExtension().toLowerCase();
21     boolean autoPreview = forceDBGPreview;
22     boolean bringToTopPreview = true;
23     boolean showHTMLFilesLocal = false;
24     boolean showXMLFilesLocal = false;
25     boolean isHTMLFileName = false;
26     boolean isXMLFileName = false;
27     if (!forceDBGPreview) {
28       autoPreview = ProjectPrefUtil.getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
29   
30       bringToTopPreview = ProjectPrefUtil
31           .getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_BRING_TO_TOP_PREVIEW_DEFAULT);
32       showHTMLFilesLocal = ProjectPrefUtil.getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_SHOW_HTML_FILES_LOCAL);
33       showXMLFilesLocal = ProjectPrefUtil.getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_SHOW_XML_FILES_LOCAL);
34       isHTMLFileName = "html".equals(extension) || "htm".equals(extension) || "xhtml".equals(extension);
35       isXMLFileName = "xml".equals(extension) || "xsd".equals(extension) || "dtd".equals(extension);
36     }
37     if (autoPreview) {
38       String localhostURL;
39       if (showHTMLFilesLocal && isHTMLFileName) {
40         localhostURL = previewFile.getLocation().toString();
41       } else if (showXMLFilesLocal && isXMLFileName) {
42         localhostURL = previewFile.getLocation().toString();
43       } else if ((localhostURL = ShowExternalPreviewAction.getLocalhostURL(null, previewFile)) == null) {
44         return;
45       }
46       localhostURL += postFix;
47       
48       try {
49         IWorkbenchPage page = WebUI.getActivePage();
50         IViewPart part = page.findView(BrowserView.ID_BROWSER);
51         if (part == null) {
52           part = page.showView(BrowserView.ID_BROWSER);
53         } else {
54           if (bringToTopPreview) {
55             page.bringToTop(part);
56           }
57         }
58         ((BrowserView) part).setUrl(localhostURL);
59   
60       } catch (Exception e) {
61 //        PHPeclipsePlugin.log(e);
62       }
63     }
64   }
65
66 }