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.widgets.Shell;
34 import org.eclipse.ui.IObjectActionDelegate;
35 import org.eclipse.ui.IWorkbenchPart;
36 import org.eclipse.ui.help.WorkbenchHelp;
37 //import org.eclipse.jdt.internal.ui.actions.OpenBrowserUtil;
38 // import org.eclipse.help.ui.browser.LaunchURL;
40 public class PHPEclipseShowAction implements IObjectActionDelegate {
41 private IWorkbenchPart workbenchPart;
43 * Constructor for Action1.
45 public PHPEclipseShowAction() {
50 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
52 public void setActivePart(IAction action, IWorkbenchPart targetPart) {
53 workbenchPart = targetPart;
56 // public static void open(final URL url, final Shell shell, final String dialogTitle) {
57 // IHelp help= WorkbenchHelp.getHelpSupport();
58 // if (help != null) {
59 // WorkbenchHelp.getHelpSupport().displayHelpResource(url.toExternalForm());
61 // showMessage(shell, dialogTitle, ActionMessages.getString("OpenBrowserUtil.help_not_available"), false); //$NON-NLS-1$
65 public void run(IAction action) {
66 ISelectionProvider selectionProvider = null;
67 selectionProvider = workbenchPart.getSite().getSelectionProvider();
69 StructuredSelection selection = null;
70 selection = (StructuredSelection) selectionProvider.getSelection();
72 IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
75 Iterator iterator = null;
76 iterator = selection.iterator();
77 while (iterator.hasNext()) {
78 // obj => selected object in the view
79 Object obj = iterator.next();
82 if (obj instanceof IResource) {
83 IResource resource = (IResource) obj;
85 // check if it's a file resource
86 switch (resource.getType()) {
90 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, "");
110 if (store.getBoolean(PHPeclipsePlugin.USE_EXTERNAL_BROWSER_PREF)) {
111 String[] arguments = { localhostURL };
112 MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF));
114 Runtime runtime = Runtime.getRuntime();
115 String command = form.format(arguments);
116 PHPConsole.write("External Browser command: "+command+"\n");
117 runtime.exec(command);
118 // runtime.exec(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF) + " " + fileName);
119 // runtime.exec("command.com /c start iexplore " + fileName);
121 // MessageDialog.openInformation(shell, "localhostURL", "localhostURL: " + localhostURL);
122 // this doesn't work under win98 ?
123 // Program.launch(localhostURL);
124 PHPConsole.write("Internal Browser URL: "+localhostURL+"\n");
125 open(new URL(localhostURL), shell, localhostURL);
127 } catch (MalformedURLException e) {
128 MessageDialog.openInformation(shell, "MalformedURLException: ", e.toString());
129 } catch (IOException e) {
130 MessageDialog.openInformation(shell, "IOException", "Cannot show: " + localhostURL);
137 * @see IActionDelegate#selectionChanged(IAction, ISelection)
139 public void selectionChanged(IAction action, ISelection selection) {
142 public static void open(final URL url, final Shell shell, final String dialogTitle) {
143 IHelp help = WorkbenchHelp.getHelpSupport();
145 WorkbenchHelp.getHelpSupport().displayHelpResource(url.toExternalForm());
147 // showMessage(shell, dialogTitle, ActionMessages.getString("OpenBrowserUtil.help_not_available"), false); //$NON-NLS-1$