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