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.io.IOException;
 
  12 import java.net.MalformedURLException;
 
  14 import java.text.MessageFormat;
 
  15 import java.util.Iterator;
 
  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;
 
  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;
 
  42 public class PHPEclipseShowAction implements IObjectActionDelegate {
 
  43   private IWorkbenchPart workbenchPart;
 
  46    * Constructor for Action1.
 
  48   public PHPEclipseShowAction() {
 
  53    * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
 
  55   public void setActivePart(IAction action, IWorkbenchPart targetPart) {
 
  56     workbenchPart = targetPart;
 
  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();
 
  66     Iterator iterator = null;
 
  67     iterator = selection.iterator();
 
  68     while (iterator.hasNext()) {
 
  69       //  obj => selected object in the view
 
  70       Object obj = iterator.next();
 
  72       if (obj instanceof IResource) {
 
  73         IResource resource = (IResource) obj;
 
  74         // check if it's a file resource
 
  75         switch (resource.getType()) {
 
  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);
 
  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");
 
  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);
 
 106               open(new URL(localhostURL), shell, localhostURL);
 
 108           } catch (MalformedURLException e) {
 
 109             MessageDialog.openInformation(shell, "MalformedURLException: ", e.toString());
 
 117    * @see IActionDelegate#selectionChanged(IAction, ISelection)
 
 119   public void selectionChanged(IAction action, ISelection selection) {
 
 122   public static void open(final URL url, final Shell shell, final String dialogTitle) {
 
 123 //    if (WebBrowserUtil.canUseInternalWebBrowser()) {
 
 124 //      IWorkbenchPage page = PHPeclipsePlugin.getActivePage();
 
 126 //        IViewPart part = page.findView(BrowserView.ID_BROWSER);
 
 127 //        if (part == null) {
 
 128 //          part = page.showView(BrowserView.ID_BROWSER);
 
 130 //          page.bringToTop(part);
 
 132 //        ((BrowserView) part).setUrl(url.toExternalForm());
 
 133 //      } catch (Exception e) {
 
 136       BrowserManager manager = BrowserManager.getInstance();
 
 137       IWebBrowser browser = manager.getCurrentWebBrowser();
 
 138       browser.openURL(url);