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