Eliminated unused classes
[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.views.browser.BrowserView;
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.ui.IEditorInput;
17 import org.eclipse.ui.IFileEditorInput;
18 import org.eclipse.ui.IViewPart;
19 import org.eclipse.ui.IWorkbenchPage;
20 import org.eclipse.ui.PartInitException;
21 import org.eclipse.ui.texteditor.ITextEditor;
22 import org.eclipse.ui.texteditor.TextEditorAction;
23 import net.sourceforge.phpeclipse.overlaypages.Util;
24 //import org.eclipse.update.internal.ui.UpdatePerspective;
25 //import org.eclipse.update.internal.ui.views.IEmbeddedWebBrowser;
26 /**
27  * ClassDeclaration that defines the action for parsing the current PHP file
28  */
29 public class ShowExternalPreviewAction extends TextEditorAction {
30   private static ShowExternalPreviewAction instance = new ShowExternalPreviewAction();
31   /**
32    * Constructs and updates the action.
33    */
34   private ShowExternalPreviewAction() {
35     super(PHPEditorMessages.getResourceBundle(), "ParserAction.", null); //$NON-NLS-1$
36     update();
37   }
38   
39 public static ShowExternalPreviewAction getInstance() {
40     return instance;
41   }
42   /**
43    * Code called when the action is fired.
44    */
45   public void run() {
46     IFile fileToParse = getFile();
47     if (fileToParse == null) {
48       // should never happen
49       return;
50     }
51     boolean autoPreview = Util.getPreviewBooleanValue(fileToParse,
52         IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
53     boolean bringToTopPreview = Util.getPreviewBooleanValue(fileToParse,
54         IPreferenceConstants.PHP_BRING_TO_TOP_PREVIEW_DEFAULT);
55     if (autoPreview) {
56       String localhostURL;
57       if ((localhostURL = PHPEclipseShowAction.getLocalhostURL(null,
58           fileToParse)) == null) {
59         return;
60       }
61       IWorkbenchPage page = PHPeclipsePlugin.getActivePage();
62       try {
63         IViewPart part = page.findView(BrowserView.ID_BROWSER);
64         if (part == null) {
65           part = page.showView(BrowserView.ID_BROWSER);
66         } else {
67           if (bringToTopPreview) {
68             page.bringToTop(part);
69           }
70         }
71         ((BrowserView) part).setUrl(localhostURL);
72         
73       } catch (PartInitException e) {
74         PHPeclipsePlugin.log(e);
75       }
76     }
77   }
78   public void refresh() {
79     IFile fileToParse = getFile();
80     if (fileToParse == null) {
81       // should never happen
82       return;
83     }
84     boolean autoPreview = Util.getPreviewBooleanValue(fileToParse,
85         IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
86     boolean bringToTopPreview = Util.getPreviewBooleanValue(fileToParse,
87         IPreferenceConstants.PHP_BRING_TO_TOP_PREVIEW_DEFAULT);
88     if (autoPreview) {
89       IWorkbenchPage page = PHPeclipsePlugin.getActivePage();
90       try {
91         IViewPart part = page.findView(BrowserView.ID_BROWSER);
92         if (part == null) {
93           part = page.showView(BrowserView.ID_BROWSER);
94         } else {
95           if (bringToTopPreview) {
96             page.bringToTop(part);
97           }
98         }
99         ((BrowserView) part).refresh();
100         
101       } catch (PartInitException e) {
102         PHPeclipsePlugin.log(e);
103       }
104     }
105   }
106   /**
107    * Finds the file that's currently opened in the PHP Text Editor
108    */
109   protected IFile getFile() {
110     ITextEditor editor = getTextEditor();
111     IEditorInput editorInput = null;
112     if (editor != null) {
113       editorInput = editor.getEditorInput();
114     }
115     if (editorInput instanceof IFileEditorInput)
116       return ((IFileEditorInput) editorInput).getFile();
117     // if nothing was found, which should never happen
118     return null;
119   }
120 }