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;
43 public class PHPEclipseShowAction implements IObjectActionDelegate {
44 private IWorkbenchPart workbenchPart;
46 * Constructor for Action1.
48 public PHPEclipseShowAction() {
53 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
55 public void setActivePart(IAction action, IWorkbenchPart targetPart) {
56 workbenchPart = targetPart;
59 public void run(IAction action) {
60 ISelectionProvider selectionProvider = null;
61 selectionProvider = workbenchPart.getSite().getSelectionProvider();
63 StructuredSelection selection = null;
64 selection = (StructuredSelection) selectionProvider.getSelection();
65 PHPConsole console = PHPConsole.getInstance();
67 IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
70 Iterator iterator = null;
71 iterator = selection.iterator();
72 while (iterator.hasNext()) {
73 // obj => selected object in the view
74 Object obj = iterator.next();
77 if (obj instanceof IResource) {
78 IResource resource = (IResource) obj;
80 // check if it's a file resource
81 switch (resource.getType()) {
85 IFile file = (IFile) resource;
87 if ((localhostURL = getLocalhostURL(store, (IFile) resource)) == null) {
88 MessageDialog.openInformation(
90 "Couldn't create localhost URL",
91 "Please configure your localhost and documentRoot");
95 if (store.getBoolean(PHPeclipsePlugin.USE_EXTERNAL_BROWSER_PREF)) {
96 String[] arguments = { localhostURL };
97 MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF));
99 Runtime runtime = Runtime.getRuntime();
100 String command = form.format(arguments);
101 console.write("External Browser command: " + command + "\n");
102 runtime.exec(command);
103 // runtime.exec(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF) + " " + fileName);
104 // runtime.exec("command.com /c start iexplore " + fileName);
106 // MessageDialog.openInformation(shell, "localhostURL", "localhostURL: " + localhostURL);
107 // this doesn't work under win98 ?
108 // Program.launch(localhostURL);
109 console.write("Internal Browser URL: " + localhostURL + "\n");
110 open(new URL(localhostURL), shell, localhostURL);
112 } catch (MalformedURLException e) {
113 MessageDialog.openInformation(shell, "MalformedURLException: ", e.toString());
114 } catch (IOException e) {
115 MessageDialog.openInformation(shell, "IOException", "Cannot show: " + localhostURL);
124 * @see IActionDelegate#selectionChanged(IAction, ISelection)
126 public void selectionChanged(IAction action, ISelection selection) {
129 public static String getLocalhostURL(IPreferenceStore store, IFile file) {
131 store = PHPeclipsePlugin.getDefault().getPreferenceStore();
134 // IPath path = file.getFullPath();
136 String localhostURL = file.getLocation().toString();
137 String lowerCaseFileName = localhostURL.toLowerCase();
138 String documentRoot = store.getString(PHPeclipsePlugin.DOCUMENTROOT_PREF);
139 documentRoot = documentRoot.replace('\\', '/');
140 documentRoot = documentRoot.toLowerCase();
142 if (lowerCaseFileName.startsWith(documentRoot)) {
143 localhostURL = localhostURL.substring(documentRoot.length());
148 return store.getString(PHPeclipsePlugin.LOCALHOST_PREF) + localhostURL;
152 public static void open(final URL url, final Shell shell, final String dialogTitle) {
153 // if (SWT.getPlatform().equals("win32")) {
154 // IWorkbenchPage page = PHPeclipsePlugin.getActivePage();
156 // IViewPart part = page.findView(UpdatePerspective.ID_BROWSER);
157 // if (part == null) {
158 // part = page.showView(UpdatePerspective.ID_BROWSER);
160 // page.bringToTop(part);
161 // ((IEmbeddedWebBrowser) part).openTo(url.toExternalForm());
162 // } catch (PartInitException e) {
163 // PHPeclipsePlugin.log(e);
166 IHelp help = WorkbenchHelp.getHelpSupport();
168 WorkbenchHelp.getHelpSupport().displayHelpResource(url.toExternalForm());
170 // showMessage(shell, dialogTitle, ActionMessages.getString("OpenBrowserUtil.help_not_available"), false); //$NON-NLS-1$