misc changes
[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   public static ShowExternalPreviewAction getInstance() {
39     return instance;
40   }
41   /**
42    * Code called when the action is fired.
43    */
44   public void run() {
45     IFile fileToParse = getFile();
46     if (fileToParse == null) {
47       // should never happen
48       return;
49     }
50     boolean autoPreview = Util.getPreviewBooleanValue(fileToParse,
51         IPreferenceConstants.PHP_AUTO_PREVIEW_DEFAULT);
52     boolean bringToTopPreview = Util.getPreviewBooleanValue(fileToParse,
53         IPreferenceConstants.PHP_BRING_TO_TOP_PREVIEW_DEFAULT);
54     if (autoPreview) {
55       String localhostURL;
56       if ((localhostURL = PHPEclipseShowAction.getLocalhostURL(null,
57           fileToParse)) == null) {
58         return;
59       }
60       IWorkbenchPage page = PHPeclipsePlugin.getActivePage();
61       try {
62         IViewPart part = page.findView(BrowserView.ID_BROWSER);
63         if (part == null) {
64           part = page.showView(BrowserView.ID_BROWSER);
65         } else {
66           if (bringToTopPreview) {
67             page.bringToTop(part);
68           }
69         }
70         ((BrowserView) part).setUrl(localhostURL);
71       } catch (PartInitException e) {
72         PHPeclipsePlugin.log(e);
73       }
74     }
75   }
76   /**
77    * Finds the file that's currently opened in the PHP Text Editor
78    */
79   protected IFile getFile() {
80     ITextEditor editor = getTextEditor();
81     IEditorInput editorInput = null;
82     if (editor != null) {
83       editorInput = editor.getEditorInput();
84     }
85     if (editorInput instanceof IFileEditorInput)
86       return ((IFileEditorInput) editorInput).getFile();
87     // if nothing was found, which should never happen
88     return null;
89   }
90 }