bugfix 1423269, http://sourceforge.net/tracker/index.php?func=detail&aid=1423269...
[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.net.MalformedURLException;
12 import java.net.URL;
13 import java.util.Iterator;
14
15 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
16 import net.sourceforge.phpeclipse.ui.IPreferenceConstants;
17 import net.sourceforge.phpeclipse.ui.editor.ShowExternalPreviewAction;
18 import net.sourceforge.phpeclipse.ui.overlaypages.ProjectPrefUtil;
19 import net.sourceforge.phpeclipse.webbrowser.IWebBrowser;
20 import net.sourceforge.phpeclipse.webbrowser.internal.BrowserManager;
21
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.IResource;
24 import org.eclipse.jface.action.IAction;
25 import org.eclipse.jface.dialogs.MessageDialog;
26 import org.eclipse.jface.preference.IPreferenceStore;
27 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.jface.viewers.ISelectionProvider;
29 import org.eclipse.jface.viewers.StructuredSelection;
30 import org.eclipse.swt.widgets.Shell;
31 import org.eclipse.ui.IActionDelegate;
32 import org.eclipse.ui.IObjectActionDelegate;
33 import org.eclipse.ui.IWorkbenchPart;
34
35
36 public class PHPEclipseShowAction implements IObjectActionDelegate {
37   private IWorkbenchPart workbenchPart;
38
39   /**
40    * Constructor for Action1.
41    */
42   public PHPEclipseShowAction() {
43     super();
44   }
45
46   /**
47    * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
48    */
49   public void setActivePart(IAction action, IWorkbenchPart targetPart) {
50     workbenchPart = targetPart;
51   }
52
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 = PHPeclipsePlugin.getDefault().getPreferenceStore();
59     Shell shell = null;
60     Iterator iterator = null;
61     iterator = selection.iterator();
62     while (iterator.hasNext()) {
63       //  obj => selected object in the view
64       Object obj = iterator.next();
65       // is it a resource
66       if (obj instanceof IResource) {
67         IResource resource = (IResource) obj;
68         // check if it's a file resource
69         switch (resource.getType()) {
70         case IResource.FILE:
71           // single file:
72           IFile previewFile = (IFile) resource;
73           String extension = previewFile.getFileExtension().toLowerCase();
74           boolean bringToTopPreview = ProjectPrefUtil.getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_BRING_TO_TOP_PREVIEW_DEFAULT);
75 //          boolean showHTMLFilesLocal = ProjectPrefUtil.getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_SHOW_HTML_FILES_LOCAL);
76 //          boolean showXMLFilesLocal = ProjectPrefUtil.getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_SHOW_XML_FILES_LOCAL);
77           boolean isHTMLFileName = "html".equals(extension) || "htm".equals(extension) || "xhtml".equals(extension);
78           boolean isXMLFileName = "xml".equals(extension) || "xsd".equals(extension) || "dtd".equals(extension);
79
80           String localhostURL;
81 //          if (showHTMLFilesLocal && isHTMLFileName) {
82 //            localhostURL = "file://"+previewFile.getLocation().toString();
83 //          } else if (showXMLFilesLocal && isXMLFileName) {
84 //            localhostURL = "file://"+previewFile.getLocation().toString();
85 //          } else
86           if ((localhostURL = ShowExternalPreviewAction.getLocalhostURL(store, previewFile)) == null) {
87             MessageDialog.openInformation(shell, "Couldn't create localhost URL",
88             "Please configure your localhost and documentRoot");
89             return;
90           }
91
92           try {
93 //            if (store.getBoolean(PHPeclipsePlugin.USE_EXTERNAL_BROWSER_PREF)) {
94 //              String[] arguments = { localhostURL };
95 //              MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF));
96 //              Runtime runtime = Runtime.getRuntime();
97 //              String command = form.format(arguments);
98 //              //                console.write("External Browser command: " + command + "\n");
99 //              runtime.exec(command);
100 //            } else {
101               open(new URL(localhostURL), shell, localhostURL);
102 //            }
103           } catch (MalformedURLException e) {
104             MessageDialog.openInformation(shell, "MalformedURLException: ", e.toString());
105           }
106         }
107       }
108     }
109   }
110
111   /**
112    * @see IActionDelegate#selectionChanged(IAction, ISelection)
113    */
114   public void selectionChanged(IAction action, ISelection selection) {
115   }
116
117   public static void open(final URL url, final Shell shell, final String dialogTitle) {
118 //    if (WebBrowserUtil.canUseInternalWebBrowser()) {
119 //      IWorkbenchPage page = PHPeclipsePlugin.getActivePage();
120 //      try {
121 //        IViewPart part = page.findView(BrowserView.ID_BROWSER);
122 //        if (part == null) {
123 //          part = page.showView(BrowserView.ID_BROWSER);
124 //        } else {
125 //          page.bringToTop(part);
126 //        }
127 //        ((BrowserView) part).setUrl(url.toExternalForm());
128 //      } catch (Exception e) {
129 //      }
130 //    } else {
131       BrowserManager manager = BrowserManager.getInstance();
132       IWebBrowser browser = manager.getCurrentWebBrowser();
133       browser.openURL(url);
134 //    }
135   }
136 }