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;
14 import java.io.IOException;
15 import java.net.MalformedURLException;
17 import java.text.MessageFormat;
18 import java.util.Iterator;
20 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
21 import net.sourceforge.phpeclipse.views.PHPConsole;
23 import org.eclipse.core.resources.IFile;
24 import org.eclipse.core.resources.IResource;
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.help.IHelp;
27 import org.eclipse.jface.action.IAction;
28 import org.eclipse.jface.dialogs.MessageDialog;
29 import org.eclipse.jface.preference.IPreferenceStore;
30 import org.eclipse.jface.viewers.ISelection;
31 import org.eclipse.jface.viewers.ISelectionProvider;
32 import org.eclipse.jface.viewers.StructuredSelection;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.widgets.Shell;
35 import org.eclipse.ui.IObjectActionDelegate;
36 import org.eclipse.ui.IViewPart;
37 import org.eclipse.ui.IWorkbenchPage;
38 import org.eclipse.ui.IWorkbenchPart;
39 import org.eclipse.ui.PartInitException;
40 import org.eclipse.update.internal.ui.UpdatePerspective;
41 import org.eclipse.ui.help.WorkbenchHelp;
43 import org.eclipse.update.internal.ui.views.IEmbeddedWebBrowser;
44 //import org.eclipse.jdt.internal.ui.actions.OpenBrowserUtil;
45 // import org.eclipse.help.ui.browser.LaunchURL;
47 public class PHPEclipseShowAction implements IObjectActionDelegate {
48 private IWorkbenchPart workbenchPart;
50 * Constructor for Action1.
52 public PHPEclipseShowAction() {
57 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
59 public void setActivePart(IAction action, IWorkbenchPart targetPart) {
60 workbenchPart = targetPart;
63 public void run(IAction action) {
64 ISelectionProvider selectionProvider = null;
65 selectionProvider = workbenchPart.getSite().getSelectionProvider();
67 StructuredSelection selection = null;
68 selection = (StructuredSelection) selectionProvider.getSelection();
69 PHPConsole console = PHPConsole.getInstance();
71 IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
74 Iterator iterator = null;
75 iterator = selection.iterator();
76 while (iterator.hasNext()) {
77 // obj => selected object in the view
78 Object obj = iterator.next();
81 if (obj instanceof IResource) {
82 IResource resource = (IResource) obj;
84 // check if it's a file resource
85 switch (resource.getType()) {
89 IFile file = (IFile) resource;
91 if ((localhostURL=getLocalhostURL(store, (IFile) resource)) == null) {
92 MessageDialog.openInformation(shell, "Couldn't create localhost URL", "Please configure your localhost and documentRoot");
96 if (store.getBoolean(PHPeclipsePlugin.USE_EXTERNAL_BROWSER_PREF)) {
97 String[] arguments = { localhostURL };
98 MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF));
100 Runtime runtime = Runtime.getRuntime();
101 String command = form.format(arguments);
102 console.write("External Browser command: " + command + "\n");
103 runtime.exec(command);
104 // runtime.exec(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF) + " " + fileName);
105 // runtime.exec("command.com /c start iexplore " + fileName);
107 // MessageDialog.openInformation(shell, "localhostURL", "localhostURL: " + localhostURL);
108 // this doesn't work under win98 ?
109 // Program.launch(localhostURL);
110 console.write("Internal Browser URL: " + localhostURL + "\n");
111 open(new URL(localhostURL), shell, localhostURL);
113 } catch (MalformedURLException e) {
114 MessageDialog.openInformation(shell, "MalformedURLException: ", e.toString());
115 } catch (IOException e) {
116 MessageDialog.openInformation(shell, "IOException", "Cannot show: " + localhostURL);
125 * @see IActionDelegate#selectionChanged(IAction, ISelection)
127 public void selectionChanged(IAction action, ISelection selection) {
130 public static String getLocalhostURL(IPreferenceStore store, IFile file) {
132 store = PHPeclipsePlugin.getDefault().getPreferenceStore();
135 // IPath path = file.getFullPath();
137 String localhostURL = file.getLocation().toString();
138 String lowerCaseFileName = localhostURL.toLowerCase();
139 String documentRoot = store.getString(PHPeclipsePlugin.DOCUMENTROOT_PREF);
140 documentRoot = documentRoot.replace('\\', '/');
141 documentRoot = documentRoot.toLowerCase();
143 if (lowerCaseFileName.startsWith(documentRoot)) {
144 localhostURL = localhostURL.substring(documentRoot.length());
149 return store.getString(PHPeclipsePlugin.LOCALHOST_PREF) + localhostURL;
153 public static void open(final URL url, final Shell shell, final String dialogTitle) {
154 if (SWT.getPlatform().equals("win32")) {
155 IWorkbenchPage page = PHPeclipsePlugin.getActivePage();
157 IViewPart part = page.findView(UpdatePerspective.ID_BROWSER);
159 part = page.showView(UpdatePerspective.ID_BROWSER);
161 page.bringToTop(part);
162 ((IEmbeddedWebBrowser) part).openTo(url.toExternalForm());
163 } catch (PartInitException e) {
164 PHPeclipsePlugin.log(e);
167 IHelp help = WorkbenchHelp.getHelpSupport();
169 WorkbenchHelp.getHelpSupport().displayHelpResource(url.toExternalForm());
171 // showMessage(shell, dialogTitle, ActionMessages.getString("OpenBrowserUtil.help_not_available"), false); //$NON-NLS-1$