#1365431 (toshihiro) - PHP Editor crashes Windows with JavaScript 'windows.close()'
[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 fileToParse = getFile();
58                 if (fileToParse == null) {
59                         // should never happen
60                         return;
61                 }
62                 boolean autoPreview = ProjectPrefUtil.getPreviewBooleanValue(
63                                 fileToParse, IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
64                 boolean bringToTopPreview = ProjectPrefUtil.getPreviewBooleanValue(
65                                 fileToParse,
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                                 }
82                         } catch (Exception e) {
83                                 // PHPeclipsePlugin.log(e);
84                         }
85                 }
86         }
87
88         /**
89          * Finds the file that's currently opened in the PHP Text Editor
90          */
91         protected IFile getFile() {
92                 ITextEditor editor = getTextEditor();
93                 IEditorInput editorInput = null;
94                 if (editor != null) {
95                         editorInput = editor.getEditorInput();
96                 }
97                 if (editorInput instanceof IFileEditorInput)
98                         return ((IFileEditorInput) editorInput).getFile();
99                 // if nothing was found, which should never happen
100                 return null;
101         }
102
103         public static String getLocalhostURL(IPreferenceStore store, IFile file) {
104                 if (file != null) {
105                         if (store == null) {
106                                 store = WebUI.getDefault().getPreferenceStore();
107                         }
108                         // IPath path = file.getFullPath();
109                         String localhostURL = file.getLocation().toString();
110                         String lowerCaseFileName = localhostURL.toLowerCase();
111                         // String documentRoot =
112                         // store.getString(PHPeclipsePlugin.DOCUMENTROOT_PREF);
113                         IPath documentRootPath = ProjectPrefUtil.getDocumentRoot(file
114                                         .getProject());
115                         String documentRoot = documentRootPath.toString().toLowerCase();
116                         if (lowerCaseFileName.startsWith(documentRoot)) {
117                                 localhostURL = localhostURL.substring(documentRoot.length());
118                         } else {
119                                 return null;
120                         }
121                         // return store.getString(PHPeclipsePlugin.LOCALHOST_PREF) +
122                         // localhostURL;
123                         return ProjectPrefUtil.getMiscProjectsPreferenceValue(file
124                                         .getProject(), IPreferenceConstants.PHP_LOCALHOST_PREF)
125                                         + localhostURL;
126                 }
127                 return "http://localhost";
128         }
129 }