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
 
   6  * Contributors: IBM Corporation - Initial implementation
 
   8  **********************************************************************************************************************************/
 
   9 package net.sourceforge.phpeclipse.actions;
 
  11 import java.net.MalformedURLException;
 
  13 import java.util.Iterator;
 
  15 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 
  16 import net.sourceforge.phpeclipse.ui.IPreferenceConstants;
 
  17 import net.sourceforge.phpeclipse.ui.WebUI;
 
  18 import net.sourceforge.phpeclipse.ui.editor.ShowExternalPreviewAction;
 
  19 import net.sourceforge.phpeclipse.ui.overlaypages.ProjectPrefUtil;
 
  20 import net.sourceforge.phpeclipse.webbrowser.IWebBrowser;
 
  21 import net.sourceforge.phpeclipse.webbrowser.internal.BrowserManager;
 
  23 import org.eclipse.core.resources.IFile;
 
  24 import org.eclipse.core.resources.IResource;
 
  25 import org.eclipse.jface.action.IAction;
 
  26 import org.eclipse.jface.dialogs.MessageDialog;
 
  27 import org.eclipse.jface.preference.IPreferenceStore;
 
  28 import org.eclipse.jface.viewers.ISelection;
 
  29 import org.eclipse.jface.viewers.ISelectionProvider;
 
  30 import org.eclipse.jface.viewers.StructuredSelection;
 
  31 import org.eclipse.swt.widgets.Shell;
 
  32 import org.eclipse.ui.IActionDelegate;
 
  33 import org.eclipse.ui.IObjectActionDelegate;
 
  34 import org.eclipse.ui.IWorkbenchPart;
 
  36 public class PHPEclipseShowAction implements IObjectActionDelegate {
 
  37         private IWorkbenchPart workbenchPart;
 
  40          * Constructor for Action1.
 
  42         public PHPEclipseShowAction() {
 
  47          * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
 
  49         public void setActivePart(IAction action, IWorkbenchPart targetPart) {
 
  50                 workbenchPart = targetPart;
 
  53         public void run(IAction action) {
 
  54                 ISelectionProvider selectionProvider = null;
 
  55                 selectionProvider = workbenchPart.getSite().getSelectionProvider();
 
  56                 StructuredSelection selection = null;
 
  57                 selection = (StructuredSelection) selectionProvider.getSelection();
 
  58                 IPreferenceStore store = WebUI.getDefault()
 
  59                                 .getPreferenceStore();
 
  61                 Iterator iterator = null;
 
  62                 iterator = selection.iterator();
 
  63                 while (iterator.hasNext()) {
 
  64                         // obj => selected object in the view
 
  65                         Object obj = iterator.next();
 
  67                         if (obj instanceof IResource) {
 
  68                                 IResource resource = (IResource) obj;
 
  69                                 // check if it's a file resource
 
  70                                 switch (resource.getType()) {
 
  73                                         IFile previewFile = (IFile) resource;
 
  74                                         String extension = previewFile.getFileExtension()
 
  76                                         boolean bringToTopPreview = ProjectPrefUtil
 
  77                                                         .getPreviewBooleanValue(
 
  79                                                                         IPreferenceConstants.PHP_BRING_TO_TOP_PREVIEW_DEFAULT);
 
  80                                         // boolean showHTMLFilesLocal =
 
  81                                         // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
 
  82                                         // IPreferenceConstants.PHP_SHOW_HTML_FILES_LOCAL);
 
  83                                         // boolean showXMLFilesLocal =
 
  84                                         // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
 
  85                                         // IPreferenceConstants.PHP_SHOW_XML_FILES_LOCAL);
 
  86                                         boolean isHTMLFileName = "html".equals(extension)
 
  87                                                         || "htm".equals(extension)
 
  88                                                         || "xhtml".equals(extension);
 
  89                                         boolean isXMLFileName = "xml".equals(extension)
 
  90                                                         || "xsd".equals(extension)
 
  91                                                         || "dtd".equals(extension);
 
  94                                         // if (showHTMLFilesLocal && isHTMLFileName) {
 
  96                                         // "file://"+previewFile.getLocation().toString();
 
  97                                         // } else if (showXMLFilesLocal && isXMLFileName) {
 
  99                                         // "file://"+previewFile.getLocation().toString();
 
 101                                         if ((localhostURL = ShowExternalPreviewAction
 
 102                                                         .getLocalhostURL(store, previewFile)) == null) {
 
 104                                                                 .openInformation(shell,
 
 105                                                                                 "Couldn't create localhost URL",
 
 106                                                                                 "Please configure your localhost and documentRoot");
 
 112                                                 // (store.getBoolean(PHPeclipsePlugin.USE_EXTERNAL_BROWSER_PREF))
 
 114                                                 // String[] arguments = { localhostURL };
 
 115                                                 // MessageFormat form = new
 
 116                                                 // MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF));
 
 117                                                 // Runtime runtime = Runtime.getRuntime();
 
 118                                                 // String command = form.format(arguments);
 
 119                                                 // // console.write("External Browser command: " +
 
 121                                                 // runtime.exec(command);
 
 123                                                 open(new URL(localhostURL), shell, localhostURL);
 
 125                                         } catch (MalformedURLException e) {
 
 126                                                 MessageDialog.openInformation(shell,
 
 127                                                                 "MalformedURLException: ", e.toString());
 
 135          * @see IActionDelegate#selectionChanged(IAction, ISelection)
 
 137         public void selectionChanged(IAction action, ISelection selection) {
 
 140         public static void open(final URL url, final Shell shell,
 
 141                         final String dialogTitle) {
 
 142                 // if (WebBrowserUtil.canUseInternalWebBrowser()) {
 
 143                 // IWorkbenchPage page = PHPeclipsePlugin.getActivePage();
 
 145                 // IViewPart part = page.findView(BrowserView.ID_BROWSER);
 
 146                 // if (part == null) {
 
 147                 // part = page.showView(BrowserView.ID_BROWSER);
 
 149                 // page.bringToTop(part);
 
 151                 // ((BrowserView) part).setUrl(url.toExternalForm());
 
 152                 // } catch (Exception e) {
 
 155                 BrowserManager manager = BrowserManager.getInstance();
 
 156                 IWebBrowser browser = manager.getCurrentWebBrowser();
 
 157                 browser.openURL(url);