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.internal.Perspective;
15 import org.eclipse.ui.internal.WorkbenchPage;
16 import org.eclipse.ui.part.IShowInTarget;
17 import org.eclipse.ui.part.ShowInContext;
19 public class BrowserUtil {
21 public static ShowInContextBrowser getShowInContext(IFile previewFile,
22 boolean forceDBGPreview, String postFix) {
23 String extension = previewFile.getFileExtension().toLowerCase();
24 // boolean showHTMLFilesLocal = false;
25 // boolean showXMLFilesLocal = false;
26 boolean isHTMLFileName = false;
27 boolean isXMLFileName = false;
29 if (!forceDBGPreview) {
30 // showHTMLFilesLocal =
31 // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
32 // IPreferenceConstants.PHP_SHOW_HTML_FILES_LOCAL);
33 // showXMLFilesLocal =
34 // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
35 // IPreferenceConstants.PHP_SHOW_XML_FILES_LOCAL);
36 isHTMLFileName = "html".equals(extension)
37 || "htm".equals(extension) || "xhtml".equals(extension);
38 isXMLFileName = "xml".equals(extension) || "xsd".equals(extension)
39 || "dtd".equals(extension);
41 // if (showHTMLFilesLocal && isHTMLFileName) {
42 // localhostURL = previewFile.getLocation().toString();
43 // } else if (showXMLFilesLocal && isXMLFileName) {
44 // localhostURL = previewFile.getLocation().toString();
46 if ((localhostURL = ShowExternalPreviewAction.getLocalhostURL(null,
47 previewFile)) == null) {
48 return new ShowInContextBrowser(previewFile, null, null);
50 localhostURL += postFix;
51 return new ShowInContextBrowser(previewFile, null, localhostURL);
55 * Returns the <code>IShowInTarget</code> for the given part, or
56 * <code>null</code> if it does not provide one.
60 * @return the <code>IShowInTarget</code> or <code>null</code>
62 private static IShowInTarget getShowInTarget(IWorkbenchPart targetPart) {
63 if (targetPart instanceof IShowInTarget) {
64 return (IShowInTarget) targetPart;
66 Object o = targetPart.getAdapter(IShowInTarget.class);
67 if (o instanceof IShowInTarget) {
68 return (IShowInTarget) o;
73 public static void showPreview(IFile previewFile, boolean forceDBGPreview,
75 if (previewFile == null) {
76 // should never happen
79 IWorkbenchPage page = WebUI.getActivePage();
80 if (page != null && page.isEditorAreaVisible()) {
81 // String extension = previewFile.getFileExtension().toLowerCase();
82 boolean autoPreview = forceDBGPreview;
83 // boolean showHTMLFilesLocal = false;
84 // boolean showXMLFilesLocal = false;
85 // boolean isHTMLFileName = false;
86 // boolean isXMLFileName = false;
87 if (!forceDBGPreview) {
88 autoPreview = ProjectPrefUtil.getPreviewBooleanValue(
90 IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
92 // showHTMLFilesLocal =
93 // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
94 // IPreferenceConstants.PHP_SHOW_HTML_FILES_LOCAL);
95 // showXMLFilesLocal =
96 // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
97 // IPreferenceConstants.PHP_SHOW_XML_FILES_LOCAL);
98 // isHTMLFileName = "html".equals(extension) ||
99 // "htm".equals(extension)
100 // || "xhtml".equals(extension);
101 // isXMLFileName = "xml".equals(extension) ||
102 // "xsd".equals(extension) ||
103 // "dtd".equals(extension);
106 // String localhostURL;
107 // if (showHTMLFilesLocal && isHTMLFileName) {
108 // localhostURL = previewFile.getLocation().toString();
109 // } else if (showXMLFilesLocal && isXMLFileName) {
110 // localhostURL = previewFile.getLocation().toString();
111 // } else if ((localhostURL =
112 // ShowExternalPreviewAction.getLocalhostURL(null, previewFile))
117 // localhostURL += postFix;
118 ShowInContext context = getShowInContext(previewFile,
119 forceDBGPreview, postFix);
120 IWorkbenchPart sourcePart = page.getActivePart();
121 if (sourcePart == null && context != null) {
126 Perspective persp = ((WorkbenchPage) page)
127 .getActivePerspective();
130 // If this view is already visible just return.
131 IViewReference ref = persp.findView(BrowserView.ID_BROWSER,
133 IViewPart view = null;
135 view = ref.getView(true);
138 IShowInTarget target = getShowInTarget(view);
139 if (target != null && target.show(context)) {
142 ((WorkbenchPage) page)
143 .performedShowIn(BrowserView.ID_BROWSER);
147 // IViewPart view = page.showView(BrowserView.ID_BROWSER);
148 // IShowInTarget target = getShowInTarget(view);
149 // if (target != null && target.show(new
150 // ShowInContext(localhostURL,
155 // page).performedShowIn(BrowserView.ID_BROWSER); //
156 // TODO: move back up
157 // } catch (PartInitException e) {
158 // WorkbenchPlugin.log(
159 // "Error showing view in ShowInAction.run", e.getStatus());
164 // IViewPart part = page.showView(BrowserView.ID_BROWSER, null,
165 // IWorkbenchPage.VIEW_VISIBLE);
166 // if (part == null) {
167 // part = page.showView(BrowserView.ID_BROWSER);
169 // page.bringToTop(part);
171 // ((BrowserView) part).setUrl(localhostURL);
173 // } catch (Exception e) {
174 // // PHPeclipsePlugin.log(e);