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 // IPath path = file.getFullPath();
93 // String localhostURL = file.getLocation().toString();
94 // String lowerCaseFileName = localhostURL.toLowerCase();
95 // // fileName = "http://localhost"+fileName.replaceAll("c:", "");
96 // String documentRoot = store.getString(PHPeclipsePlugin.DOCUMENTROOT_PREF);
97 // documentRoot = documentRoot.replace('\\', '/');
98 // documentRoot = documentRoot.toLowerCase();
100 // if (lowerCaseFileName.startsWith(documentRoot)) {
101 // localhostURL = localhostURL.substring(documentRoot.length());
103 // MessageDialog.openInformation(shell, "Wrong DocumentRoot", "Adjust DocumentRoot: " + documentRoot);
107 // localhostURL = store.getString(PHPeclipsePlugin.LOCALHOST_PREF) + localhostURL.replaceAll(documentRoot, "");
109 if ((localhostURL=getLocalhostURL(store, (IFile) resource)) == null) {
110 MessageDialog.openInformation(shell, "Couldn't create localhost URL", "Please configure your localhost and documentRoot");
114 if (store.getBoolean(PHPeclipsePlugin.USE_EXTERNAL_BROWSER_PREF)) {
115 String[] arguments = { localhostURL };
116 MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF));
118 Runtime runtime = Runtime.getRuntime();
119 String command = form.format(arguments);
120 console.write("External Browser command: " + command + "\n");
121 runtime.exec(command);
122 // runtime.exec(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF) + " " + fileName);
123 // runtime.exec("command.com /c start iexplore " + fileName);
125 // MessageDialog.openInformation(shell, "localhostURL", "localhostURL: " + localhostURL);
126 // this doesn't work under win98 ?
127 // Program.launch(localhostURL);
128 console.write("Internal Browser URL: " + localhostURL + "\n");
129 open(new URL(localhostURL), shell, localhostURL);
131 } catch (MalformedURLException e) {
132 MessageDialog.openInformation(shell, "MalformedURLException: ", e.toString());
133 } catch (IOException e) {
134 MessageDialog.openInformation(shell, "IOException", "Cannot show: " + localhostURL);
143 * @see IActionDelegate#selectionChanged(IAction, ISelection)
145 public void selectionChanged(IAction action, ISelection selection) {
148 public static String getLocalhostURL(IPreferenceStore store, IFile file) {
150 store = PHPeclipsePlugin.getDefault().getPreferenceStore();
153 // IPath path = file.getFullPath();
155 String localhostURL = file.getLocation().toString();
156 String lowerCaseFileName = localhostURL.toLowerCase();
157 // fileName = "http://localhost"+fileName.replaceAll("c:", "");
158 String documentRoot = store.getString(PHPeclipsePlugin.DOCUMENTROOT_PREF);
159 documentRoot = documentRoot.replace('\\', '/');
160 documentRoot = documentRoot.toLowerCase();
162 if (lowerCaseFileName.startsWith(documentRoot)) {
163 localhostURL = localhostURL.substring(documentRoot.length());
168 return store.getString(PHPeclipsePlugin.LOCALHOST_PREF) + localhostURL.replaceAll(documentRoot, "");
172 public static void open(final URL url, final Shell shell, final String dialogTitle) {
173 if (SWT.getPlatform().equals("win32")) {
174 IWorkbenchPage page = PHPeclipsePlugin.getActivePage();
176 IViewPart part = page.findView(UpdatePerspective.ID_BROWSER);
178 part = page.showView(UpdatePerspective.ID_BROWSER);
180 page.bringToTop(part);
181 ((IEmbeddedWebBrowser) part).openTo(url.toExternalForm());
182 } catch (PartInitException e) {
183 PHPeclipsePlugin.log(e);
186 IHelp help = WorkbenchHelp.getHelpSupport();
188 WorkbenchHelp.getHelpSupport().displayHelpResource(url.toExternalForm());
190 // showMessage(shell, dialogTitle, ActionMessages.getString("OpenBrowserUtil.help_not_available"), false); //$NON-NLS-1$