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, 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;
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);
37 // if (showHTMLFilesLocal && isHTMLFileName) {
38 // localhostURL = previewFile.getLocation().toString();
39 // } else if (showXMLFilesLocal && isXMLFileName) {
40 // localhostURL = previewFile.getLocation().toString();
42 if ((localhostURL = ShowExternalPreviewAction.getLocalhostURL(null, previewFile)) == null) {
43 return new ShowInContextBrowser(previewFile, null, null);
45 localhostURL += postFix;
46 return new ShowInContextBrowser(previewFile, null, localhostURL);
50 * Returns the <code>IShowInTarget</code> for the given part, or
51 * <code>null</code> if it does not provide one.
55 * @return the <code>IShowInTarget</code> or <code>null</code>
57 private static IShowInTarget getShowInTarget(IWorkbenchPart targetPart) {
58 if (targetPart instanceof IShowInTarget) {
59 return (IShowInTarget) targetPart;
61 Object o = targetPart.getAdapter(IShowInTarget.class);
62 if (o instanceof IShowInTarget) {
63 return (IShowInTarget) o;
68 public static void showPreview(IFile previewFile, boolean forceDBGPreview, String postFix) {
69 if (previewFile == null) {
70 // should never happen
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);
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);
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)) ==
106 // localhostURL += postFix;
107 ShowInContext context = getShowInContext(previewFile, forceDBGPreview, postFix);
108 IWorkbenchPart sourcePart = page.getActivePart();
109 if (sourcePart == null && context != null) {
114 Perspective persp = ((WorkbenchPage) page).getActivePerspective();
117 // If this view is already visible just return.
118 IViewReference ref = persp.findView(BrowserView.ID_BROWSER, null);
119 IViewPart view = null;
121 view = ref.getView(true);
124 IShowInTarget target = getShowInTarget(view);
125 if (target != null && target.show(context)) {
128 ((WorkbenchPage) page).performedShowIn(BrowserView.ID_BROWSER);
132 // IViewPart view = page.showView(BrowserView.ID_BROWSER);
133 // IShowInTarget target = getShowInTarget(view);
134 // if (target != null && target.show(new ShowInContext(localhostURL,
138 // ((WorkbenchPage) page).performedShowIn(BrowserView.ID_BROWSER); //
139 // TODO: move back up
140 // } catch (PartInitException e) {
141 // WorkbenchPlugin.log(
142 // "Error showing view in ShowInAction.run", e.getStatus());
147 // IViewPart part = page.showView(BrowserView.ID_BROWSER, null,
148 // IWorkbenchPage.VIEW_VISIBLE);
149 // if (part == null) {
150 // part = page.showView(BrowserView.ID_BROWSER);
152 // page.bringToTop(part);
154 // ((BrowserView) part).setUrl(localhostURL);
156 // } catch (Exception e) {
157 // // PHPeclipsePlugin.log(e);