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);
 
  72                 boolean stickyBrowserURL = ProjectPrefUtil.getPreviewBooleanValue(
 
  74                                 IPreferenceConstants.PHP_STICKY_BROWSER_URL_DEFAULT);
 
  77                         IWorkbenchPage page = WebUI.getActivePage();
 
  83                                 // IViewPart part = page.findView(BrowserView.ID_BROWSER);
 
  84                                 // if (part == null) {
 
  85                                 // part = page.showView(BrowserView.ID_BROWSER);
 
  87                                 IViewPart part = page.showView(BrowserView.ID_BROWSER, null,
 
  88                                                 IWorkbenchPage.VIEW_CREATE);
 
  90                                         if (bringToTopPreview) {
 
  91                                                 // page.bringToTop(part);
 
  92                                                 new WorkbenchJob(getClass().getName()) {
 
  93                                                         public IStatus runInUIThread(
 
  94                                                                         IProgressMonitor monitor) {
 
  95                                                                 IWorkbenchPage page = WebUI.getActivePage();
 
  98                                                                                         .findView(BrowserView.ID_BROWSER);
 
 100                                                                                 page.bringToTop(part);
 
 103                                                                 return Status.OK_STATUS;
 
 107                                         // ((BrowserView) part).refresh();
 
 109                                                         && ((BrowserView) part).getUrl() != null
 
 110                                                         && ((BrowserView) part).getUrl().length() > 0) {
 
 111                                                 ((BrowserView) part).refresh();
 
 113                                                 String localhostURL = getLocalhostURL(null, previewFile);
 
 114                                                 ((BrowserView) part).refresh(localhostURL);
 
 117                         } catch (PartInitException e) {
 
 119                                 WebUI.getDefault().getLog().log(
 
 120                                                 new Status(IStatus.ERROR,
 
 121                                                                 "net.sourceforge.phpeclipse.ui", IStatus.OK,
 
 122                                                                 "Failed to show Browser View", e));
 
 123                                 // PHPeclipsePlugin.log(e);
 
 129          * Finds the file that's currently opened in the PHP Text Editor
 
 131         protected IFile getFile() {
 
 132                 ITextEditor editor = getTextEditor();
 
 133                 IEditorInput editorInput = null;
 
 134                 if (editor != null) {
 
 135                         editorInput = editor.getEditorInput();
 
 137                 if (editorInput instanceof IFileEditorInput)
 
 138                         return ((IFileEditorInput) editorInput).getFile();
 
 139                 // if nothing was found, which should never happen
 
 143         public static String getLocalhostURL(IPreferenceStore store, IFile file) {
 
 146                                 store = WebUI.getDefault().getPreferenceStore();
 
 148                         // IPath path = file.getFullPath();
 
 149                         String localhostURL = file.getFullPath().toString();
 
 150                         String lowerCaseFileName = localhostURL.toLowerCase();
 
 151                         //removed by ed_mann for RSE fixes testing
 
 152                         // String documentRoot =
 
 153                         // store.getString(PHPeclipsePlugin.DOCUMENTROOT_PREF);
 
 154                         //IPath documentRootPath = ProjectPrefUtil.getDocumentRoot(file
 
 156                         IPath documentRootPath = file.getProject().getFullPath();
 
 157                         String documentRoot = documentRootPath.toString().toLowerCase();
 
 158                         if (lowerCaseFileName.startsWith(documentRoot)) {
 
 159                                 localhostURL = localhostURL.substring(documentRoot.length());
 
 163                         // return store.getString(PHPeclipsePlugin.LOCALHOST_PREF) +
 
 165                         String projectPath = ProjectPrefUtil.getMiscProjectsPreferenceValue(file
 
 166                     .getProject(), IPreferenceConstants.PHP_LOCALHOST_PREF);
 
 167                         if(projectPath.endsWith("/") && localhostURL.startsWith("/")) {
 
 168                             localhostURL = localhostURL.substring(1);
 
 170                         return projectPath + localhostURL;
 
 172                 return "http://localhost";