improved PHP parser
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / actions / PHPEclipseShowAction.java
1 /***********************************************************************************************************************************
2  * Copyright (c) 2000, 2002 IBM Corp. and others. All rights reserved. This program and the accompanying materials are made
3  * available under the terms of the Common Public License v1.0 which accompanies this distribution, and is available at
4  * http://www.eclipse.org/legal/cpl-v10.html
5  * 
6  * Contributors: IBM Corporation - Initial implementation 
7  *               www.phpeclipse.de
8  **********************************************************************************************************************************/
9 package net.sourceforge.phpeclipse.actions;
10
11 import java.io.IOException;
12 import java.net.MalformedURLException;
13 import java.net.URL;
14 import java.text.MessageFormat;
15 import java.util.Iterator;
16
17 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
18 import net.sourceforge.phpeclipse.ui.IPreferenceConstants;
19 import net.sourceforge.phpeclipse.ui.editor.ShowExternalPreviewAction;
20 import net.sourceforge.phpeclipse.ui.overlaypages.ProjectPrefUtil;
21 import net.sourceforge.phpeclipse.webbrowser.IWebBrowser;
22 import net.sourceforge.phpeclipse.webbrowser.internal.BrowserManager;
23 import net.sourceforge.phpeclipse.webbrowser.internal.WebBrowserUtil;
24 import net.sourceforge.phpeclipse.webbrowser.views.BrowserView;
25
26 import org.eclipse.core.resources.IFile;
27 import org.eclipse.core.resources.IResource;
28 import org.eclipse.jface.action.IAction;
29 import org.eclipse.jface.dialogs.MessageDialog;
30 import org.eclipse.jface.preference.IPreferenceStore;
31 import org.eclipse.jface.viewers.ISelection;
32 import org.eclipse.jface.viewers.ISelectionProvider;
33 import org.eclipse.jface.viewers.StructuredSelection;
34 import org.eclipse.swt.widgets.Shell;
35 import org.eclipse.ui.IActionDelegate;
36 import org.eclipse.ui.IObjectActionDelegate;
37 import org.eclipse.ui.IViewPart;
38 import org.eclipse.ui.IWorkbenchPage;
39 import org.eclipse.ui.IWorkbenchPart;
40
41
42 public class PHPEclipseShowAction implements IObjectActionDelegate {
43   private IWorkbenchPart workbenchPart;
44
45   /**
46    * Constructor for Action1.
47    */
48   public PHPEclipseShowAction() {
49     super();
50   }
51
52   /**
53    * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
54    */
55   public void setActivePart(IAction action, IWorkbenchPart targetPart) {
56     workbenchPart = targetPart;
57   }
58
59   public void run(IAction action) {
60     ISelectionProvider selectionProvider = null;
61     selectionProvider = workbenchPart.getSite().getSelectionProvider();
62     StructuredSelection selection = null;
63     selection = (StructuredSelection) selectionProvider.getSelection();
64     IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
65     Shell shell = null;
66     Iterator iterator = null;
67     iterator = selection.iterator();
68     while (iterator.hasNext()) {
69       //  obj => selected object in the view
70       Object obj = iterator.next();
71       // is it a resource
72       if (obj instanceof IResource) {
73         IResource resource = (IResource) obj;
74         // check if it's a file resource
75         switch (resource.getType()) {
76         case IResource.FILE:
77           // single file:
78           IFile previewFile = (IFile) resource;
79           String extension = previewFile.getFileExtension().toLowerCase();
80           boolean bringToTopPreview = ProjectPrefUtil.getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_BRING_TO_TOP_PREVIEW_DEFAULT);
81           boolean showHTMLFilesLocal = ProjectPrefUtil.getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_SHOW_HTML_FILES_LOCAL);
82           boolean showXMLFilesLocal = ProjectPrefUtil.getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_SHOW_XML_FILES_LOCAL);
83           boolean isHTMLFileName = "html".equals(extension) || "htm".equals(extension) || "xhtml".equals(extension);
84           boolean isXMLFileName = "xml".equals(extension) || "xsd".equals(extension) || "dtd".equals(extension);
85
86           String localhostURL;
87           if (showHTMLFilesLocal && isHTMLFileName) {
88             localhostURL = "file://"+previewFile.getLocation().toString();
89           } else if (showXMLFilesLocal && isXMLFileName) {
90             localhostURL = "file://"+previewFile.getLocation().toString();
91           } else if ((localhostURL = ShowExternalPreviewAction.getLocalhostURL(store, previewFile)) == null) {
92             MessageDialog.openInformation(shell, "Couldn't create localhost URL",
93             "Please configure your localhost and documentRoot");
94             return;
95           }
96         
97           try {
98 //            if (store.getBoolean(PHPeclipsePlugin.USE_EXTERNAL_BROWSER_PREF)) {
99 //              String[] arguments = { localhostURL };
100 //              MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF));
101 //              Runtime runtime = Runtime.getRuntime();
102 //              String command = form.format(arguments);
103 //              //                console.write("External Browser command: " + command + "\n");
104 //              runtime.exec(command);
105 //            } else {
106               open(new URL(localhostURL), shell, localhostURL);
107 //            }
108           } catch (MalformedURLException e) {
109             MessageDialog.openInformation(shell, "MalformedURLException: ", e.toString());
110           }
111         }
112       }
113     }
114   }
115
116   /**
117    * @see IActionDelegate#selectionChanged(IAction, ISelection)
118    */
119   public void selectionChanged(IAction action, ISelection selection) {
120   }
121
122   public static void open(final URL url, final Shell shell, final String dialogTitle) {
123 //    if (WebBrowserUtil.canUseInternalWebBrowser()) {
124 //      IWorkbenchPage page = PHPeclipsePlugin.getActivePage();
125 //      try {
126 //        IViewPart part = page.findView(BrowserView.ID_BROWSER);
127 //        if (part == null) {
128 //          part = page.showView(BrowserView.ID_BROWSER);
129 //        } else {
130 //          page.bringToTop(part);
131 //        }
132 //        ((BrowserView) part).setUrl(url.toExternalForm());
133 //      } catch (Exception e) {
134 //      }
135 //    } else {
136       BrowserManager manager = BrowserManager.getInstance();
137       IWebBrowser browser = manager.getCurrentWebBrowser();
138       browser.openURL(url);
139 //    }
140   }
141 }