RC2 compatibility
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / ShowExternalPreviewAction.java
1 package net.sourceforge.phpeclipse.phpeditor;
2 /*******************************************************************************
3  * Copyright (c) 2000, 2002 IBM Corp. and others. All rights reserved. This
4  * program and the accompanying materials are made available under the terms of
5  * the Common Public License v1.0 which accompanies this distribution, and is
6  * available at http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors: IBM Corporation - Initial implementation Klaus Hartlage -
9  * www.eclipseproject.de
10  ******************************************************************************/
11 import net.sourceforge.phpeclipse.IPreferenceConstants;
12 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
13 import net.sourceforge.phpeclipse.actions.PHPEclipseShowAction;
14 import net.sourceforge.phpeclipse.overlaypages.Util;
15 import net.sourceforge.phpeclipse.views.browser.BrowserView;
16
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.ui.IEditorInput;
19 import org.eclipse.ui.IFileEditorInput;
20 import org.eclipse.ui.IViewPart;
21 import org.eclipse.ui.IWorkbenchPage;
22 import org.eclipse.ui.PartInitException;
23 import org.eclipse.ui.texteditor.ITextEditor;
24 import org.eclipse.ui.texteditor.TextEditorAction;
25 //import org.eclipse.update.internal.ui.UpdatePerspective;
26 //import org.eclipse.update.internal.ui.views.IEmbeddedWebBrowser;
27 /**
28  * ClassDeclaration that defines the action for parsing the current PHP file
29  */
30 public class ShowExternalPreviewAction extends TextEditorAction {
31   private static ShowExternalPreviewAction instance = new ShowExternalPreviewAction();
32   /**
33    * Constructs and updates the action.
34    */
35   private ShowExternalPreviewAction() {
36     super(PHPEditorMessages.getResourceBundle(), "ParserAction.", null); //$NON-NLS-1$
37     update();
38   }
39   
40 public static ShowExternalPreviewAction getInstance() {
41     return instance;
42   }
43   /**
44    * Code called when the action is fired.
45    */
46   public void run() {
47     IFile fileToParse = getFile();
48     if (fileToParse == null) {
49       // should never happen
50       return;
51     }
52     boolean autoPreview = Util.getPreviewBooleanValue(fileToParse,
53         IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
54     boolean bringToTopPreview = Util.getPreviewBooleanValue(fileToParse,
55         IPreferenceConstants.PHP_BRING_TO_TOP_PREVIEW_DEFAULT);
56     if (autoPreview) {
57       String localhostURL;
58       if ((localhostURL = PHPEclipseShowAction.getLocalhostURL(null,
59           fileToParse)) == null) {
60         return;
61       }
62       IWorkbenchPage page = PHPeclipsePlugin.getActivePage();
63       try {
64         IViewPart part = page.findView(BrowserView.ID_BROWSER);
65         if (part == null) {
66           part = page.showView(BrowserView.ID_BROWSER);
67         } else {
68           if (bringToTopPreview) {
69             page.bringToTop(part);
70           }
71         }
72         ((BrowserView) part).setUrl(localhostURL);
73         
74       } catch (PartInitException e) {
75         PHPeclipsePlugin.log(e);
76       }
77     }
78   }
79   public void refresh() {
80     IFile fileToParse = getFile();
81     if (fileToParse == null) {
82       // should never happen
83       return;
84     }
85     boolean autoPreview = Util.getPreviewBooleanValue(fileToParse,
86         IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
87     boolean bringToTopPreview = Util.getPreviewBooleanValue(fileToParse,
88         IPreferenceConstants.PHP_BRING_TO_TOP_PREVIEW_DEFAULT);
89     if (autoPreview) {
90       IWorkbenchPage page = PHPeclipsePlugin.getActivePage();
91       try {
92         IViewPart part = page.findView(BrowserView.ID_BROWSER);
93         if (part == null) {
94           part = page.showView(BrowserView.ID_BROWSER);
95         } else {
96           if (bringToTopPreview) {
97             page.bringToTop(part);
98           }
99         }
100         ((BrowserView) part).refresh();
101         
102       } catch (PartInitException e) {
103         PHPeclipsePlugin.log(e);
104       }
105     }
106   }
107   /**
108    * Finds the file that's currently opened in the PHP Text Editor
109    */
110   protected IFile getFile() {
111     ITextEditor editor = getTextEditor();
112     IEditorInput editorInput = null;
113     if (editor != null) {
114       editorInput = editor.getEditorInput();
115     }
116     if (editorInput instanceof IFileEditorInput)
117       return ((IFileEditorInput) editorInput).getFile();
118     // if nothing was found, which should never happen
119     return null;
120   }
121 }