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