1596135 - debugger: open browser when debugging starts
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / ui / editor / BrowserUtil.java
1 package net.sourceforge.phpeclipse.ui.editor;
2
3 import java.net.MalformedURLException;
4 import java.net.URL;
5
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;
11
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;
25
26 public class BrowserUtil {
27
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;
35                 String localhostURL;
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);
47                 }
48                 // if (showHTMLFilesLocal && isHTMLFileName) {
49                 // localhostURL = previewFile.getLocation().toString();
50                 // } else if (showXMLFilesLocal && isXMLFileName) {
51                 // localhostURL = previewFile.getLocation().toString();
52                 // } else
53                 if ((localhostURL = ShowExternalPreviewAction.getLocalhostURL(null,
54                                 previewFile)) == null) {
55                         return new ShowInContextBrowser(previewFile, null, null);
56                 }
57                 localhostURL += postFix;
58                 return new ShowInContextBrowser(previewFile, null, localhostURL);
59         }
60
61         /**
62          * Returns the <code>IShowInTarget</code> for the given part, or
63          * <code>null</code> if it does not provide one.
64          * 
65          * @param targetPart
66          *            the target part
67          * @return the <code>IShowInTarget</code> or <code>null</code>
68          */
69         private static IShowInTarget getShowInTarget(IWorkbenchPart targetPart) {
70                 if (targetPart instanceof IShowInTarget) {
71                         return (IShowInTarget) targetPart;
72                 }
73                 Object o = targetPart.getAdapter(IShowInTarget.class);
74                 if (o instanceof IShowInTarget) {
75                         return (IShowInTarget) o;
76                 }
77                 return null;
78         }
79
80         public static void showPreview(IFile previewFile, boolean forceDBGPreview,
81                         String postFix) {
82                 if (previewFile == null) {
83                         // should never happen
84                         return;
85                 }
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(
96                                                 previewFile,
97                                                 IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
98
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);
111                         }
112                         if (autoPreview) {
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))
120                                 // ==
121                                 // null) {
122                                 // return;
123                                 // }
124                                 // localhostURL += postFix;
125                                 ShowInContext context = getShowInContext(previewFile,
126                                                 forceDBGPreview, postFix);
127                                 IWorkbenchPart sourcePart = page.getActivePart();
128                                 if (sourcePart == null && context != null) {
129                                         return;
130                                 }
131
132                                 // try {
133                                 Perspective persp = ((WorkbenchPage) page)
134                                                 .getActivePerspective();
135                                 if (persp != null) {
136
137                                         // If this view is already visible just return.
138                                         IViewReference ref = persp.findView(BrowserView.ID_BROWSER,
139                                                         null);
140                                         IViewPart view = null;
141                                         if (ref != null) {
142                                                 view = ref.getView(true);
143                                         }
144                                         if (view == null && forceDBGPreview) {
145                                                 try {
146                                                         view = persp.showView(BrowserView.ID_BROWSER, null);
147                                                         persp.bringToTop(persp.findView(
148                                                                         BrowserView.ID_BROWSER, null));
149                                                 } catch (PartInitException e) {
150                                                         WebUI.log(e);
151                                                 }
152                                         }
153                                         if (view != null) {
154                                                 IShowInTarget target = getShowInTarget(view);
155                                                 if (target != null && target.show(context)) {
156                                                         // success
157                                                 }
158                                                 ((WorkbenchPage) page)
159                                                                 .performedShowIn(BrowserView.ID_BROWSER);
160                                         }
161                                 }
162
163                                 // IViewPart view = page.showView(BrowserView.ID_BROWSER);
164                                 // IShowInTarget target = getShowInTarget(view);
165                                 // if (target != null && target.show(new
166                                 // ShowInContext(localhostURL,
167                                 // null))) {
168                                 // // success
169                                 // }
170                                 // ((WorkbenchPage)
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());
176                                 // //$NON-NLS-1$
177                                 // }
178
179                                 // try {
180                                 // IViewPart part = page.showView(BrowserView.ID_BROWSER, null,
181                                 // IWorkbenchPage.VIEW_VISIBLE);
182                                 // if (part == null) {
183                                 // part = page.showView(BrowserView.ID_BROWSER);
184                                 // } else {
185                                 // page.bringToTop(part);
186                                 // }
187                                 // ((BrowserView) part).setUrl(localhostURL);
188                                 //
189                                 // } catch (Exception e) {
190                                 // // PHPeclipsePlugin.log(e);
191                                 // }
192                         }
193                 }
194         }
195
196         private static final String BROWSER_ID = "net.sourceforge.phpeclipse.browser";
197
198         /**
199          * convenient method to show browser as Editor
200          * 
201          */
202         public static void showBrowserAsEditor(IFile file, String queryString) {
203                 showBrowser(IWorkbenchBrowserSupport.AS_EDITOR, file, queryString);
204         }
205
206         /**
207          * convenient method to show browser as External Web Browser
208          * 
209          */
210         public static void showBrowserAsExternal(IFile file, String queryString) {
211                 showBrowser(IWorkbenchBrowserSupport.AS_EXTERNAL, file, queryString);
212         }
213
214         /**
215          * convenient method to show browser as View
216          * 
217          */
218         public static void showBrowserAsView(IFile file, String queryString) {
219                 showBrowser(IWorkbenchBrowserSupport.AS_VIEW, file, queryString);
220         }
221
222         /**
223          * Show browser according to General settings
224          * 
225          * See IWorkbenchBrowserSupport and DefaultWorkbenchBrowserSupport.
226          */
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;
231                 switch (style) {
232                 case IWorkbenchBrowserSupport.AS_EXTERNAL:
233                         id += ".x";
234                         break;
235                 case IWorkbenchBrowserSupport.AS_EDITOR:
236                         id += ".e";
237                         break;
238                 case IWorkbenchBrowserSupport.AS_VIEW:
239                         id += ".v";
240                         break;
241                 }
242                 style |= IWorkbenchBrowserSupport.LOCATION_BAR
243                                 | IWorkbenchBrowserSupport.NAVIGATION_BAR
244                                 | IWorkbenchBrowserSupport.STATUS;
245                 try {
246                         IWebBrowser browser = PlatformUI.getWorkbench().getBrowserSupport()
247                                         .createBrowser(style, id, null, url);
248                         browser.openURL(new URL(url));
249
250                 } catch (PartInitException e) {
251                         WebUI.log(e);
252                 } catch (MalformedURLException e) {
253                         WebUI.log(e);
254                 }
255         }
256
257 }