1 package net.sourceforge.phpeclipse.ui.editor;
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;
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.PartInitException;
15 import org.eclipse.ui.internal.Perspective;
16 import org.eclipse.ui.internal.WorkbenchPage;
17 import org.eclipse.ui.part.IShowInTarget;
18 import org.eclipse.ui.part.ShowInContext;
20 public class BrowserUtil {
22 public static ShowInContextBrowser getShowInContext(IFile previewFile,
23 boolean forceDBGPreview, String postFix) {
24 String extension = previewFile.getFileExtension().toLowerCase();
25 // boolean showHTMLFilesLocal = false;
26 // boolean showXMLFilesLocal = false;
27 boolean isHTMLFileName = false;
28 boolean isXMLFileName = false;
30 if (!forceDBGPreview) {
31 // showHTMLFilesLocal =
32 // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
33 // IPreferenceConstants.PHP_SHOW_HTML_FILES_LOCAL);
34 // showXMLFilesLocal =
35 // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
36 // IPreferenceConstants.PHP_SHOW_XML_FILES_LOCAL);
37 isHTMLFileName = "html".equals(extension)
38 || "htm".equals(extension) || "xhtml".equals(extension);
39 isXMLFileName = "xml".equals(extension) || "xsd".equals(extension)
40 || "dtd".equals(extension);
42 // if (showHTMLFilesLocal && isHTMLFileName) {
43 // localhostURL = previewFile.getLocation().toString();
44 // } else if (showXMLFilesLocal && isXMLFileName) {
45 // localhostURL = previewFile.getLocation().toString();
47 if ((localhostURL = ShowExternalPreviewAction.getLocalhostURL(null,
48 previewFile)) == null) {
49 return new ShowInContextBrowser(previewFile, null, null);
51 localhostURL += postFix;
52 return new ShowInContextBrowser(previewFile, null, localhostURL);
56 * Returns the <code>IShowInTarget</code> for the given part, or
57 * <code>null</code> if it does not provide one.
61 * @return the <code>IShowInTarget</code> or <code>null</code>
63 private static IShowInTarget getShowInTarget(IWorkbenchPart targetPart) {
64 if (targetPart instanceof IShowInTarget) {
65 return (IShowInTarget) targetPart;
67 Object o = targetPart.getAdapter(IShowInTarget.class);
68 if (o instanceof IShowInTarget) {
69 return (IShowInTarget) o;
74 public static void showPreview(IFile previewFile, boolean forceDBGPreview,
76 if (previewFile == null) {
77 // should never happen
80 IWorkbenchPage page = WebUI.getActivePage();
81 if (page != null && page.isEditorAreaVisible()) {
82 // String extension = previewFile.getFileExtension().toLowerCase();
83 boolean autoPreview = forceDBGPreview;
84 // boolean showHTMLFilesLocal = false;
85 // boolean showXMLFilesLocal = false;
86 // boolean isHTMLFileName = false;
87 // boolean isXMLFileName = false;
88 if (!forceDBGPreview) {
89 autoPreview = ProjectPrefUtil.getPreviewBooleanValue(
91 IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
93 // showHTMLFilesLocal =
94 // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
95 // IPreferenceConstants.PHP_SHOW_HTML_FILES_LOCAL);
96 // showXMLFilesLocal =
97 // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
98 // IPreferenceConstants.PHP_SHOW_XML_FILES_LOCAL);
99 // isHTMLFileName = "html".equals(extension) ||
100 // "htm".equals(extension)
101 // || "xhtml".equals(extension);
102 // isXMLFileName = "xml".equals(extension) ||
103 // "xsd".equals(extension) ||
104 // "dtd".equals(extension);
107 // String localhostURL;
108 // if (showHTMLFilesLocal && isHTMLFileName) {
109 // localhostURL = previewFile.getLocation().toString();
110 // } else if (showXMLFilesLocal && isXMLFileName) {
111 // localhostURL = previewFile.getLocation().toString();
112 // } else if ((localhostURL =
113 // ShowExternalPreviewAction.getLocalhostURL(null, previewFile))
118 // localhostURL += postFix;
119 ShowInContext context = getShowInContext(previewFile,
120 forceDBGPreview, postFix);
121 IWorkbenchPart sourcePart = page.getActivePart();
122 if (sourcePart == null && context != null) {
127 Perspective persp = ((WorkbenchPage) page)
128 .getActivePerspective();
131 // If this view is already visible just return.
132 IViewReference ref = persp.findView(BrowserView.ID_BROWSER,
134 IViewPart view = null;
136 view = ref.getView(true);
138 if (view == null && forceDBGPreview) {
140 view = persp.showView(BrowserView.ID_BROWSER, null);
141 persp.bringToTop(persp.findView(
142 BrowserView.ID_BROWSER, null));
143 } catch (PartInitException e) {
148 IShowInTarget target = getShowInTarget(view);
149 if (target != null && target.show(context)) {
152 ((WorkbenchPage) page)
153 .performedShowIn(BrowserView.ID_BROWSER);
157 // IViewPart view = page.showView(BrowserView.ID_BROWSER);
158 // IShowInTarget target = getShowInTarget(view);
159 // if (target != null && target.show(new
160 // ShowInContext(localhostURL,
165 // page).performedShowIn(BrowserView.ID_BROWSER); //
166 // TODO: move back up
167 // } catch (PartInitException e) {
168 // WorkbenchPlugin.log(
169 // "Error showing view in ShowInAction.run", e.getStatus());
174 // IViewPart part = page.showView(BrowserView.ID_BROWSER, null,
175 // IWorkbenchPage.VIEW_VISIBLE);
176 // if (part == null) {
177 // part = page.showView(BrowserView.ID_BROWSER);
179 // page.bringToTop(part);
181 // ((BrowserView) part).setUrl(localhostURL);
183 // } catch (Exception e) {
184 // // PHPeclipsePlugin.log(e);