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.core.runtime.IStatus;
11 import org.eclipse.core.runtime.Status;
12 import org.eclipse.jface.preference.IPreferenceStore;
13 import org.eclipse.ui.IEditorInput;
14 import org.eclipse.ui.IFileEditorInput;
15 import org.eclipse.ui.IViewPart;
16 import org.eclipse.ui.IWorkbenchPage;
17 import org.eclipse.ui.PartInitException;
18 import org.eclipse.ui.texteditor.ITextEditor;
19 import org.eclipse.ui.texteditor.TextEditorAction;
22 * ClassDeclaration that defines the action for parsing the current PHP file
24 public class ShowExternalPreviewAction extends TextEditorAction {
25 public final static int XML_TYPE = 1;
27 public final static int HTML_TYPE = 2;
29 public final static int SMARTY_TYPE = 3;
31 public final static int PHP_TYPE = 4;
33 private static ShowExternalPreviewAction instance = new ShowExternalPreviewAction();
36 * Constructs and updates the action.
38 private ShowExternalPreviewAction() {
39 super(EditorMessages.getResourceBundle(), "ParserAction.", null); //$NON-NLS-1$
43 public static ShowExternalPreviewAction getInstance() {
48 * Code called when the action is fired.
54 public void doRun(int type) {
55 IFile previewFile = getFile();
56 BrowserUtil.showPreview(previewFile, false, "");
59 public void refresh(int type) {
60 IFile previewFile = getFile();
61 if (previewFile == null) {
62 // should never happen
65 boolean autoPreview = ProjectPrefUtil.getPreviewBooleanValue(
66 previewFile, IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
67 boolean bringToTopPreview = ProjectPrefUtil.getPreviewBooleanValue(
69 IPreferenceConstants.PHP_BRING_TO_TOP_PREVIEW_DEFAULT);
72 IWorkbenchPage page = WebUI.getActivePage();
74 // IViewPart part = page.findView(BrowserView.ID_BROWSER);
75 // if (part == null) {
76 // part = page.showView(BrowserView.ID_BROWSER);
78 IViewPart part = page.showView(BrowserView.ID_BROWSER, null,
79 IWorkbenchPage.VIEW_CREATE);
81 if (bringToTopPreview) {
82 page.bringToTop(part);
84 // ((BrowserView) part).refresh();
85 String localhostURL = getLocalhostURL(null, previewFile);
86 ((BrowserView) part).refresh(localhostURL);
88 } catch (PartInitException e) {
90 WebUI.getDefault().getLog().log(
91 new Status(IStatus.ERROR,
92 "net.sourceforge.phpeclipse.ui", IStatus.OK,
93 "Failed to show Browser View", e));
94 // PHPeclipsePlugin.log(e);
100 * Finds the file that's currently opened in the PHP Text Editor
102 protected IFile getFile() {
103 ITextEditor editor = getTextEditor();
104 IEditorInput editorInput = null;
105 if (editor != null) {
106 editorInput = editor.getEditorInput();
108 if (editorInput instanceof IFileEditorInput)
109 return ((IFileEditorInput) editorInput).getFile();
110 // if nothing was found, which should never happen
114 public static String getLocalhostURL(IPreferenceStore store, IFile file) {
117 store = WebUI.getDefault().getPreferenceStore();
119 // IPath path = file.getFullPath();
120 String localhostURL = file.getLocation().toString();
121 String lowerCaseFileName = localhostURL.toLowerCase();
122 // String documentRoot =
123 // store.getString(PHPeclipsePlugin.DOCUMENTROOT_PREF);
124 IPath documentRootPath = ProjectPrefUtil.getDocumentRoot(file
126 String documentRoot = documentRootPath.toString().toLowerCase();
127 if (lowerCaseFileName.startsWith(documentRoot)) {
128 localhostURL = localhostURL.substring(documentRoot.length());
132 // return store.getString(PHPeclipsePlugin.LOCALHOST_PREF) +
134 return ProjectPrefUtil.getMiscProjectsPreferenceValue(file
135 .getProject(), IPreferenceConstants.PHP_LOCALHOST_PREF)
138 return "http://localhost";