1) Readded the lost BreakpointImageProvider.java
[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.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;
12
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;
30
31 public class BrowserUtil {
32
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;
40                 String localhostURL;
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);
52                 }
53                 // if (showHTMLFilesLocal && isHTMLFileName) {
54                 // localhostURL = previewFile.getLocation().toString();
55                 // } else if (showXMLFilesLocal && isXMLFileName) {
56                 // localhostURL = previewFile.getLocation().toString();
57                 // } else
58                 if ((localhostURL = ShowExternalPreviewAction.getLocalhostURL(null,
59                                 previewFile)) == null) {
60                         return new ShowInContextBrowser(previewFile, null, null);
61                 }
62                 localhostURL += postFix;
63                 return new ShowInContextBrowser(previewFile, null, localhostURL);
64         }
65
66         /**
67          * Returns the <code>IShowInTarget</code> for the given part, or
68          * <code>null</code> if it does not provide one.
69          * 
70          * @param targetPart
71          *            the target part
72          * @return the <code>IShowInTarget</code> or <code>null</code>
73          */
74         private static IShowInTarget getShowInTarget(IWorkbenchPart targetPart) {
75                 if (targetPart instanceof IShowInTarget) {
76                         return (IShowInTarget) targetPart;
77                 }
78                 Object o = targetPart.getAdapter(IShowInTarget.class);
79                 if (o instanceof IShowInTarget) {
80                         return (IShowInTarget) o;
81                 }
82                 return null;
83         }
84
85         public static void showPreview(IFile previewFile, boolean forceDBGPreview,
86                         String postFix) {
87                 if (previewFile == null) {
88                         // should never happen
89                         return;
90                 }
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(
101                                                 previewFile,
102                                                 IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
103
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);
116                         }
117                         if (autoPreview) {
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))
125                                 // ==
126                                 // null) {
127                                 // return;
128                                 // }
129                                 // localhostURL += postFix;
130                                 ShowInContext context = getShowInContext (previewFile, forceDBGPreview, postFix);
131                                 IWorkbenchPart sourcePart = page.getActivePart();
132                                 
133                                 if (sourcePart == null && context != null) {
134                                         return;
135                                 }
136
137                                 // try {
138 /* Don't know what this is for, but I removed it by now because of some compile errors!                 
139                                 Perspective persp = ((WorkbenchPage) page).getActivePerspective();
140                                 if (persp != null) {
141                                         // If this view is already visible just return.
142                                         IViewReference ref = persp.findView (BrowserView.ID_BROWSER, null);
143                                         IViewPart view = null;
144                                         
145                                         if (ref != null) {
146                                                 view = ref.getView(true);
147                                         }
148                                         
149                                         if (view == null && forceDBGPreview) {
150                                                 try {
151                                                         view = persp.showView (BrowserView.ID_BROWSER, null);
152                                                         persp.bringToTop (persp.findView (BrowserView.ID_BROWSER, null));
153                                                 } catch (PartInitException e) {
154                                                         WebUI.log(e);
155                                                 }
156                                         }
157                                         
158                                         if (view != null) {
159                                                 IShowInTarget target = getShowInTarget(view);
160                                                 boolean stickyBrowserURL = ProjectPrefUtil.getPreviewBooleanValue(
161                                                                 previewFile,
162                                                                 IPreferenceConstants.PHP_STICKY_BROWSER_URL_DEFAULT);
163                                                 if (target != null) {
164                                                         if (stickyBrowserURL
165                                                                         && ((BrowserView) target).getUrl() != null
166                                                                         && ((BrowserView) target).getUrl().length() > 0) {
167                                                                 ((BrowserView) target).refresh();
168                                                         } else {
169                                                                 target.show(context);
170                                                         }
171                                                 }
172                                                 ((WorkbenchPage) page).performedShowIn (BrowserView.ID_BROWSER);
173                                         }
174                                 }
175 */
176                                 // IViewPart view = page.showView(BrowserView.ID_BROWSER);
177                                 // IShowInTarget target = getShowInTarget(view);
178                                 // if (target != null && target.show(new
179                                 // ShowInContext(localhostURL,
180                                 // null))) {
181                                 // // success
182                                 // }
183                                 // ((WorkbenchPage)
184                                 // page).performedShowIn(BrowserView.ID_BROWSER); //
185                                 // TODO: move back up
186                                 // } catch (PartInitException e) {
187                                 // WorkbenchPlugin.log(
188                                 // "Error showing view in ShowInAction.run", e.getStatus());
189                                 // //$NON-NLS-1$
190                                 // }
191
192                                 // try {
193                                 // IViewPart part = page.showView(BrowserView.ID_BROWSER, null,
194                                 // IWorkbenchPage.VIEW_VISIBLE);
195                                 // if (part == null) {
196                                 // part = page.showView(BrowserView.ID_BROWSER);
197                                 // } else {
198                                 // page.bringToTop(part);
199                                 // }
200                                 // ((BrowserView) part).setUrl(localhostURL);
201                                 //
202                                 // } catch (Exception e) {
203                                 // // PHPeclipsePlugin.log(e);
204                                 // }
205                         }
206                 }
207         }
208
209         private static final String BROWSER_ID = "net.sourceforge.phpeclipse.browser";
210
211         /**
212          * convenient method to show browser as Editor
213          * 
214          */
215         public static void showBrowserAsEditor(IFile file, String queryString) {
216                 showBrowser(IWorkbenchBrowserSupport.AS_EDITOR, file, queryString);
217         }
218
219         /**
220          * convenient method to show browser as External Web Browser
221          * 
222          */
223         public static void showBrowserAsExternal(IFile file, String queryString) {
224                 showBrowser(IWorkbenchBrowserSupport.AS_EXTERNAL, file, queryString);
225         }
226
227         /**
228          * convenient method to show browser as View
229          * 
230          */
231         public static void showBrowserAsView(IFile file, String queryString) {
232                 showBrowser(IWorkbenchBrowserSupport.AS_VIEW, file, queryString);
233         }
234
235         /**
236          * Show browser according to General settings
237          * 
238          * See IWorkbenchBrowserSupport and DefaultWorkbenchBrowserSupport.
239          */
240         public static void showBrowser(int style, IFile file, String queryString) {
241                 ShowInContextBrowser context = getShowInContext(file, true, queryString);
242                 String url = context.getLocalhostUrl();
243                 if (url == null) {
244                         String dialogTitle = WebUIMessages
245                                         .getString("BrowserUtil.error.dialog.title");
246                         String message = WebUIMessages
247                                         .getString("BrowserUtil.null.url.message");
248                         String reason = WebUIMessages
249                                         .getString("BrowserUtil.null.url.reason");
250                         IStatus status = new Status(IStatus.ERROR, WebUI.PLUGIN_ID, 0,
251                                         reason, null);
252                         ErrorDialog.openError(new Shell(), dialogTitle, message, status);
253                         return;
254                 }
255                 String id = BROWSER_ID;
256                 switch (style) {
257                 case IWorkbenchBrowserSupport.AS_EXTERNAL:
258                         id += ".x";
259                         break;
260                 case IWorkbenchBrowserSupport.AS_EDITOR:
261                         id += ".e";
262                         break;
263                 case IWorkbenchBrowserSupport.AS_VIEW:
264                         id += ".v";
265                         break;
266                 }
267                 style |= IWorkbenchBrowserSupport.LOCATION_BAR
268                                 | IWorkbenchBrowserSupport.NAVIGATION_BAR
269                                 | IWorkbenchBrowserSupport.STATUS;
270                 try {
271                         IWebBrowser browser = PlatformUI.getWorkbench().getBrowserSupport()
272                                         .createBrowser(style, id, null, url);
273                         browser.openURL(new URL(url));
274
275                 } catch (PartInitException e) {
276                         WebUI.log(e);
277                 } catch (MalformedURLException e) {
278                         WebUI.log(e);
279                 }
280         }
281
282 }