Fixed: 1777185 - Remote Debug option 'Open with ...' should work
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / ui / editor / BrowserUtil.java
1 package net.sourceforge.phpeclipse.ui.editor;
2
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;
8
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.PartInitException;
15 import org.eclipse.ui.internal.Perspective;
16 import org.eclipse.ui.internal.WorkbenchPage;
17 import org.eclipse.ui.part.IShowInTarget;
18 import org.eclipse.ui.part.ShowInContext;
19
20 public class BrowserUtil {
21
22         public static ShowInContextBrowser getShowInContext(IFile previewFile,
23                         boolean forceDBGPreview, String postFix) {
24                 String extension = previewFile.getFileExtension().toLowerCase();
25                 // boolean showHTMLFilesLocal = false;
26                 // boolean showXMLFilesLocal = false;
27                 boolean isHTMLFileName = false;
28                 boolean isXMLFileName = false;
29                 String localhostURL;
30                 if (!forceDBGPreview) {
31                         // showHTMLFilesLocal =
32                         // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
33                         // IPreferenceConstants.PHP_SHOW_HTML_FILES_LOCAL);
34                         // showXMLFilesLocal =
35                         // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
36                         // IPreferenceConstants.PHP_SHOW_XML_FILES_LOCAL);
37                         isHTMLFileName = "html".equals(extension)
38                                         || "htm".equals(extension) || "xhtml".equals(extension);
39                         isXMLFileName = "xml".equals(extension) || "xsd".equals(extension)
40                                         || "dtd".equals(extension);
41                 }
42                 // if (showHTMLFilesLocal && isHTMLFileName) {
43                 // localhostURL = previewFile.getLocation().toString();
44                 // } else if (showXMLFilesLocal && isXMLFileName) {
45                 // localhostURL = previewFile.getLocation().toString();
46                 // } else
47                 if ((localhostURL = ShowExternalPreviewAction.getLocalhostURL(null,
48                                 previewFile)) == null) {
49                         return new ShowInContextBrowser(previewFile, null, null);
50                 }
51                 localhostURL += postFix;
52                 return new ShowInContextBrowser(previewFile, null, localhostURL);
53         }
54
55         /**
56          * Returns the <code>IShowInTarget</code> for the given part, or
57          * <code>null</code> if it does not provide one.
58          * 
59          * @param targetPart
60          *            the target part
61          * @return the <code>IShowInTarget</code> or <code>null</code>
62          */
63         private static IShowInTarget getShowInTarget(IWorkbenchPart targetPart) {
64                 if (targetPart instanceof IShowInTarget) {
65                         return (IShowInTarget) targetPart;
66                 }
67                 Object o = targetPart.getAdapter(IShowInTarget.class);
68                 if (o instanceof IShowInTarget) {
69                         return (IShowInTarget) o;
70                 }
71                 return null;
72         }
73
74         public static void showPreview(IFile previewFile, boolean forceDBGPreview,
75                         String postFix) {
76                 if (previewFile == null) {
77                         // should never happen
78                         return;
79                 }
80                 IWorkbenchPage page = WebUI.getActivePage();
81                 if (page != null && page.isEditorAreaVisible()) {
82                         // String extension = previewFile.getFileExtension().toLowerCase();
83                         boolean autoPreview = forceDBGPreview;
84                         // boolean showHTMLFilesLocal = false;
85                         // boolean showXMLFilesLocal = false;
86                         // boolean isHTMLFileName = false;
87                         // boolean isXMLFileName = false;
88                         if (!forceDBGPreview) {
89                                 autoPreview = ProjectPrefUtil.getPreviewBooleanValue(
90                                                 previewFile,
91                                                 IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
92
93                                 // showHTMLFilesLocal =
94                                 // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
95                                 // IPreferenceConstants.PHP_SHOW_HTML_FILES_LOCAL);
96                                 // showXMLFilesLocal =
97                                 // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
98                                 // IPreferenceConstants.PHP_SHOW_XML_FILES_LOCAL);
99                                 // isHTMLFileName = "html".equals(extension) ||
100                                 // "htm".equals(extension)
101                                 // || "xhtml".equals(extension);
102                                 // isXMLFileName = "xml".equals(extension) ||
103                                 // "xsd".equals(extension) ||
104                                 // "dtd".equals(extension);
105                         }
106                         if (autoPreview) {
107                                 // String localhostURL;
108                                 // if (showHTMLFilesLocal && isHTMLFileName) {
109                                 // localhostURL = previewFile.getLocation().toString();
110                                 // } else if (showXMLFilesLocal && isXMLFileName) {
111                                 // localhostURL = previewFile.getLocation().toString();
112                                 // } else if ((localhostURL =
113                                 // ShowExternalPreviewAction.getLocalhostURL(null, previewFile))
114                                 // ==
115                                 // null) {
116                                 // return;
117                                 // }
118                                 // localhostURL += postFix;
119                                 ShowInContext context = getShowInContext(previewFile,
120                                                 forceDBGPreview, postFix);
121                                 IWorkbenchPart sourcePart = page.getActivePart();
122                                 if (sourcePart == null && context != null) {
123                                         return;
124                                 }
125
126                                 // try {
127                                 Perspective persp = ((WorkbenchPage) page)
128                                                 .getActivePerspective();
129                                 if (persp != null) {
130
131                                         // If this view is already visible just return.
132                                         IViewReference ref = persp.findView(BrowserView.ID_BROWSER,
133                                                         null);
134                                         IViewPart view = null;
135                                         if (ref != null) {
136                                                 view = ref.getView(true);
137                                         }
138                                         if (view == null && forceDBGPreview) {
139                                                 try {
140                                                         view = persp.showView(BrowserView.ID_BROWSER, null);
141                                                         persp.bringToTop(persp.findView(
142                                                                         BrowserView.ID_BROWSER, null));
143                                                 } catch (PartInitException e) {
144                                                         WebUI.log(e);
145                                                 }
146                                         }
147                                         if (view != null) {
148                                                 IShowInTarget target = getShowInTarget(view);
149                                                 if (target != null && target.show(context)) {
150                                                         // success
151                                                 }
152                                                 ((WorkbenchPage) page)
153                                                                 .performedShowIn(BrowserView.ID_BROWSER);
154                                         }
155                                 }
156
157                                 // IViewPart view = page.showView(BrowserView.ID_BROWSER);
158                                 // IShowInTarget target = getShowInTarget(view);
159                                 // if (target != null && target.show(new
160                                 // ShowInContext(localhostURL,
161                                 // null))) {
162                                 // // success
163                                 // }
164                                 // ((WorkbenchPage)
165                                 // page).performedShowIn(BrowserView.ID_BROWSER); //
166                                 // TODO: move back up
167                                 // } catch (PartInitException e) {
168                                 // WorkbenchPlugin.log(
169                                 // "Error showing view in ShowInAction.run", e.getStatus());
170                                 // //$NON-NLS-1$
171                                 // }
172
173                                 // try {
174                                 // IViewPart part = page.showView(BrowserView.ID_BROWSER, null,
175                                 // IWorkbenchPage.VIEW_VISIBLE);
176                                 // if (part == null) {
177                                 // part = page.showView(BrowserView.ID_BROWSER);
178                                 // } else {
179                                 // page.bringToTop(part);
180                                 // }
181                                 // ((BrowserView) part).setUrl(localhostURL);
182                                 //
183                                 // } catch (Exception e) {
184                                 // // PHPeclipsePlugin.log(e);
185                                 // }
186                         }
187                 }
188         }
189
190 }