Optimized Scanner
[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 public class BrowserUtil {
13
14         public static void showPreview(IFile previewFile, boolean forceDBGPreview, String postFix) {
15                 if (previewFile == null) {
16                         // should never happen
17                         return;
18                 }
19                 String extension = previewFile.getFileExtension().toLowerCase();
20                 boolean autoPreview = forceDBGPreview;
21                 boolean bringToTopPreview = true;
22                 boolean showHTMLFilesLocal = false;
23                 boolean showXMLFilesLocal = false;
24                 boolean isHTMLFileName = false;
25                 boolean isXMLFileName = false;
26                 if (!forceDBGPreview) {
27                         autoPreview = ProjectPrefUtil.getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
28
29                         bringToTopPreview = ProjectPrefUtil
30                                         .getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_BRING_TO_TOP_PREVIEW_DEFAULT);
31                         showHTMLFilesLocal = ProjectPrefUtil.getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_SHOW_HTML_FILES_LOCAL);
32                         showXMLFilesLocal = ProjectPrefUtil.getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_SHOW_XML_FILES_LOCAL);
33                         isHTMLFileName = "html".equals(extension) || "htm".equals(extension) || "xhtml".equals(extension);
34                         isXMLFileName = "xml".equals(extension) || "xsd".equals(extension) || "dtd".equals(extension);
35                 }
36                 if (autoPreview) {
37                         String localhostURL;
38                         if (showHTMLFilesLocal && isHTMLFileName) {
39                                 localhostURL = previewFile.getLocation().toString();
40                         } else if (showXMLFilesLocal && isXMLFileName) {
41                                 localhostURL = previewFile.getLocation().toString();
42                         } else if ((localhostURL = ShowExternalPreviewAction.getLocalhostURL(null, previewFile)) == null) {
43                                 return;
44                         }
45                         localhostURL += postFix;
46
47                         try {
48                                 IWorkbenchPage page = WebUI.getActivePage();
49                                 IViewPart part = page.showView(BrowserView.ID_BROWSER, null, IWorkbenchPage.VIEW_VISIBLE);
50                                 // if (part == null) {
51                                 // part = page.showView(BrowserView.ID_BROWSER);
52                                 // } else {
53                                 if (part != null) {
54                                         if (bringToTopPreview) { 
55                                                 page.bringToTop(part);
56                                         }
57                                         ((BrowserView) part).setUrl(localhostURL);
58                                 }
59                                 // }
60
61                         } catch (Exception e) {
62                                 // PHPeclipsePlugin.log(e);
63                         }
64                 }
65         }
66
67 }