1 package net.sourceforge.phpeclipse.ui.editor;
3 import java.net.MalformedURLException;
6 import net.sourceforge.phpeclipse.ui.IPreferenceConstants;
7 import net.sourceforge.phpeclipse.ui.WebUI;
8 import net.sourceforge.phpeclipse.ui.overlaypages.ProjectPrefUtil;
9 import net.sourceforge.phpeclipse.webbrowser.views.BrowserView;
10 import net.sourceforge.phpeclipse.webbrowser.views.ShowInContextBrowser;
12 import org.eclipse.core.resources.IFile;
13 import org.eclipse.ui.IViewPart;
14 import org.eclipse.ui.IViewReference;
15 import org.eclipse.ui.IWorkbenchPage;
16 import org.eclipse.ui.IWorkbenchPart;
17 import org.eclipse.ui.PartInitException;
18 import org.eclipse.ui.PlatformUI;
19 import org.eclipse.ui.browser.IWebBrowser;
20 import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
21 import org.eclipse.ui.internal.Perspective;
22 import org.eclipse.ui.internal.WorkbenchPage;
23 import org.eclipse.ui.part.IShowInTarget;
24 import org.eclipse.ui.part.ShowInContext;
26 public class BrowserUtil {
28 public static ShowInContextBrowser getShowInContext(IFile previewFile,
29 boolean forceDBGPreview, String postFix) {
30 String extension = previewFile.getFileExtension().toLowerCase();
31 // boolean showHTMLFilesLocal = false;
32 // boolean showXMLFilesLocal = false;
33 boolean isHTMLFileName = false;
34 boolean isXMLFileName = false;
36 if (!forceDBGPreview) {
37 // showHTMLFilesLocal =
38 // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
39 // IPreferenceConstants.PHP_SHOW_HTML_FILES_LOCAL);
40 // showXMLFilesLocal =
41 // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
42 // IPreferenceConstants.PHP_SHOW_XML_FILES_LOCAL);
43 isHTMLFileName = "html".equals(extension)
44 || "htm".equals(extension) || "xhtml".equals(extension);
45 isXMLFileName = "xml".equals(extension) || "xsd".equals(extension)
46 || "dtd".equals(extension);
48 // if (showHTMLFilesLocal && isHTMLFileName) {
49 // localhostURL = previewFile.getLocation().toString();
50 // } else if (showXMLFilesLocal && isXMLFileName) {
51 // localhostURL = previewFile.getLocation().toString();
53 if ((localhostURL = ShowExternalPreviewAction.getLocalhostURL(null,
54 previewFile)) == null) {
55 return new ShowInContextBrowser(previewFile, null, null);
57 localhostURL += postFix;
58 return new ShowInContextBrowser(previewFile, null, localhostURL);
62 * Returns the <code>IShowInTarget</code> for the given part, or
63 * <code>null</code> if it does not provide one.
67 * @return the <code>IShowInTarget</code> or <code>null</code>
69 private static IShowInTarget getShowInTarget(IWorkbenchPart targetPart) {
70 if (targetPart instanceof IShowInTarget) {
71 return (IShowInTarget) targetPart;
73 Object o = targetPart.getAdapter(IShowInTarget.class);
74 if (o instanceof IShowInTarget) {
75 return (IShowInTarget) o;
80 public static void showPreview(IFile previewFile, boolean forceDBGPreview,
82 if (previewFile == null) {
83 // should never happen
86 IWorkbenchPage page = WebUI.getActivePage();
87 if (page != null && page.isEditorAreaVisible()) {
88 // String extension = previewFile.getFileExtension().toLowerCase();
89 boolean autoPreview = forceDBGPreview;
90 // boolean showHTMLFilesLocal = false;
91 // boolean showXMLFilesLocal = false;
92 // boolean isHTMLFileName = false;
93 // boolean isXMLFileName = false;
94 if (!forceDBGPreview) {
95 autoPreview = ProjectPrefUtil.getPreviewBooleanValue(
97 IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
99 // showHTMLFilesLocal =
100 // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
101 // IPreferenceConstants.PHP_SHOW_HTML_FILES_LOCAL);
102 // showXMLFilesLocal =
103 // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
104 // IPreferenceConstants.PHP_SHOW_XML_FILES_LOCAL);
105 // isHTMLFileName = "html".equals(extension) ||
106 // "htm".equals(extension)
107 // || "xhtml".equals(extension);
108 // isXMLFileName = "xml".equals(extension) ||
109 // "xsd".equals(extension) ||
110 // "dtd".equals(extension);
113 // String localhostURL;
114 // if (showHTMLFilesLocal && isHTMLFileName) {
115 // localhostURL = previewFile.getLocation().toString();
116 // } else if (showXMLFilesLocal && isXMLFileName) {
117 // localhostURL = previewFile.getLocation().toString();
118 // } else if ((localhostURL =
119 // ShowExternalPreviewAction.getLocalhostURL(null, previewFile))
124 // localhostURL += postFix;
125 ShowInContext context = getShowInContext(previewFile,
126 forceDBGPreview, postFix);
127 IWorkbenchPart sourcePart = page.getActivePart();
128 if (sourcePart == null && context != null) {
133 Perspective persp = ((WorkbenchPage) page)
134 .getActivePerspective();
137 // If this view is already visible just return.
138 IViewReference ref = persp.findView(BrowserView.ID_BROWSER,
140 IViewPart view = null;
142 view = ref.getView(true);
144 if (view == null && forceDBGPreview) {
146 view = persp.showView(BrowserView.ID_BROWSER, null);
147 persp.bringToTop(persp.findView(
148 BrowserView.ID_BROWSER, null));
149 } catch (PartInitException e) {
154 IShowInTarget target = getShowInTarget(view);
155 if (target != null && target.show(context)) {
158 ((WorkbenchPage) page)
159 .performedShowIn(BrowserView.ID_BROWSER);
163 // IViewPart view = page.showView(BrowserView.ID_BROWSER);
164 // IShowInTarget target = getShowInTarget(view);
165 // if (target != null && target.show(new
166 // ShowInContext(localhostURL,
171 // page).performedShowIn(BrowserView.ID_BROWSER); //
172 // TODO: move back up
173 // } catch (PartInitException e) {
174 // WorkbenchPlugin.log(
175 // "Error showing view in ShowInAction.run", e.getStatus());
180 // IViewPart part = page.showView(BrowserView.ID_BROWSER, null,
181 // IWorkbenchPage.VIEW_VISIBLE);
182 // if (part == null) {
183 // part = page.showView(BrowserView.ID_BROWSER);
185 // page.bringToTop(part);
187 // ((BrowserView) part).setUrl(localhostURL);
189 // } catch (Exception e) {
190 // // PHPeclipsePlugin.log(e);
196 private static final String BROWSER_ID = "net.sourceforge.phpeclipse.browser";
199 * convenient method to show browser as Editor
202 public static void showBrowserAsEditor(IFile file, String queryString) {
203 showBrowser(IWorkbenchBrowserSupport.AS_EDITOR, file, queryString);
207 * convenient method to show browser as External Web Browser
210 public static void showBrowserAsExternal(IFile file, String queryString) {
211 showBrowser(IWorkbenchBrowserSupport.AS_EXTERNAL, file, queryString);
215 * convenient method to show browser as View
218 public static void showBrowserAsView(IFile file, String queryString) {
219 showBrowser(IWorkbenchBrowserSupport.AS_VIEW, file, queryString);
223 * Show browser according to General settings
225 * See IWorkbenchBrowserSupport and DefaultWorkbenchBrowserSupport.
227 public static void showBrowser(int style, IFile file, String queryString) {
228 ShowInContextBrowser context = getShowInContext(file, true, queryString);
229 String url = context.getLocalhostUrl();
230 String id = BROWSER_ID;
232 case IWorkbenchBrowserSupport.AS_EXTERNAL:
235 case IWorkbenchBrowserSupport.AS_EDITOR:
238 case IWorkbenchBrowserSupport.AS_VIEW:
242 style |= IWorkbenchBrowserSupport.LOCATION_BAR
243 | IWorkbenchBrowserSupport.NAVIGATION_BAR
244 | IWorkbenchBrowserSupport.STATUS;
246 IWebBrowser browser = PlatformUI.getWorkbench().getBrowserSupport()
247 .createBrowser(style, id, null, url);
248 browser.openURL(new URL(url));
250 } catch (PartInitException e) {
252 } catch (MalformedURLException e) {