1 package net.sourceforge.phpeclipse.ui.editor;
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;
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;
19 * ClassDeclaration that defines the action for parsing the current PHP file
21 public class ShowExternalPreviewAction extends TextEditorAction {
22 public final static int XML_TYPE = 1;
24 public final static int HTML_TYPE = 2;
26 public final static int SMARTY_TYPE = 3;
28 public final static int PHP_TYPE = 4;
30 private static ShowExternalPreviewAction instance = new ShowExternalPreviewAction();
33 * Constructs and updates the action.
35 private ShowExternalPreviewAction() {
36 super(EditorMessages.getResourceBundle(), "ParserAction.", null); //$NON-NLS-1$
40 public static ShowExternalPreviewAction getInstance() {
45 * Code called when the action is fired.
51 public void doRun(int type) {
52 IFile previewFile = getFile();
53 BrowserUtil.showPreview(previewFile, false, "");
56 public void refresh(int type) {
57 IFile previewFile = getFile();
58 if (previewFile == null) {
59 // should never happen
62 boolean autoPreview = ProjectPrefUtil.getPreviewBooleanValue(
63 previewFile, IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
64 boolean bringToTopPreview = ProjectPrefUtil.getPreviewBooleanValue(
66 IPreferenceConstants.PHP_BRING_TO_TOP_PREVIEW_DEFAULT);
69 IWorkbenchPage page = WebUI.getActivePage();
71 IViewPart part = page.findView(BrowserView.ID_BROWSER);
73 part = page.showView(BrowserView.ID_BROWSER);
75 if (bringToTopPreview) {
76 page.bringToTop(part);
80 // ((BrowserView) part).refresh();
81 String localhostURL = getLocalhostURL(null, previewFile);
82 ((BrowserView) part).refresh(localhostURL);
84 } catch (Exception e) {
85 // PHPeclipsePlugin.log(e);
91 * Finds the file that's currently opened in the PHP Text Editor
93 protected IFile getFile() {
94 ITextEditor editor = getTextEditor();
95 IEditorInput editorInput = null;
97 editorInput = editor.getEditorInput();
99 if (editorInput instanceof IFileEditorInput)
100 return ((IFileEditorInput) editorInput).getFile();
101 // if nothing was found, which should never happen
105 public static String getLocalhostURL(IPreferenceStore store, IFile file) {
108 store = WebUI.getDefault().getPreferenceStore();
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
117 String documentRoot = documentRootPath.toString().toLowerCase();
118 if (lowerCaseFileName.startsWith(documentRoot)) {
119 localhostURL = localhostURL.substring(documentRoot.length());
123 // return store.getString(PHPeclipsePlugin.LOCALHOST_PREF) +
125 return ProjectPrefUtil.getMiscProjectsPreferenceValue(file
126 .getProject(), IPreferenceConstants.PHP_LOCALHOST_PREF)
129 return "http://localhost";