1 package net.sourceforge.phpeclipse.ui.editor;
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
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;
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;
27 * ClassDeclaration that defines the action for parsing the current PHP file
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;
35 private static ShowExternalPreviewAction instance = new ShowExternalPreviewAction();
38 * Constructs and updates the action.
40 private ShowExternalPreviewAction() {
41 super(EditorMessages.getResourceBundle(), "ParserAction.", null); //$NON-NLS-1$
45 public static ShowExternalPreviewAction getInstance() {
50 * Code called when the action is fired.
56 public void doRun(int type) {
57 IFile previewFile = getFile();
58 if (previewFile == null) {
59 // should never happen
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);
72 if (showHTMLFilesLocal && isHTMLFileName) {
73 localhostURL = previewFile.getLocation().toString();
74 } else if ((localhostURL = getLocalhostURL(null, previewFile)) == null) {
77 IWorkbenchPage page = WebUI.getActivePage();
79 IViewPart part = page.findView(BrowserView.ID_BROWSER);
81 part = page.showView(BrowserView.ID_BROWSER);
83 if (bringToTopPreview) {
84 page.bringToTop(part);
87 ((BrowserView) part).setUrl(localhostURL);
89 } catch (Exception e) {
90 //PHPeclipsePlugin.log(e);
95 public void refresh(int type) {
96 IFile fileToParse = getFile();
97 if (fileToParse == null) {
98 // should never happen
101 boolean autoPreview = Util.getPreviewBooleanValue(fileToParse, IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
102 boolean bringToTopPreview = Util.getPreviewBooleanValue(fileToParse, IPreferenceConstants.PHP_BRING_TO_TOP_PREVIEW_DEFAULT);
105 IWorkbenchPage page = WebUI.getActivePage();
107 IViewPart part = page.findView(BrowserView.ID_BROWSER);
109 part = page.showView(BrowserView.ID_BROWSER);
111 if (bringToTopPreview) {
112 page.bringToTop(part);
115 ((BrowserView) part).refresh();
117 } catch (Exception e) {
118 // PHPeclipsePlugin.log(e);
124 * Finds the file that's currently opened in the PHP Text Editor
126 protected IFile getFile() {
127 ITextEditor editor = getTextEditor();
128 IEditorInput editorInput = null;
129 if (editor != null) {
130 editorInput = editor.getEditorInput();
132 if (editorInput instanceof IFileEditorInput)
133 return ((IFileEditorInput) editorInput).getFile();
134 // if nothing was found, which should never happen
138 public static String getLocalhostURL(IPreferenceStore store, IFile file) {
140 store = WebUI.getDefault().getPreferenceStore();
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);
148 documentRoot = documentRoot.replace('\\', '/');
149 documentRoot = documentRoot.toLowerCase();
151 if (lowerCaseFileName.startsWith(documentRoot)) {
152 localhostURL = localhostURL.substring(documentRoot.length());
156 // return store.getString(PHPeclipsePlugin.LOCALHOST_PREF) + localhostURL;
157 return Util.getMiscProjectsPreferenceValue(file.getProject(), IPreferenceConstants.PHP_LOCALHOST_PREF) + localhostURL;