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;
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.IResource;
24 import org.eclipse.core.runtime.IPath;
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.widgets.Shell;
33 import org.eclipse.ui.IObjectActionDelegate;
34 import org.eclipse.ui.IWorkbenchPart;
35 import org.eclipse.ui.help.WorkbenchHelp;
36 //import org.eclipse.jdt.internal.ui.actions.OpenBrowserUtil;
37 // import org.eclipse.help.ui.browser.LaunchURL;
39 public class PHPEclipseShowAction implements IObjectActionDelegate {
40 private IWorkbenchPart workbenchPart;
42 * Constructor for Action1.
44 public PHPEclipseShowAction() {
49 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
51 public void setActivePart(IAction action, IWorkbenchPart targetPart) {
52 workbenchPart = targetPart;
55 // public static void open(final URL url, final Shell shell, final String dialogTitle) {
56 // IHelp help= WorkbenchHelp.getHelpSupport();
57 // if (help != null) {
58 // WorkbenchHelp.getHelpSupport().displayHelpResource(url.toExternalForm());
60 // showMessage(shell, dialogTitle, ActionMessages.getString("OpenBrowserUtil.help_not_available"), false); //$NON-NLS-1$
64 public void run(IAction action) {
65 ISelectionProvider selectionProvider = null;
66 selectionProvider = workbenchPart.getSite().getSelectionProvider();
68 StructuredSelection selection = null;
69 selection = (StructuredSelection) selectionProvider.getSelection();
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;
90 IPath path = file.getFullPath();
92 String localhostURL = file.getLocation().toString();
93 String lowerCaseFileName = localhostURL.toLowerCase();
94 // fileName = "http://localhost"+fileName.replaceAll("c:", "");
95 String documentRoot = store.getString(PHPeclipsePlugin.DOCUMENTROOT_PREF);
96 documentRoot = documentRoot.replace('\\', '/');
97 documentRoot = documentRoot.toLowerCase();
99 if (lowerCaseFileName.startsWith(documentRoot)) {
100 localhostURL = localhostURL.substring(documentRoot.length());
102 MessageDialog.openInformation(shell, "Wrong DocumentRoot", "Adjust DocumentRoot: " + documentRoot);
106 localhostURL = store.getString(PHPeclipsePlugin.LOCALHOST_PREF) + localhostURL.replaceAll(documentRoot, "");
109 if (store.getBoolean(PHPeclipsePlugin.USE_EXTERNAL_BROWSER_PREF)) {
110 String[] arguments = { localhostURL };
111 MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF));
113 Runtime runtime = Runtime.getRuntime();
114 String command = form.format(arguments);
115 PHPConsole.write("External Browser command: "+command+"\n");
116 runtime.exec(command);
117 // runtime.exec(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF) + " " + fileName);
118 // runtime.exec("command.com /c start iexplore " + fileName);
120 // MessageDialog.openInformation(shell, "localhostURL", "localhostURL: " + localhostURL);
121 // this doesn't work under win98 ?
122 // Program.launch(localhostURL);
123 PHPConsole.write("Internal Browser URL: "+localhostURL+"\n");
124 open(new URL(localhostURL), shell, localhostURL);
126 } catch (MalformedURLException e) {
127 MessageDialog.openInformation(shell, "MalformedURLException: ", e.toString());
128 } catch (IOException e) {
129 MessageDialog.openInformation(shell, "IOException", "Cannot show: " + localhostURL);
136 * @see IActionDelegate#selectionChanged(IAction, ISelection)
138 public void selectionChanged(IAction action, ISelection selection) {
141 public static void open(final URL url, final Shell shell, final String dialogTitle) {
142 IHelp help = WorkbenchHelp.getHelpSupport();
144 WorkbenchHelp.getHelpSupport().displayHelpResource(url.toExternalForm());
146 // showMessage(shell, dialogTitle, ActionMessages.getString("OpenBrowserUtil.help_not_available"), false); //$NON-NLS-1$