misc
[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     if (previewFile == null) {
54       // should never happen
55       return;
56     }
57     String extension = previewFile.getFileExtension().toLowerCase();
58     boolean autoPreview = ProjectPrefUtil.getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
59     boolean bringToTopPreview = ProjectPrefUtil.getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_BRING_TO_TOP_PREVIEW_DEFAULT);
60     boolean showHTMLFilesLocal = ProjectPrefUtil.getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_SHOW_HTML_FILES_LOCAL);
61     boolean showXMLFilesLocal = ProjectPrefUtil.getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_SHOW_XML_FILES_LOCAL);
62     boolean isHTMLFileName = "html".equals(extension) || "htm".equals(extension) || "xhtml".equals(extension);
63     boolean isXMLFileName = "xml".equals(extension) || "xsd".equals(extension) || "dtd".equals(extension);
64
65     if (autoPreview) {
66       String localhostURL;
67       if (showHTMLFilesLocal && isHTMLFileName) {
68         localhostURL = previewFile.getLocation().toString();
69       } else if (showXMLFilesLocal && isXMLFileName) {
70         localhostURL = previewFile.getLocation().toString();
71       } else if ((localhostURL = getLocalhostURL(null, previewFile)) == null) {
72         return;
73       }
74       IWorkbenchPage page = WebUI.getActivePage();
75       try {
76         IViewPart part = page.findView(BrowserView.ID_BROWSER);
77         if (part == null) {
78           part = page.showView(BrowserView.ID_BROWSER);
79         } else {
80           if (bringToTopPreview) {
81             page.bringToTop(part);
82           }
83         }
84         ((BrowserView) part).setUrl(localhostURL);
85
86       } catch (Exception e) {
87         //PHPeclipsePlugin.log(e);
88       }
89     }
90   }
91
92   public void refresh(int type) {
93     IFile fileToParse = getFile();
94     if (fileToParse == null) {
95       // should never happen
96       return;
97     }
98     boolean autoPreview = ProjectPrefUtil.getPreviewBooleanValue(fileToParse, IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
99     boolean bringToTopPreview = ProjectPrefUtil.getPreviewBooleanValue(fileToParse, IPreferenceConstants.PHP_BRING_TO_TOP_PREVIEW_DEFAULT);
100
101     if (autoPreview) {
102       IWorkbenchPage page = WebUI.getActivePage();
103       try {
104         IViewPart part = page.findView(BrowserView.ID_BROWSER);
105         if (part == null) {
106           part = page.showView(BrowserView.ID_BROWSER);
107         } else {
108           if (bringToTopPreview) {
109             page.bringToTop(part);
110           }
111         }
112         if (part != null) {
113           ((BrowserView) part).refresh();
114         }
115       } catch (Exception e) {
116         //  PHPeclipsePlugin.log(e);
117       }
118     }
119   }
120
121   /**
122    * Finds the file that's currently opened in the PHP Text Editor
123    */
124   protected IFile getFile() {
125     ITextEditor editor = getTextEditor();
126     IEditorInput editorInput = null;
127     if (editor != null) {
128       editorInput = editor.getEditorInput();
129     }
130     if (editorInput instanceof IFileEditorInput)
131       return ((IFileEditorInput) editorInput).getFile();
132     // if nothing was found, which should never happen
133     return null;
134   }
135
136   public static String getLocalhostURL(IPreferenceStore store, IFile file) {
137     if (store == null) {
138       store = WebUI.getDefault().getPreferenceStore();
139     }
140     // IPath path = file.getFullPath();
141     String localhostURL = file.getLocation().toString();
142     String lowerCaseFileName = localhostURL.toLowerCase();
143     //  String documentRoot = store.getString(PHPeclipsePlugin.DOCUMENTROOT_PREF);
144     IPath documentRootPath = ProjectPrefUtil.getDocumentRoot(file.getProject());
145     String documentRoot = documentRootPath.toString().toLowerCase();
146     if (lowerCaseFileName.startsWith(documentRoot)) {
147       localhostURL = localhostURL.substring(documentRoot.length());
148     } else {
149       return null;
150     }
151     //    return store.getString(PHPeclipsePlugin.LOCALHOST_PREF) + localhostURL;
152     return ProjectPrefUtil.getMiscProjectsPreferenceValue(file.getProject(), IPreferenceConstants.PHP_LOCALHOST_PREF) + localhostURL;
153   }
154 }