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.help.IHelp;
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.SWT;
33 import org.eclipse.swt.widgets.Shell;
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.ui.help.WorkbenchHelp;
40 import org.eclipse.update.internal.ui.UpdatePerspective;
41 import org.eclipse.update.internal.ui.views.IEmbeddedWebBrowser;
42 //import org.eclipse.jdt.internal.ui.actions.OpenBrowserUtil;
43 // import org.eclipse.help.ui.browser.LaunchURL;
45 public class PHPEclipseShowAction implements IObjectActionDelegate {
46 private IWorkbenchPart workbenchPart;
48 * Constructor for Action1.
50 public PHPEclipseShowAction() {
55 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
57 public void setActivePart(IAction action, IWorkbenchPart targetPart) {
58 workbenchPart = targetPart;
61 public void run(IAction action) {
62 ISelectionProvider selectionProvider = null;
63 selectionProvider = workbenchPart.getSite().getSelectionProvider();
65 StructuredSelection selection = null;
66 selection = (StructuredSelection) selectionProvider.getSelection();
67 PHPConsole console = PHPConsole.getInstance();
69 IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
72 Iterator iterator = null;
73 iterator = selection.iterator();
74 while (iterator.hasNext()) {
75 // obj => selected object in the view
76 Object obj = iterator.next();
79 if (obj instanceof IResource) {
80 IResource resource = (IResource) obj;
82 // check if it's a file resource
83 switch (resource.getType()) {
87 IFile file = (IFile) resource;
89 if ((localhostURL=getLocalhostURL(store, (IFile) resource)) == null) {
90 MessageDialog.openInformation(shell, "Couldn't create localhost URL", "Please configure your localhost and documentRoot");
94 if (store.getBoolean(PHPeclipsePlugin.USE_EXTERNAL_BROWSER_PREF)) {
95 String[] arguments = { localhostURL };
96 MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF));
98 Runtime runtime = Runtime.getRuntime();
99 String command = form.format(arguments);
100 console.write("External Browser command: " + command + "\n");
101 runtime.exec(command);
102 // runtime.exec(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF) + " " + fileName);
103 // runtime.exec("command.com /c start iexplore " + fileName);
105 // MessageDialog.openInformation(shell, "localhostURL", "localhostURL: " + localhostURL);
106 // this doesn't work under win98 ?
107 // Program.launch(localhostURL);
108 console.write("Internal Browser URL: " + localhostURL + "\n");
109 open(new URL(localhostURL), shell, localhostURL);
111 } catch (MalformedURLException e) {
112 MessageDialog.openInformation(shell, "MalformedURLException: ", e.toString());
113 } catch (IOException e) {
114 MessageDialog.openInformation(shell, "IOException", "Cannot show: " + localhostURL);
123 * @see IActionDelegate#selectionChanged(IAction, ISelection)
125 public void selectionChanged(IAction action, ISelection selection) {
128 public static String getLocalhostURL(IPreferenceStore store, IFile file) {
130 store = PHPeclipsePlugin.getDefault().getPreferenceStore();
133 // IPath path = file.getFullPath();
135 String localhostURL = file.getLocation().toString();
136 String lowerCaseFileName = localhostURL.toLowerCase();
137 String documentRoot = store.getString(PHPeclipsePlugin.DOCUMENTROOT_PREF);
138 documentRoot = documentRoot.replace('\\', '/');
139 documentRoot = documentRoot.toLowerCase();
141 if (lowerCaseFileName.startsWith(documentRoot)) {
142 localhostURL = localhostURL.substring(documentRoot.length());
147 return store.getString(PHPeclipsePlugin.LOCALHOST_PREF) + localhostURL;
151 public static void open(final URL url, final Shell shell, final String dialogTitle) {
152 if (SWT.getPlatform().equals("win32")) {
153 IWorkbenchPage page = PHPeclipsePlugin.getActivePage();
155 IViewPart part = page.findView(UpdatePerspective.ID_BROWSER);
157 part = page.showView(UpdatePerspective.ID_BROWSER);
159 page.bringToTop(part);
160 ((IEmbeddedWebBrowser) part).openTo(url.toExternalForm());
161 } catch (PartInitException e) {
162 PHPeclipsePlugin.log(e);
165 IHelp help = WorkbenchHelp.getHelpSupport();
167 WorkbenchHelp.getHelpSupport().displayHelpResource(url.toExternalForm());
169 // showMessage(shell, dialogTitle, ActionMessages.getString("OpenBrowserUtil.help_not_available"), false); //$NON-NLS-1$