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 // PHPConsole console = PHPConsole.getInstance();
65 IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
67 Iterator iterator = null;
68 iterator = selection.iterator();
69 while (iterator.hasNext()) {
70 // obj => selected object in the view
71 Object obj = iterator.next();
73 if (obj instanceof IResource) {
74 IResource resource = (IResource) obj;
75 // check if it's a file resource
76 switch (resource.getType()) {
79 IFile previewFile = (IFile) resource;
80 String extension = previewFile.getFileExtension().toLowerCase();
81 boolean bringToTopPreview = ProjectPrefUtil.getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_BRING_TO_TOP_PREVIEW_DEFAULT);
82 boolean showHTMLFilesLocal = ProjectPrefUtil.getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_SHOW_HTML_FILES_LOCAL);
83 boolean showXMLFilesLocal = ProjectPrefUtil.getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_SHOW_XML_FILES_LOCAL);
84 boolean isHTMLFileName = "html".equals(extension) || "htm".equals(extension) || "xhtml".equals(extension);
85 boolean isXMLFileName = "xml".equals(extension) || "xsd".equals(extension) || "dtd".equals(extension);
88 if (showHTMLFilesLocal && isHTMLFileName) {
89 localhostURL = "file://"+previewFile.getLocation().toString();
90 } else if (showXMLFilesLocal && isXMLFileName) {
91 localhostURL = "file://"+previewFile.getLocation().toString();
92 } else if ((localhostURL = ShowExternalPreviewAction.getLocalhostURL(store, previewFile)) == null) {
93 MessageDialog.openInformation(shell, "Couldn't create localhost URL",
94 "Please configure your localhost and documentRoot");
99 // if (store.getBoolean(PHPeclipsePlugin.USE_EXTERNAL_BROWSER_PREF)) {
100 // String[] arguments = { localhostURL };
101 // MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF));
102 // Runtime runtime = Runtime.getRuntime();
103 // String command = form.format(arguments);
104 // // console.write("External Browser command: " + command + "\n");
105 // runtime.exec(command);
107 open(new URL(localhostURL), shell, localhostURL);
109 } catch (MalformedURLException e) {
110 MessageDialog.openInformation(shell, "MalformedURLException: ", e.toString());
118 * @see IActionDelegate#selectionChanged(IAction, ISelection)
120 public void selectionChanged(IAction action, ISelection selection) {
123 public static void open(final URL url, final Shell shell, final String dialogTitle) {
124 // if (WebBrowserUtil.canUseInternalWebBrowser()) {
125 // IWorkbenchPage page = PHPeclipsePlugin.getActivePage();
127 // IViewPart part = page.findView(BrowserView.ID_BROWSER);
128 // if (part == null) {
129 // part = page.showView(BrowserView.ID_BROWSER);
131 // page.bringToTop(part);
133 // ((BrowserView) part).setUrl(url.toExternalForm());
134 // } catch (Exception e) {
137 BrowserManager manager = BrowserManager.getInstance();
138 IWebBrowser browser = manager.getCurrentWebBrowser();
139 browser.openURL(url);