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.internal.WebUIMessages;
9 import net.sourceforge.phpeclipse.ui.overlaypages.ProjectPrefUtil;
10 import net.sourceforge.phpeclipse.webbrowser.views.BrowserView;
11 import net.sourceforge.phpeclipse.webbrowser.views.ShowInContextBrowser;
13 import org.eclipse.core.resources.IFile;
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.core.runtime.Status;
16 import org.eclipse.jface.dialogs.ErrorDialog;
17 import org.eclipse.swt.widgets.Shell;
18 import org.eclipse.ui.IViewPart;
19 import org.eclipse.ui.IViewReference;
20 import org.eclipse.ui.IWorkbenchPage;
21 import org.eclipse.ui.IWorkbenchPart;
22 import org.eclipse.ui.PartInitException;
23 import org.eclipse.ui.PlatformUI;
24 import org.eclipse.ui.browser.IWebBrowser;
25 import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
26 import org.eclipse.ui.internal.Perspective;
27 import org.eclipse.ui.internal.WorkbenchPage;
28 import org.eclipse.ui.part.IShowInTarget;
29 import org.eclipse.ui.part.ShowInContext;
31 public class BrowserUtil {
33 public static ShowInContextBrowser getShowInContext(IFile previewFile,
34 boolean forceDBGPreview, String postFix) {
35 String extension = previewFile.getFileExtension().toLowerCase();
36 // boolean showHTMLFilesLocal = false;
37 // boolean showXMLFilesLocal = false;
38 boolean isHTMLFileName = false;
39 boolean isXMLFileName = false;
41 if (!forceDBGPreview) {
42 // showHTMLFilesLocal =
43 // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
44 // IPreferenceConstants.PHP_SHOW_HTML_FILES_LOCAL);
45 // showXMLFilesLocal =
46 // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
47 // IPreferenceConstants.PHP_SHOW_XML_FILES_LOCAL);
48 isHTMLFileName = "html".equals(extension)
49 || "htm".equals(extension) || "xhtml".equals(extension);
50 isXMLFileName = "xml".equals(extension) || "xsd".equals(extension)
51 || "dtd".equals(extension);
53 // if (showHTMLFilesLocal && isHTMLFileName) {
54 // localhostURL = previewFile.getLocation().toString();
55 // } else if (showXMLFilesLocal && isXMLFileName) {
56 // localhostURL = previewFile.getLocation().toString();
58 if ((localhostURL = ShowExternalPreviewAction.getLocalhostURL(null,
59 previewFile)) == null) {
60 return new ShowInContextBrowser(previewFile, null, null);
62 localhostURL += postFix;
63 return new ShowInContextBrowser(previewFile, null, localhostURL);
67 * Returns the <code>IShowInTarget</code> for the given part, or
68 * <code>null</code> if it does not provide one.
72 * @return the <code>IShowInTarget</code> or <code>null</code>
74 private static IShowInTarget getShowInTarget(IWorkbenchPart targetPart) {
75 if (targetPart instanceof IShowInTarget) {
76 return (IShowInTarget) targetPart;
78 Object o = targetPart.getAdapter(IShowInTarget.class);
79 if (o instanceof IShowInTarget) {
80 return (IShowInTarget) o;
85 public static void showPreview(IFile previewFile, boolean forceDBGPreview,
87 if (previewFile == null) {
88 // should never happen
91 IWorkbenchPage page = WebUI.getActivePage();
92 if (page != null && page.isEditorAreaVisible()) {
93 // String extension = previewFile.getFileExtension().toLowerCase();
94 boolean autoPreview = forceDBGPreview;
95 // boolean showHTMLFilesLocal = false;
96 // boolean showXMLFilesLocal = false;
97 // boolean isHTMLFileName = false;
98 // boolean isXMLFileName = false;
99 if (!forceDBGPreview) {
100 autoPreview = ProjectPrefUtil.getPreviewBooleanValue(
102 IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
104 // showHTMLFilesLocal =
105 // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
106 // IPreferenceConstants.PHP_SHOW_HTML_FILES_LOCAL);
107 // showXMLFilesLocal =
108 // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
109 // IPreferenceConstants.PHP_SHOW_XML_FILES_LOCAL);
110 // isHTMLFileName = "html".equals(extension) ||
111 // "htm".equals(extension)
112 // || "xhtml".equals(extension);
113 // isXMLFileName = "xml".equals(extension) ||
114 // "xsd".equals(extension) ||
115 // "dtd".equals(extension);
118 // String localhostURL;
119 // if (showHTMLFilesLocal && isHTMLFileName) {
120 // localhostURL = previewFile.getLocation().toString();
121 // } else if (showXMLFilesLocal && isXMLFileName) {
122 // localhostURL = previewFile.getLocation().toString();
123 // } else if ((localhostURL =
124 // ShowExternalPreviewAction.getLocalhostURL(null, previewFile))
129 // localhostURL += postFix;
130 ShowInContext context = getShowInContext(previewFile,
131 forceDBGPreview, postFix);
132 IWorkbenchPart sourcePart = page.getActivePart();
133 if (sourcePart == null && context != null) {
138 Perspective persp = ((WorkbenchPage) page)
139 .getActivePerspective();
142 // If this view is already visible just return.
143 IViewReference ref = persp.findView(BrowserView.ID_BROWSER,
145 IViewPart view = null;
147 view = ref.getView(true);
149 if (view == null && forceDBGPreview) {
151 view = persp.showView(BrowserView.ID_BROWSER, null);
152 persp.bringToTop(persp.findView(
153 BrowserView.ID_BROWSER, null));
154 } catch (PartInitException e) {
159 IShowInTarget target = getShowInTarget(view);
160 boolean stickyBrowserURL = ProjectPrefUtil.getPreviewBooleanValue(
162 IPreferenceConstants.PHP_STICKY_BROWSER_URL_DEFAULT);
163 if (target != null) {
165 && ((BrowserView) target).getUrl() != null
166 && ((BrowserView) target).getUrl().length() > 0) {
167 ((BrowserView) target).refresh();
169 target.show(context);
172 ((WorkbenchPage) page)
173 .performedShowIn(BrowserView.ID_BROWSER);
177 // IViewPart view = page.showView(BrowserView.ID_BROWSER);
178 // IShowInTarget target = getShowInTarget(view);
179 // if (target != null && target.show(new
180 // ShowInContext(localhostURL,
185 // page).performedShowIn(BrowserView.ID_BROWSER); //
186 // TODO: move back up
187 // } catch (PartInitException e) {
188 // WorkbenchPlugin.log(
189 // "Error showing view in ShowInAction.run", e.getStatus());
194 // IViewPart part = page.showView(BrowserView.ID_BROWSER, null,
195 // IWorkbenchPage.VIEW_VISIBLE);
196 // if (part == null) {
197 // part = page.showView(BrowserView.ID_BROWSER);
199 // page.bringToTop(part);
201 // ((BrowserView) part).setUrl(localhostURL);
203 // } catch (Exception e) {
204 // // PHPeclipsePlugin.log(e);
210 private static final String BROWSER_ID = "net.sourceforge.phpeclipse.browser";
213 * convenient method to show browser as Editor
216 public static void showBrowserAsEditor(IFile file, String queryString) {
217 showBrowser(IWorkbenchBrowserSupport.AS_EDITOR, file, queryString);
221 * convenient method to show browser as External Web Browser
224 public static void showBrowserAsExternal(IFile file, String queryString) {
225 showBrowser(IWorkbenchBrowserSupport.AS_EXTERNAL, file, queryString);
229 * convenient method to show browser as View
232 public static void showBrowserAsView(IFile file, String queryString) {
233 showBrowser(IWorkbenchBrowserSupport.AS_VIEW, file, queryString);
237 * Show browser according to General settings
239 * See IWorkbenchBrowserSupport and DefaultWorkbenchBrowserSupport.
241 public static void showBrowser(int style, IFile file, String queryString) {
242 ShowInContextBrowser context = getShowInContext(file, true, queryString);
243 String url = context.getLocalhostUrl();
245 String dialogTitle = WebUIMessages
246 .getString("BrowserUtil.error.dialog.title");
247 String message = WebUIMessages
248 .getString("BrowserUtil.null.url.message");
249 String reason = WebUIMessages
250 .getString("BrowserUtil.null.url.reason");
251 IStatus status = new Status(IStatus.ERROR, WebUI.PLUGIN_ID, 0,
253 ErrorDialog.openError(new Shell(), dialogTitle, message, status);
256 String id = BROWSER_ID;
258 case IWorkbenchBrowserSupport.AS_EXTERNAL:
261 case IWorkbenchBrowserSupport.AS_EDITOR:
264 case IWorkbenchBrowserSupport.AS_VIEW:
268 style |= IWorkbenchBrowserSupport.LOCATION_BAR
269 | IWorkbenchBrowserSupport.NAVIGATION_BAR
270 | IWorkbenchBrowserSupport.STATUS;
272 IWebBrowser browser = PlatformUI.getWorkbench().getBrowserSupport()
273 .createBrowser(style, id, null, url);
274 browser.openURL(new URL(url));
276 } catch (PartInitException e) {
278 } catch (MalformedURLException e) {