1 /**********************************************************************
 
   2  Copyright (c) 2000, 2002 IBM Corp. and others.
 
   3  All rights reserved. This program and the accompanying materials
 
   4  are made available under the terms of the Common Public License v1.0
 
   5  which accompanies this distribution, and is available at
 
   6  http://www.eclipse.org/legal/cpl-v10.html
 
   9  IBM Corporation - Initial implementation
 
  10  Klaus Hartlage - www.eclipseproject.de
 
  11  **********************************************************************/
 
  12 package net.sourceforge.phpeclipse.actions;
 
  13 import java.io.IOException;
 
  14 import java.net.MalformedURLException;
 
  16 import java.text.MessageFormat;
 
  17 import java.util.Iterator;
 
  19 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 
  20 import net.sourceforge.phpeclipse.ui.editor.ShowExternalPreviewAction;
 
  21 import net.sourceforge.phpeclipse.views.PHPConsole;
 
  22 import net.sourceforge.phpeclipse.webbrowser.views.BrowserView;
 
  24 import org.eclipse.core.resources.IFile;
 
  25 import org.eclipse.core.resources.IResource;
 
  26 import org.eclipse.jface.action.IAction;
 
  27 import org.eclipse.jface.dialogs.MessageDialog;
 
  28 import org.eclipse.jface.preference.IPreferenceStore;
 
  29 import org.eclipse.jface.viewers.ISelection;
 
  30 import org.eclipse.jface.viewers.ISelectionProvider;
 
  31 import org.eclipse.jface.viewers.StructuredSelection;
 
  32 import org.eclipse.swt.widgets.Shell;
 
  33 import org.eclipse.ui.IActionDelegate;
 
  34 import org.eclipse.ui.IObjectActionDelegate;
 
  35 import org.eclipse.ui.IViewPart;
 
  36 import org.eclipse.ui.IWorkbenchPage;
 
  37 import org.eclipse.ui.IWorkbenchPart;
 
  38 //import org.eclipse.update.internal.ui.UpdatePerspective;
 
  39 //import org.eclipse.update.internal.ui.views.IEmbeddedWebBrowser;
 
  40 public class PHPEclipseShowAction implements IObjectActionDelegate {
 
  41   private IWorkbenchPart workbenchPart;
 
  43    * Constructor for Action1.
 
  45   public PHPEclipseShowAction() {
 
  49    * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
 
  51   public void setActivePart(IAction action, IWorkbenchPart targetPart) {
 
  52     workbenchPart = targetPart;
 
  54   public void run(IAction action) {
 
  55     ISelectionProvider selectionProvider = null;
 
  56     selectionProvider = workbenchPart.getSite().getSelectionProvider();
 
  57     StructuredSelection selection = null;
 
  58     selection = (StructuredSelection) selectionProvider.getSelection();
 
  59     PHPConsole console = PHPConsole.getInstance();
 
  60     IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
 
  62     Iterator iterator = null;
 
  63     iterator = selection.iterator();
 
  64     while (iterator.hasNext()) {
 
  65       //  obj => selected object in the view
 
  66       Object obj = iterator.next();
 
  68       if (obj instanceof IResource) {
 
  69         IResource resource = (IResource) obj;
 
  70         // check if it's a file resource
 
  71         switch (resource.getType()) {
 
  74             IFile file = (IFile) resource;
 
  76             if ((localhostURL = ShowExternalPreviewAction.getLocalhostURL(store, (IFile) resource)) == null) {
 
  77               MessageDialog.openInformation(shell,
 
  78                   "Couldn't create localhost URL",
 
  79                   "Please configure your localhost and documentRoot");
 
  83               if (store.getBoolean(PHPeclipsePlugin.USE_EXTERNAL_BROWSER_PREF)) {
 
  84                 String[] arguments = {localhostURL};
 
  85                 MessageFormat form = new MessageFormat(store
 
  86                     .getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF));
 
  87                 Runtime runtime = Runtime.getRuntime();
 
  88                 String command = form.format(arguments);
 
  89                 console.write("External Browser command: " + command + "\n");
 
  90                 runtime.exec(command);
 
  91                 //                      runtime.exec(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF)
 
  93                 //                                                              runtime.exec("command.com /c start iexplore " + fileName);
 
  95                 //    MessageDialog.openInformation(shell, "localhostURL",
 
  96                 // "localhostURL: " + localhostURL);
 
  97                 //  this doesn't work under win98 ?
 
  98                 //     Program.launch(localhostURL);
 
  99                 console.write("Internal Browser URL: " + localhostURL + "\n");
 
 100                 open(new URL(localhostURL), shell, localhostURL);
 
 102             } catch (MalformedURLException e) {
 
 103               MessageDialog.openInformation(shell, "MalformedURLException: ", e
 
 105             } catch (IOException e) {
 
 106               MessageDialog.openInformation(shell, "IOException",
 
 107                   "Cannot show: " + localhostURL);
 
 114    * @see IActionDelegate#selectionChanged(IAction, ISelection)
 
 116   public void selectionChanged(IAction action, ISelection selection) {
 
 118 //  public static String getLocalhostURL(IPreferenceStore store, IFile file) {
 
 119 //    if (store == null) {
 
 120 //      store = PHPeclipsePlugin.getDefault().getPreferenceStore();
 
 122 //    // IPath path = file.getFullPath();
 
 123 //    String localhostURL = file.getLocation().toString();
 
 124 //    String lowerCaseFileName = localhostURL.toLowerCase();
 
 125 //  //  String documentRoot = store.getString(PHPeclipsePlugin.DOCUMENTROOT_PREF);
 
 126 //    String documentRoot = Util.getMiscProjectsPreferenceValue(
 
 127 //              file.getProject(), IPreferenceConstants.PHP_DOCUMENTROOT_PREF);
 
 129 //    documentRoot = documentRoot.replace('\\', '/');
 
 130 //    documentRoot = documentRoot.toLowerCase();
 
 132 //    if (lowerCaseFileName.startsWith(documentRoot)) {
 
 133 //      localhostURL = localhostURL.substring(documentRoot.length());
 
 137 ////    return store.getString(PHPeclipsePlugin.LOCALHOST_PREF) + localhostURL;
 
 138 //    return Util.getMiscProjectsPreferenceValue(file.getProject(), IPreferenceConstants.PHP_LOCALHOST_PREF) + localhostURL;
 
 141   public static void open(final URL url, final Shell shell,
 
 142       final String dialogTitle) {
 
 143     IWorkbenchPage page = PHPeclipsePlugin.getActivePage();
 
 145       IViewPart part = page.findView(BrowserView.ID_BROWSER);
 
 147         part = page.showView(BrowserView.ID_BROWSER);
 
 149         page.bringToTop(part);
 
 151       ((BrowserView) part).setUrl(url.toExternalForm());
 
 152     } catch (Exception e) {