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;
18 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
19 import net.sourceforge.phpeclipse.views.PHPConsole;
20 import net.sourceforge.phpeclipse.views.browser.BrowserView;
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.jface.action.IAction;
24 import org.eclipse.jface.dialogs.MessageDialog;
25 import org.eclipse.jface.preference.IPreferenceStore;
26 import org.eclipse.jface.viewers.ISelection;
27 import org.eclipse.jface.viewers.ISelectionProvider;
28 import org.eclipse.jface.viewers.StructuredSelection;
29 import org.eclipse.swt.widgets.Shell;
30 import org.eclipse.ui.IActionDelegate;
31 import org.eclipse.ui.IObjectActionDelegate;
32 import org.eclipse.ui.IViewPart;
33 import org.eclipse.ui.IWorkbenchPage;
34 import org.eclipse.ui.IWorkbenchPart;
35 import org.eclipse.ui.PartInitException;
36 //import org.eclipse.update.internal.ui.UpdatePerspective;
37 //import org.eclipse.update.internal.ui.views.IEmbeddedWebBrowser;
38 public class PHPEclipseShowAction implements IObjectActionDelegate {
39 private IWorkbenchPart workbenchPart;
41 * Constructor for Action1.
43 public PHPEclipseShowAction() {
47 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
49 public void setActivePart(IAction action, IWorkbenchPart targetPart) {
50 workbenchPart = targetPart;
52 public void run(IAction action) {
53 ISelectionProvider selectionProvider = null;
54 selectionProvider = workbenchPart.getSite().getSelectionProvider();
55 StructuredSelection selection = null;
56 selection = (StructuredSelection) selectionProvider.getSelection();
57 PHPConsole console = PHPConsole.getInstance();
58 IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
60 Iterator iterator = null;
61 iterator = selection.iterator();
62 while (iterator.hasNext()) {
63 // obj => selected object in the view
64 Object obj = iterator.next();
66 if (obj instanceof IResource) {
67 IResource resource = (IResource) obj;
68 // check if it's a file resource
69 switch (resource.getType()) {
72 IFile file = (IFile) resource;
74 if ((localhostURL = getLocalhostURL(store, (IFile) resource)) == null) {
75 MessageDialog.openInformation(shell,
76 "Couldn't create localhost URL",
77 "Please configure your localhost and documentRoot");
81 if (store.getBoolean(PHPeclipsePlugin.USE_EXTERNAL_BROWSER_PREF)) {
82 String[] arguments = {localhostURL};
83 MessageFormat form = new MessageFormat(store
84 .getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF));
85 Runtime runtime = Runtime.getRuntime();
86 String command = form.format(arguments);
87 console.write("External Browser command: " + command + "\n");
88 runtime.exec(command);
89 // runtime.exec(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF)
91 // runtime.exec("command.com /c start iexplore " + fileName);
93 // MessageDialog.openInformation(shell, "localhostURL",
94 // "localhostURL: " + localhostURL);
95 // this doesn't work under win98 ?
96 // Program.launch(localhostURL);
97 console.write("Internal Browser URL: " + localhostURL + "\n");
98 open(new URL(localhostURL), shell, localhostURL);
100 } catch (MalformedURLException e) {
101 MessageDialog.openInformation(shell, "MalformedURLException: ", e
103 } catch (IOException e) {
104 MessageDialog.openInformation(shell, "IOException",
105 "Cannot show: " + localhostURL);
112 * @see IActionDelegate#selectionChanged(IAction, ISelection)
114 public void selectionChanged(IAction action, ISelection selection) {
116 public static String getLocalhostURL(IPreferenceStore store, IFile file) {
118 store = PHPeclipsePlugin.getDefault().getPreferenceStore();
120 // IPath path = file.getFullPath();
121 String localhostURL = file.getLocation().toString();
122 String lowerCaseFileName = localhostURL.toLowerCase();
123 String documentRoot = store.getString(PHPeclipsePlugin.DOCUMENTROOT_PREF);
124 documentRoot = documentRoot.replace('\\', '/');
125 documentRoot = documentRoot.toLowerCase();
126 if (lowerCaseFileName.startsWith(documentRoot)) {
127 localhostURL = localhostURL.substring(documentRoot.length());
131 return store.getString(PHPeclipsePlugin.LOCALHOST_PREF) + localhostURL;
134 public static void open(final URL url, final Shell shell,
135 final String dialogTitle) {
136 IWorkbenchPage page = PHPeclipsePlugin.getActivePage();
138 IViewPart part = page.findView(BrowserView.ID_BROWSER);
140 part = page.showView(BrowserView.ID_BROWSER);
142 page.bringToTop(part);
144 ((BrowserView) part).setUrl(url.toExternalForm());
145 } catch (PartInitException e) {
146 PHPeclipsePlugin.log(e);