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.IProgressMonitor;
11 import org.eclipse.core.runtime.IStatus;
12 import org.eclipse.core.runtime.Status;
13 import org.eclipse.jface.preference.IPreferenceStore;
14 import org.eclipse.ui.IEditorInput;
15 import org.eclipse.ui.IFileEditorInput;
16 import org.eclipse.ui.IViewPart;
17 import org.eclipse.ui.IWorkbenchPage;
18 import org.eclipse.ui.PartInitException;
19 import org.eclipse.ui.progress.WorkbenchJob;
20 import org.eclipse.ui.texteditor.ITextEditor;
21 import org.eclipse.ui.texteditor.TextEditorAction;
24 * ClassDeclaration that defines the action for parsing the current PHP file
26 public class ShowExternalPreviewAction extends TextEditorAction {
27 public final static int XML_TYPE = 1;
29 public final static int HTML_TYPE = 2;
31 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 BrowserUtil.showPreview(previewFile, false, "");
61 public void refresh(int type) {
62 IFile previewFile = getFile();
63 if (previewFile == null) {
64 // should never happen
67 boolean autoPreview = ProjectPrefUtil.getPreviewBooleanValue(
68 previewFile, IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
69 boolean bringToTopPreview = ProjectPrefUtil.getPreviewBooleanValue(
71 IPreferenceConstants.PHP_BRING_TO_TOP_PREVIEW_DEFAULT);
74 IWorkbenchPage page = WebUI.getActivePage();
80 // IViewPart part = page.findView(BrowserView.ID_BROWSER);
81 // if (part == null) {
82 // part = page.showView(BrowserView.ID_BROWSER);
84 IViewPart part = page.showView(BrowserView.ID_BROWSER, null,
85 IWorkbenchPage.VIEW_CREATE);
87 if (bringToTopPreview) {
88 // page.bringToTop(part);
89 new WorkbenchJob(getClass().getName()) {
90 public IStatus runInUIThread(
91 IProgressMonitor monitor) {
92 IWorkbenchPage page = WebUI.getActivePage();
95 .findView(BrowserView.ID_BROWSER);
97 page.bringToTop(part);
100 return Status.OK_STATUS;
104 // ((BrowserView) part).refresh();
105 String localhostURL = getLocalhostURL(null, previewFile);
106 ((BrowserView) part).refresh(localhostURL);
108 } catch (PartInitException e) {
110 WebUI.getDefault().getLog().log(
111 new Status(IStatus.ERROR,
112 "net.sourceforge.phpeclipse.ui", IStatus.OK,
113 "Failed to show Browser View", e));
114 // PHPeclipsePlugin.log(e);
120 * Finds the file that's currently opened in the PHP Text Editor
122 protected IFile getFile() {
123 ITextEditor editor = getTextEditor();
124 IEditorInput editorInput = null;
125 if (editor != null) {
126 editorInput = editor.getEditorInput();
128 if (editorInput instanceof IFileEditorInput)
129 return ((IFileEditorInput) editorInput).getFile();
130 // if nothing was found, which should never happen
134 public static String getLocalhostURL(IPreferenceStore store, IFile file) {
137 store = WebUI.getDefault().getPreferenceStore();
139 // IPath path = file.getFullPath();
140 String localhostURL = file.getLocation().toString();
141 String lowerCaseFileName = localhostURL.toLowerCase();
142 // String documentRoot =
143 // store.getString(PHPeclipsePlugin.DOCUMENTROOT_PREF);
144 IPath documentRootPath = ProjectPrefUtil.getDocumentRoot(file
146 String documentRoot = documentRootPath.toString().toLowerCase();
147 if (lowerCaseFileName.startsWith(documentRoot)) {
148 localhostURL = localhostURL.substring(documentRoot.length());
152 // return store.getString(PHPeclipsePlugin.LOCALHOST_PREF) +
154 return ProjectPrefUtil.getMiscProjectsPreferenceValue(file
155 .getProject(), IPreferenceConstants.PHP_LOCALHOST_PREF)
158 return "http://localhost";