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