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;
32 public final static int HTML_TYPE = 2;
34 public final static int SMARTY_TYPE = 3;
36 public final static int PHP_TYPE = 4;
38 private static ShowExternalPreviewAction instance = new ShowExternalPreviewAction();
41 * Constructs and updates the action.
43 private ShowExternalPreviewAction() {
44 super(EditorMessages.getResourceBundle(), "ParserAction.", null); //$NON-NLS-1$
48 public static ShowExternalPreviewAction getInstance() {
53 * Code called when the action is fired.
59 public void doRun(int type) {
60 IFile previewFile = getFile();
61 if (previewFile == null) {
62 // should never happen
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);
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) {
82 IWorkbenchPage page = WebUI.getActivePage();
84 IViewPart part = page.findView(BrowserView.ID_BROWSER);
86 part = page.showView(BrowserView.ID_BROWSER);
88 if (bringToTopPreview) {
89 page.bringToTop(part);
92 ((BrowserView) part).setUrl(localhostURL);
94 } catch (Exception e) {
95 //PHPeclipsePlugin.log(e);
100 public void refresh(int type) {
101 IFile fileToParse = getFile();
102 if (fileToParse == null) {
103 // should never happen
106 boolean autoPreview = Util.getPreviewBooleanValue(fileToParse, IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
107 boolean bringToTopPreview = Util.getPreviewBooleanValue(fileToParse, IPreferenceConstants.PHP_BRING_TO_TOP_PREVIEW_DEFAULT);
110 IWorkbenchPage page = WebUI.getActivePage();
112 IViewPart part = page.findView(BrowserView.ID_BROWSER);
114 part = page.showView(BrowserView.ID_BROWSER);
116 if (bringToTopPreview) {
117 page.bringToTop(part);
121 ((BrowserView) part).refresh();
123 } catch (Exception e) {
124 // PHPeclipsePlugin.log(e);
130 * Finds the file that's currently opened in the PHP Text Editor
132 protected IFile getFile() {
133 ITextEditor editor = getTextEditor();
134 IEditorInput editorInput = null;
135 if (editor != null) {
136 editorInput = editor.getEditorInput();
138 if (editorInput instanceof IFileEditorInput)
139 return ((IFileEditorInput) editorInput).getFile();
140 // if nothing was found, which should never happen
144 public static String getLocalhostURL(IPreferenceStore store, IFile file) {
146 store = WebUI.getDefault().getPreferenceStore();
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);
154 documentRoot = documentRoot.replace('\\', '/');
155 documentRoot = documentRoot.toLowerCase();
157 if (lowerCaseFileName.startsWith(documentRoot)) {
158 localhostURL = localhostURL.substring(documentRoot.length());
162 // return store.getString(PHPeclipsePlugin.LOCALHOST_PREF) + localhostURL;
163 return Util.getMiscProjectsPreferenceValue(file.getProject(), IPreferenceConstants.PHP_LOCALHOST_PREF) + localhostURL;