776e99266207e67de24e19cd3777dca93bd66c9e
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / ui / editor / ShowExternalPreviewAction.java
1 package net.sourceforge.phpeclipse.ui.editor;
2
3 /*******************************************************************************
4  * Copyright (c) 2000, 2002 IBM Corp. and others. All rights reserved. This
5  * program and the accompanying materials are made available under the terms of
6  * the Common Public License v1.0 which accompanies this distribution, and is
7  * available at http://www.eclipse.org/legal/cpl-v10.html
8  * 
9  * Contributors: IBM Corporation - Initial implementation Klaus Hartlage -
10  * www.eclipseproject.de
11  ******************************************************************************/
12 import net.sourceforge.phpeclipse.ui.IPreferenceConstants;
13 import net.sourceforge.phpeclipse.ui.WebUI;
14 import net.sourceforge.phpeclipse.ui.overlaypages.Util;
15 import net.sourceforge.phpeclipse.webbrowser.views.BrowserView;
16
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.jface.preference.IPreferenceStore;
19 import org.eclipse.ui.IEditorInput;
20 import org.eclipse.ui.IFileEditorInput;
21 import org.eclipse.ui.IViewPart;
22 import org.eclipse.ui.IWorkbenchPage;
23 import org.eclipse.ui.texteditor.ITextEditor;
24 import org.eclipse.ui.texteditor.TextEditorAction;
25
26 /**
27  * ClassDeclaration that defines the action for parsing the current PHP file
28  */
29 public class ShowExternalPreviewAction extends TextEditorAction {
30   public final static int XML_TYPE = 1;
31
32   public final static int HTML_TYPE = 2;
33
34   public final static int SMARTY_TYPE = 3;
35
36   public final static int PHP_TYPE = 4;
37
38   private static ShowExternalPreviewAction instance = new ShowExternalPreviewAction();
39
40   /**
41    * Constructs and updates the action.
42    */
43   private ShowExternalPreviewAction() {
44     super(EditorMessages.getResourceBundle(), "ParserAction.", null); //$NON-NLS-1$
45     update();
46   }
47
48   public static ShowExternalPreviewAction getInstance() {
49     return instance;
50   }
51
52   /**
53    * Code called when the action is fired.
54    */
55   public void run() {
56     doRun(PHP_TYPE);
57   }
58
59   public void doRun(int type) {
60     IFile previewFile = getFile();
61     if (previewFile == null) {
62       // should never happen
63       return;
64     }
65     String extension = previewFile.getFileExtension().toLowerCase();
66     boolean autoPreview = Util.getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
67     boolean bringToTopPreview = Util.getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_BRING_TO_TOP_PREVIEW_DEFAULT);
68     boolean showHTMLFilesLocal = Util.getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_SHOW_HTML_FILES_LOCAL);
69     boolean showXMLFilesLocal = Util.getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_SHOW_XML_FILES_LOCAL);
70     boolean isHTMLFileName = "html".equals(extension) || "htm".equals(extension) || "xhtml".equals(extension);
71     boolean isXMLFileName = "xml".equals(extension) || "xsd".equals(extension) || "dtd".equals(extension);
72
73     if (autoPreview) {
74       String localhostURL;
75       if (showHTMLFilesLocal && isHTMLFileName) {
76         localhostURL = previewFile.getLocation().toString();
77       } else if (showXMLFilesLocal && isXMLFileName) {
78         localhostURL = previewFile.getLocation().toString();
79       } else if ((localhostURL = getLocalhostURL(null, previewFile)) == null) {
80         return;
81       }
82       IWorkbenchPage page = WebUI.getActivePage();
83       try {
84         IViewPart part = page.findView(BrowserView.ID_BROWSER);
85         if (part == null) {
86           part = page.showView(BrowserView.ID_BROWSER);
87         } else {
88           if (bringToTopPreview) {
89             page.bringToTop(part);
90           }
91         }
92         ((BrowserView) part).setUrl(localhostURL);
93
94       } catch (Exception e) {
95         //PHPeclipsePlugin.log(e);
96       }
97     }
98   }
99
100   public void refresh(int type) {
101     IFile fileToParse = getFile();
102     if (fileToParse == null) {
103       // should never happen
104       return;
105     }
106     boolean autoPreview = Util.getPreviewBooleanValue(fileToParse, IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
107     boolean bringToTopPreview = Util.getPreviewBooleanValue(fileToParse, IPreferenceConstants.PHP_BRING_TO_TOP_PREVIEW_DEFAULT);
108
109     if (autoPreview) {
110       IWorkbenchPage page = WebUI.getActivePage();
111       try {
112         IViewPart part = page.findView(BrowserView.ID_BROWSER);
113         if (part == null) {
114           part = page.showView(BrowserView.ID_BROWSER);
115         } else {
116           if (bringToTopPreview) {
117             page.bringToTop(part);
118           }
119         }
120         if (part != null) {
121           ((BrowserView) part).refresh();
122         }
123       } catch (Exception e) {
124         //  PHPeclipsePlugin.log(e);
125       }
126     }
127   }
128
129   /**
130    * Finds the file that's currently opened in the PHP Text Editor
131    */
132   protected IFile getFile() {
133     ITextEditor editor = getTextEditor();
134     IEditorInput editorInput = null;
135     if (editor != null) {
136       editorInput = editor.getEditorInput();
137     }
138     if (editorInput instanceof IFileEditorInput)
139       return ((IFileEditorInput) editorInput).getFile();
140     // if nothing was found, which should never happen
141     return null;
142   }
143
144   public static String getLocalhostURL(IPreferenceStore store, IFile file) {
145     if (store == null) {
146       store = WebUI.getDefault().getPreferenceStore();
147     }
148     // IPath path = file.getFullPath();
149     String localhostURL = file.getLocation().toString();
150     String lowerCaseFileName = localhostURL.toLowerCase();
151     //  String documentRoot = store.getString(PHPeclipsePlugin.DOCUMENTROOT_PREF);
152     String documentRoot = Util.getMiscProjectsPreferenceValue(file.getProject(), IPreferenceConstants.PHP_DOCUMENTROOT_PREF);
153
154     documentRoot = documentRoot.replace('\\', '/');
155     documentRoot = documentRoot.toLowerCase();
156
157     if (lowerCaseFileName.startsWith(documentRoot)) {
158       localhostURL = localhostURL.substring(documentRoot.length());
159     } else {
160       return null;
161     }
162     //    return store.getString(PHPeclipsePlugin.LOCALHOST_PREF) + localhostURL;
163     return Util.getMiscProjectsPreferenceValue(file.getProject(), IPreferenceConstants.PHP_LOCALHOST_PREF) + localhostURL;
164   }
165 }