#1365431 (toshihiro) - PHP Editor crashes Windows with JavaScript 'windows.close()'
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / ui / editor / ShowExternalPreviewAction.java
index bef108a..abe117e 100644 (file)
@@ -19,102 +19,111 @@ import org.eclipse.ui.texteditor.TextEditorAction;
  * ClassDeclaration that defines the action for parsing the current PHP file
  */
 public class ShowExternalPreviewAction extends TextEditorAction {
-  public final static int XML_TYPE = 1;
+       public final static int XML_TYPE = 1;
 
-  public final static int HTML_TYPE = 2;
+       public final static int HTML_TYPE = 2;
 
-  public final static int SMARTY_TYPE = 3;
+       public final static int SMARTY_TYPE = 3;
 
-  public final static int PHP_TYPE = 4;
+       public final static int PHP_TYPE = 4;
 
-  private static ShowExternalPreviewAction instance = new ShowExternalPreviewAction();
+       private static ShowExternalPreviewAction instance = new ShowExternalPreviewAction();
 
-  /**
-   * Constructs and updates the action.
-   */
-  private ShowExternalPreviewAction() {
-    super(EditorMessages.getResourceBundle(), "ParserAction.", null); //$NON-NLS-1$
-    update();
-  }
+       /**
+        * Constructs and updates the action.
+        */
+       private ShowExternalPreviewAction() {
+               super(EditorMessages.getResourceBundle(), "ParserAction.", null); //$NON-NLS-1$
+               update();
+       }
 
-  public static ShowExternalPreviewAction getInstance() {
-    return instance;
-  }
+       public static ShowExternalPreviewAction getInstance() {
+               return instance;
+       }
 
-  /**
-   * Code called when the action is fired.
-   */
-  public void run() {
-    doRun(PHP_TYPE);
-  }
+       /**
+        * Code called when the action is fired.
+        */
+       public void run() {
+               doRun(PHP_TYPE);
+       }
 
-  public void doRun(int type) {
-    IFile previewFile = getFile();
-    BrowserUtil.showPreview(previewFile, false, "");
-  }
-  
-  public void refresh(int type) {
-    IFile fileToParse = getFile();
-    if (fileToParse == null) {
-      // should never happen
-      return;
-    }
-    boolean autoPreview = ProjectPrefUtil.getPreviewBooleanValue(fileToParse, IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
-    boolean bringToTopPreview = ProjectPrefUtil.getPreviewBooleanValue(fileToParse,
-        IPreferenceConstants.PHP_BRING_TO_TOP_PREVIEW_DEFAULT);
+       public void doRun(int type) {
+               IFile previewFile = getFile();
+               BrowserUtil.showPreview(previewFile, false, "");
+       }
 
-    if (autoPreview) {
-      IWorkbenchPage page = WebUI.getDefault().getActivePage();
-      try {
-        IViewPart part = page.findView(BrowserView.ID_BROWSER);
-        if (part == null) {
-          part = page.showView(BrowserView.ID_BROWSER);
-        } else {
-          if (bringToTopPreview) {
-            page.bringToTop(part);
-          }
-        }
-        if (part != null) {
-          ((BrowserView) part).refresh();
-        }
-      } catch (Exception e) {
-        //  PHPeclipsePlugin.log(e);
-      }
-    }
-  }
+       public void refresh(int type) {
+               IFile fileToParse = getFile();
+               if (fileToParse == null) {
+                       // should never happen
+                       return;
+               }
+               boolean autoPreview = ProjectPrefUtil.getPreviewBooleanValue(
+                               fileToParse, IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
+               boolean bringToTopPreview = ProjectPrefUtil.getPreviewBooleanValue(
+                               fileToParse,
+                               IPreferenceConstants.PHP_BRING_TO_TOP_PREVIEW_DEFAULT);
 
-  /**
-   * Finds the file that's currently opened in the PHP Text Editor
-   */
-  protected IFile getFile() {
-    ITextEditor editor = getTextEditor();
-    IEditorInput editorInput = null;
-    if (editor != null) {
-      editorInput = editor.getEditorInput();
-    }
-    if (editorInput instanceof IFileEditorInput)
-      return ((IFileEditorInput) editorInput).getFile();
-    // if nothing was found, which should never happen
-    return null;
-  }
+               if (autoPreview) {
+                       IWorkbenchPage page = WebUI.getActivePage();
+                       try {
+                               IViewPart part = page.findView(BrowserView.ID_BROWSER);
+                               if (part == null) {
+                                       part = page.showView(BrowserView.ID_BROWSER);
+                               } else {
+                                       if (bringToTopPreview) {
+                                               page.bringToTop(part);
+                                       }
+                               }
+                               if (part != null) {
+                                       ((BrowserView) part).refresh();
+                               }
+                       } catch (Exception e) {
+                               // PHPeclipsePlugin.log(e);
+                       }
+               }
+       }
 
-  public static String getLocalhostURL(IPreferenceStore store, IFile file) {
-    if (store == null) {
-      store = WebUI.getDefault().getPreferenceStore();
-    }
-    // IPath path = file.getFullPath();
-    String localhostURL = file.getLocation().toString();
-    String lowerCaseFileName = localhostURL.toLowerCase();
-    //  String documentRoot = store.getString(PHPeclipsePlugin.DOCUMENTROOT_PREF);
-    IPath documentRootPath = ProjectPrefUtil.getDocumentRoot(file.getProject());
-    String documentRoot = documentRootPath.toString().toLowerCase();
-    if (lowerCaseFileName.startsWith(documentRoot)) {
-      localhostURL = localhostURL.substring(documentRoot.length());
-    } else {
-      return null;
-    }
-    //    return store.getString(PHPeclipsePlugin.LOCALHOST_PREF) + localhostURL;
-    return ProjectPrefUtil.getMiscProjectsPreferenceValue(file.getProject(), IPreferenceConstants.PHP_LOCALHOST_PREF)
-        + localhostURL;
-  }
+       /**
+        * Finds the file that's currently opened in the PHP Text Editor
+        */
+       protected IFile getFile() {
+               ITextEditor editor = getTextEditor();
+               IEditorInput editorInput = null;
+               if (editor != null) {
+                       editorInput = editor.getEditorInput();
+               }
+               if (editorInput instanceof IFileEditorInput)
+                       return ((IFileEditorInput) editorInput).getFile();
+               // if nothing was found, which should never happen
+               return null;
+       }
+
+       public static String getLocalhostURL(IPreferenceStore store, IFile file) {
+               if (file != null) {
+                       if (store == null) {
+                               store = WebUI.getDefault().getPreferenceStore();
+                       }
+                       // IPath path = file.getFullPath();
+                       String localhostURL = file.getLocation().toString();
+                       String lowerCaseFileName = localhostURL.toLowerCase();
+                       // String documentRoot =
+                       // store.getString(PHPeclipsePlugin.DOCUMENTROOT_PREF);
+                       IPath documentRootPath = ProjectPrefUtil.getDocumentRoot(file
+                                       .getProject());
+                       String documentRoot = documentRootPath.toString().toLowerCase();
+                       if (lowerCaseFileName.startsWith(documentRoot)) {
+                               localhostURL = localhostURL.substring(documentRoot.length());
+                       } else {
+                               return null;
+                       }
+                       // return store.getString(PHPeclipsePlugin.LOCALHOST_PREF) +
+                       // localhostURL;
+                       return ProjectPrefUtil.getMiscProjectsPreferenceValue(file
+                                       .getProject(), IPreferenceConstants.PHP_LOCALHOST_PREF)
+                                       + localhostURL;
+               }
+               return "http://localhost";
+       }
 }
\ No newline at end of file