8e8c9b8cf91b6a75eec14410223c02595beebbac
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / ui / editor / ShowExternalPreviewAction.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
8 import org.eclipse.core.resources.IFile;
9 import org.eclipse.core.runtime.IPath;
10 import org.eclipse.core.runtime.IProgressMonitor;
11 import org.eclipse.core.runtime.IStatus;
12 import org.eclipse.core.runtime.Status;
13 import org.eclipse.jface.preference.IPreferenceStore;
14 import org.eclipse.ui.IEditorInput;
15 import org.eclipse.ui.IFileEditorInput;
16 import org.eclipse.ui.IViewPart;
17 import org.eclipse.ui.IWorkbenchPage;
18 import org.eclipse.ui.PartInitException;
19 import org.eclipse.ui.progress.WorkbenchJob;
20 import org.eclipse.ui.texteditor.ITextEditor;
21 import org.eclipse.ui.texteditor.TextEditorAction;
22
23 /**
24  * ClassDeclaration that defines the action for parsing the current PHP file
25  */
26 public class ShowExternalPreviewAction extends TextEditorAction {
27         public final static int XML_TYPE = 1;
28
29         public final static int HTML_TYPE = 2;
30
31         public final static int SMARTY_TYPE = 3;
32
33         public final static int PHP_TYPE = 4;
34
35         private static ShowExternalPreviewAction instance = new ShowExternalPreviewAction();
36
37         /**
38          * Constructs and updates the action.
39          */
40         private ShowExternalPreviewAction() {
41                 super(EditorMessages.getResourceBundle(), "ParserAction.", null); //$NON-NLS-1$
42                 update();
43         }
44
45         public static ShowExternalPreviewAction getInstance() {
46                 return instance;
47         }
48
49         /**
50          * Code called when the action is fired.
51          */
52         public void run() {
53                 doRun(PHP_TYPE);
54         }
55
56         public void doRun(int type) {
57                 IFile previewFile = getFile();
58                 BrowserUtil.showPreview(previewFile, false, "");
59         }
60
61         public void refresh(int type) {
62                 IFile previewFile = getFile();
63                 if (previewFile == null) {
64                         // should never happen
65                         return;
66                 }
67                 boolean autoPreview = ProjectPrefUtil.getPreviewBooleanValue(
68                                 previewFile, IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
69                 boolean bringToTopPreview = ProjectPrefUtil.getPreviewBooleanValue(
70                                 previewFile,
71                                 IPreferenceConstants.PHP_BRING_TO_TOP_PREVIEW_DEFAULT);
72
73                 if (autoPreview) {
74                         IWorkbenchPage page = WebUI.getActivePage();
75                         if (page == null) {
76                                 // startup stage
77                                 return;
78                         }
79                         try {
80                                 // IViewPart part = page.findView(BrowserView.ID_BROWSER);
81                                 // if (part == null) {
82                                 // part = page.showView(BrowserView.ID_BROWSER);
83                                 // }
84                                 IViewPart part = page.showView(BrowserView.ID_BROWSER, null,
85                                                 IWorkbenchPage.VIEW_CREATE);
86                                 if (part != null) {
87                                         if (bringToTopPreview) {
88                                                 // page.bringToTop(part);
89                                                 new WorkbenchJob(getClass().getName()) {
90                                                         public IStatus runInUIThread(
91                                                                         IProgressMonitor monitor) {
92                                                                 IWorkbenchPage page = WebUI.getActivePage();
93                                                                 if (page != null) {
94                                                                         IViewPart part = page
95                                                                                         .findView(BrowserView.ID_BROWSER);
96                                                                         if (part != null) {
97                                                                                 page.bringToTop(part);
98                                                                         }
99                                                                 }
100                                                                 return Status.OK_STATUS;
101                                                         }
102                                                 }.schedule();
103                                         }
104                                         // ((BrowserView) part).refresh();
105                                         String localhostURL = getLocalhostURL(null, previewFile);
106                                         ((BrowserView) part).refresh(localhostURL);
107                                 }
108                         } catch (PartInitException e) {
109                                 // ad hoc
110                                 WebUI.getDefault().getLog().log(
111                                                 new Status(IStatus.ERROR,
112                                                                 "net.sourceforge.phpeclipse.ui", IStatus.OK,
113                                                                 "Failed to show Browser View", e));
114                                 // PHPeclipsePlugin.log(e);
115                         }
116                 }
117         }
118
119         /**
120          * Finds the file that's currently opened in the PHP Text Editor
121          */
122         protected IFile getFile() {
123                 ITextEditor editor = getTextEditor();
124                 IEditorInput editorInput = null;
125                 if (editor != null) {
126                         editorInput = editor.getEditorInput();
127                 }
128                 if (editorInput instanceof IFileEditorInput)
129                         return ((IFileEditorInput) editorInput).getFile();
130                 // if nothing was found, which should never happen
131                 return null;
132         }
133
134         public static String getLocalhostURL(IPreferenceStore store, IFile file) {
135                 if (file != null) {
136                         if (store == null) {
137                                 store = WebUI.getDefault().getPreferenceStore();
138                         }
139                         // IPath path = file.getFullPath();
140                         String localhostURL = file.getLocation().toString();
141                         String lowerCaseFileName = localhostURL.toLowerCase();
142                         // String documentRoot =
143                         // store.getString(PHPeclipsePlugin.DOCUMENTROOT_PREF);
144                         IPath documentRootPath = ProjectPrefUtil.getDocumentRoot(file
145                                         .getProject());
146                         String documentRoot = documentRootPath.toString().toLowerCase();
147                         if (lowerCaseFileName.startsWith(documentRoot)) {
148                                 localhostURL = localhostURL.substring(documentRoot.length());
149                         } else {
150                                 return null;
151                         }
152                         // return store.getString(PHPeclipsePlugin.LOCALHOST_PREF) +
153                         // localhostURL;
154                         return ProjectPrefUtil.getMiscProjectsPreferenceValue(file
155                                         .getProject(), IPreferenceConstants.PHP_LOCALHOST_PREF)
156                                         + localhostURL;
157                 }
158                 return "http://localhost";
159         }
160 }