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.IPreferenceConstants;
20 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
21 import net.sourceforge.phpeclipse.overlaypages.Util;
22 import net.sourceforge.phpeclipse.views.PHPConsole;
23 import net.sourceforge.phpeclipse.views.browser.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.ui.PartInitException;
39 //import org.eclipse.update.internal.ui.UpdatePerspective;
40 //import org.eclipse.update.internal.ui.views.IEmbeddedWebBrowser;
41 public class PHPEclipseShowAction implements IObjectActionDelegate {
42 private IWorkbenchPart workbenchPart;
44 * Constructor for Action1.
46 public PHPEclipseShowAction() {
50 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
52 public void setActivePart(IAction action, IWorkbenchPart targetPart) {
53 workbenchPart = targetPart;
55 public void run(IAction action) {
56 ISelectionProvider selectionProvider = null;
57 selectionProvider = workbenchPart.getSite().getSelectionProvider();
58 StructuredSelection selection = null;
59 selection = (StructuredSelection) selectionProvider.getSelection();
60 PHPConsole console = PHPConsole.getInstance();
61 IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
63 Iterator iterator = null;
64 iterator = selection.iterator();
65 while (iterator.hasNext()) {
66 // obj => selected object in the view
67 Object obj = iterator.next();
69 if (obj instanceof IResource) {
70 IResource resource = (IResource) obj;
71 // check if it's a file resource
72 switch (resource.getType()) {
75 IFile file = (IFile) resource;
77 if ((localhostURL = getLocalhostURL(store, (IFile) resource)) == null) {
78 MessageDialog.openInformation(shell,
79 "Couldn't create localhost URL",
80 "Please configure your localhost and documentRoot");
84 if (store.getBoolean(PHPeclipsePlugin.USE_EXTERNAL_BROWSER_PREF)) {
85 String[] arguments = {localhostURL};
86 MessageFormat form = new MessageFormat(store
87 .getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF));
88 Runtime runtime = Runtime.getRuntime();
89 String command = form.format(arguments);
90 console.write("External Browser command: " + command + "\n");
91 runtime.exec(command);
92 // runtime.exec(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF)
94 // runtime.exec("command.com /c start iexplore " + fileName);
96 // MessageDialog.openInformation(shell, "localhostURL",
97 // "localhostURL: " + localhostURL);
98 // this doesn't work under win98 ?
99 // Program.launch(localhostURL);
100 console.write("Internal Browser URL: " + localhostURL + "\n");
101 open(new URL(localhostURL), shell, localhostURL);
103 } catch (MalformedURLException e) {
104 MessageDialog.openInformation(shell, "MalformedURLException: ", e
106 } catch (IOException e) {
107 MessageDialog.openInformation(shell, "IOException",
108 "Cannot show: " + localhostURL);
115 * @see IActionDelegate#selectionChanged(IAction, ISelection)
117 public void selectionChanged(IAction action, ISelection selection) {
119 public static String getLocalhostURL(IPreferenceStore store, IFile file) {
121 store = PHPeclipsePlugin.getDefault().getPreferenceStore();
123 // IPath path = file.getFullPath();
124 String localhostURL = file.getLocation().toString();
125 String lowerCaseFileName = localhostURL.toLowerCase();
126 // String documentRoot = store.getString(PHPeclipsePlugin.DOCUMENTROOT_PREF);
127 String documentRoot = Util.getMiscProjectsPreferenceValue(
128 file.getProject(), IPreferenceConstants.PHP_DOCUMENTROOT_PREF);
130 documentRoot = documentRoot.replace('\\', '/');
131 documentRoot = documentRoot.toLowerCase();
133 if (lowerCaseFileName.startsWith(documentRoot)) {
134 localhostURL = localhostURL.substring(documentRoot.length());
138 // return store.getString(PHPeclipsePlugin.LOCALHOST_PREF) + localhostURL;
139 return Util.getMiscProjectsPreferenceValue(file.getProject(), IPreferenceConstants.PHP_LOCALHOST_PREF) + localhostURL;
142 public static void open(final URL url, final Shell shell,
143 final String dialogTitle) {
144 IWorkbenchPage page = PHPeclipsePlugin.getActivePage();
146 IViewPart part = page.findView(BrowserView.ID_BROWSER);
148 part = page.showView(BrowserView.ID_BROWSER);
150 page.bringToTop(part);
152 ((BrowserView) part).setUrl(url.toExternalForm());
153 } catch (PartInitException e) {
154 PHPeclipsePlugin.log(e);