095d62455d67009139fa89f3522f10ed2b5943d0
[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 import net.sourceforge.phpeclipse.webbrowser.views.ShowInContextBrowser;
8
9 import org.eclipse.core.resources.IFile;
10 import org.eclipse.ui.IViewPart;
11 import org.eclipse.ui.IViewReference;
12 import org.eclipse.ui.IWorkbenchPage;
13 import org.eclipse.ui.IWorkbenchPart;
14 import org.eclipse.ui.internal.Perspective;
15 import org.eclipse.ui.internal.WorkbenchPage;
16 import org.eclipse.ui.part.IShowInTarget;
17 import org.eclipse.ui.part.ShowInContext;
18
19 public class BrowserUtil {
20
21         public static ShowInContextBrowser getShowInContext(IFile previewFile, boolean forceDBGPreview, String postFix) {
22                 String extension = previewFile.getFileExtension().toLowerCase();
23                 // boolean showHTMLFilesLocal = false;
24                 // boolean showXMLFilesLocal = false;
25                 boolean isHTMLFileName = false;
26                 boolean isXMLFileName = false;
27                 String localhostURL;
28                 if (!forceDBGPreview) {
29                         // showHTMLFilesLocal =
30                         // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
31                         // IPreferenceConstants.PHP_SHOW_HTML_FILES_LOCAL);
32                         // showXMLFilesLocal = ProjectPrefUtil.getPreviewBooleanValue(previewFile,
33                         // 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 (showHTMLFilesLocal && isHTMLFileName) {
38                 // localhostURL = previewFile.getLocation().toString();
39                 // } else if (showXMLFilesLocal && isXMLFileName) {
40                 // localhostURL = previewFile.getLocation().toString();
41                 // } else
42                 if ((localhostURL = ShowExternalPreviewAction.getLocalhostURL(null, previewFile)) == null) {
43                         return new ShowInContextBrowser(previewFile, null, null);
44                 }
45                 localhostURL += postFix;
46                 return new ShowInContextBrowser(previewFile, null, localhostURL);
47         }
48
49         /**
50          * Returns the <code>IShowInTarget</code> for the given part, or
51          * <code>null</code> if it does not provide one.
52          *
53          * @param targetPart
54          *          the target part
55          * @return the <code>IShowInTarget</code> or <code>null</code>
56          */
57         private static IShowInTarget getShowInTarget(IWorkbenchPart targetPart) {
58                 if (targetPart instanceof IShowInTarget) {
59                         return (IShowInTarget) targetPart;
60                 }
61                 Object o = targetPart.getAdapter(IShowInTarget.class);
62                 if (o instanceof IShowInTarget) {
63                         return (IShowInTarget) o;
64                 }
65                 return null;
66         }
67
68         public static void showPreview(IFile previewFile, boolean forceDBGPreview, String postFix) {
69                 if (previewFile == null) {
70                         // should never happen
71                         return;
72                 }
73                 IWorkbenchPage page = WebUI.getActivePage();
74                 if (page != null && page.isEditorAreaVisible()) {
75                         // String extension = previewFile.getFileExtension().toLowerCase();
76                         boolean autoPreview = forceDBGPreview;
77                         // boolean showHTMLFilesLocal = false;
78                         // boolean showXMLFilesLocal = false;
79                         // boolean isHTMLFileName = false;
80                         // boolean isXMLFileName = false;
81                         if (!forceDBGPreview) {
82                                 autoPreview = ProjectPrefUtil.getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
83
84                                 // showHTMLFilesLocal =
85                                 // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
86                                 // IPreferenceConstants.PHP_SHOW_HTML_FILES_LOCAL);
87                                 // showXMLFilesLocal =
88                                 // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
89                                 // IPreferenceConstants.PHP_SHOW_XML_FILES_LOCAL);
90                                 // isHTMLFileName = "html".equals(extension) || "htm".equals(extension)
91                                 // || "xhtml".equals(extension);
92                                 // isXMLFileName = "xml".equals(extension) || "xsd".equals(extension) ||
93                                 // "dtd".equals(extension);
94                         }
95                         if (autoPreview) {
96                                 // String localhostURL;
97                                 // if (showHTMLFilesLocal && isHTMLFileName) {
98                                 // localhostURL = previewFile.getLocation().toString();
99                                 // } else if (showXMLFilesLocal && isXMLFileName) {
100                                 // localhostURL = previewFile.getLocation().toString();
101                                 // } else if ((localhostURL =
102                                 // ShowExternalPreviewAction.getLocalhostURL(null, previewFile)) ==
103                                 // null) {
104                                 // return;
105                                 // }
106                                 // localhostURL += postFix;
107                                 ShowInContext context = getShowInContext(previewFile, forceDBGPreview, postFix);
108                                 IWorkbenchPart sourcePart = page.getActivePart();
109                                 if (sourcePart == null && context != null) {
110                                         return;
111                                 }
112
113                                 // try {
114                                 Perspective persp = ((WorkbenchPage) page).getActivePerspective();
115                                 if (persp != null) {
116
117                                         // If this view is already visible just return.
118                                         IViewReference ref = persp.findView(BrowserView.ID_BROWSER, null);
119                                         IViewPart view = null;
120                                         if (ref != null)
121                                                 view = ref.getView(true);
122                                         IShowInTarget target = getShowInTarget(view);
123                                         if (target != null && target.show(context)) {
124                                                 // success
125                                         }
126                                         ((WorkbenchPage) page).performedShowIn(BrowserView.ID_BROWSER);
127                                 }
128
129                                 // IViewPart view = page.showView(BrowserView.ID_BROWSER);
130                                 // IShowInTarget target = getShowInTarget(view);
131                                 // if (target != null && target.show(new ShowInContext(localhostURL,
132                                 // null))) {
133                                 // // success
134                                 // }
135                                 // ((WorkbenchPage) page).performedShowIn(BrowserView.ID_BROWSER); //
136                                 // TODO: move back up
137                                 // } catch (PartInitException e) {
138                                 // WorkbenchPlugin.log(
139                                 // "Error showing view in ShowInAction.run", e.getStatus());
140                                 // //$NON-NLS-1$
141                                 // }
142
143                                 // try {
144                                 // IViewPart part = page.showView(BrowserView.ID_BROWSER, null,
145                                 // IWorkbenchPage.VIEW_VISIBLE);
146                                 // if (part == null) {
147                                 // part = page.showView(BrowserView.ID_BROWSER);
148                                 // } else {
149                                 // page.bringToTop(part);
150                                 // }
151                                 // ((BrowserView) part).setUrl(localhostURL);
152                                 //
153                                 // } catch (Exception e) {
154                                 // // PHPeclipsePlugin.log(e);
155                                 // }
156                         }
157                 }
158         }
159
160 }