misc changes
[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   public final static int HTML_TYPE = 2;
32   public final static int SMARTY_TYPE = 3;
33   public final static int PHP_TYPE = 4;
34
35   private static ShowExternalPreviewAction instance = new ShowExternalPreviewAction();
36
37   /**
38    * Constructs and updates the action.
39    */
40   private ShowExternalPreviewAction() {
41     super(EditorMessages.getResourceBundle(), "ParserAction.", null); //$NON-NLS-1$
42     update();
43   }
44
45   public static ShowExternalPreviewAction getInstance() {
46     return instance;
47   }
48
49   /**
50    * Code called when the action is fired.
51    */
52   public void run() {
53     doRun(PHP_TYPE);
54   }
55
56   public void doRun(int type) {
57     IFile previewFile = getFile();
58     if (previewFile == null) {
59       // should never happen
60       return;
61     }
62     String extension = previewFile.getFileExtension().toLowerCase();
63     boolean autoPreview = Util.getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
64     boolean bringToTopPreview = Util.getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_BRING_TO_TOP_PREVIEW_DEFAULT);
65     boolean showHTMLFilesLocal = Util.getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_SHOW_HTML_FILES_LOCAL);
66     boolean isHTMLFileName = "xml".equals(extension)
67         || "html".equals(extension) 
68         || "htm".equals(extension)
69         || "xhtml".equals(extension);
70     if (autoPreview) {
71       String localhostURL;
72       if (showHTMLFilesLocal && isHTMLFileName) {
73         localhostURL = previewFile.getLocation().toString();
74       } else if ((localhostURL = getLocalhostURL(null, previewFile)) == null) {
75         return;
76       }
77       IWorkbenchPage page = WebUI.getActivePage();
78       try {
79         IViewPart part = page.findView(BrowserView.ID_BROWSER);
80         if (part == null) {
81           part = page.showView(BrowserView.ID_BROWSER);
82         } else {
83           if (bringToTopPreview) {
84             page.bringToTop(part);
85           }
86         }
87         ((BrowserView) part).setUrl(localhostURL);
88
89       } catch (Exception e) {
90         //PHPeclipsePlugin.log(e);
91       }
92     }
93   }
94
95   public void refresh(int type) {
96     IFile fileToParse = getFile();
97     if (fileToParse == null) {
98       // should never happen
99       return;
100     }
101     boolean autoPreview = Util.getPreviewBooleanValue(fileToParse, IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
102     boolean bringToTopPreview = Util.getPreviewBooleanValue(fileToParse, IPreferenceConstants.PHP_BRING_TO_TOP_PREVIEW_DEFAULT);
103
104     if (autoPreview) {
105       IWorkbenchPage page = WebUI.getActivePage();
106       try {
107         IViewPart part = page.findView(BrowserView.ID_BROWSER);
108         if (part == null) {
109           part = page.showView(BrowserView.ID_BROWSER);
110         } else {
111           if (bringToTopPreview) {
112             page.bringToTop(part);
113           }
114         }
115         ((BrowserView) part).refresh();
116
117       } catch (Exception e) {
118         //  PHPeclipsePlugin.log(e);
119       }
120     }
121   }
122
123   /**
124    * Finds the file that's currently opened in the PHP Text Editor
125    */
126   protected IFile getFile() {
127     ITextEditor editor = getTextEditor();
128     IEditorInput editorInput = null;
129     if (editor != null) {
130       editorInput = editor.getEditorInput();
131     }
132     if (editorInput instanceof IFileEditorInput)
133       return ((IFileEditorInput) editorInput).getFile();
134     // if nothing was found, which should never happen
135     return null;
136   }
137
138   public static String getLocalhostURL(IPreferenceStore store, IFile file) {
139     if (store == null) {
140       store = WebUI.getDefault().getPreferenceStore();
141     }
142     // IPath path = file.getFullPath();
143     String localhostURL = file.getLocation().toString();
144     String lowerCaseFileName = localhostURL.toLowerCase();
145     //  String documentRoot = store.getString(PHPeclipsePlugin.DOCUMENTROOT_PREF);
146     String documentRoot = Util.getMiscProjectsPreferenceValue(file.getProject(), IPreferenceConstants.PHP_DOCUMENTROOT_PREF);
147
148     documentRoot = documentRoot.replace('\\', '/');
149     documentRoot = documentRoot.toLowerCase();
150
151     if (lowerCaseFileName.startsWith(documentRoot)) {
152       localhostURL = localhostURL.substring(documentRoot.length());
153     } else {
154       return null;
155     }
156     //    return store.getString(PHPeclipsePlugin.LOCALHOST_PREF) + localhostURL;
157     return Util.getMiscProjectsPreferenceValue(file.getProject(), IPreferenceConstants.PHP_LOCALHOST_PREF) + localhostURL;
158   }
159 }