some bugfixes
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / actions / PHPEclipseShowAction.java
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
7
8 Contributors:
9     IBM Corporation - Initial implementation
10     Klaus Hartlage - www.eclipseproject.de
11 **********************************************************************/
12 package net.sourceforge.phpeclipse.actions;
13
14 import java.io.IOException;
15 import java.net.MalformedURLException;
16 import java.net.URL;
17 import java.text.MessageFormat;
18 import java.util.Iterator;
19
20 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
21 import net.sourceforge.phpeclipse.views.PHPConsole;
22
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;
42
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;
46
47 public class PHPEclipseShowAction implements IObjectActionDelegate {
48   private IWorkbenchPart workbenchPart;
49   /**
50    * Constructor for Action1.
51    */
52   public PHPEclipseShowAction() {
53     super();
54   }
55
56   /**
57    * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
58    */
59   public void setActivePart(IAction action, IWorkbenchPart targetPart) {
60     workbenchPart = targetPart;
61   }
62
63   public void run(IAction action) {
64     ISelectionProvider selectionProvider = null;
65     selectionProvider = workbenchPart.getSite().getSelectionProvider();
66
67     StructuredSelection selection = null;
68     selection = (StructuredSelection) selectionProvider.getSelection();
69     PHPConsole console = PHPConsole.getInstance();
70
71     IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
72
73     Shell shell = null;
74     Iterator iterator = null;
75     iterator = selection.iterator();
76     while (iterator.hasNext()) {
77       //  obj => selected object in the view
78       Object obj = iterator.next();
79
80       // is it a resource
81       if (obj instanceof IResource) {
82         IResource resource = (IResource) obj;
83
84         // check if it's a file resource
85         switch (resource.getType()) {
86
87           case IResource.FILE :
88             // single file:
89             IFile file = (IFile) resource;
90           String localhostURL;
91             if ((localhostURL=getLocalhostURL(store, (IFile) resource)) == null) {
92               MessageDialog.openInformation(shell, "Couldn't create localhost URL", "Please configure your localhost and documentRoot");
93               return;
94             }
95             try {
96               if (store.getBoolean(PHPeclipsePlugin.USE_EXTERNAL_BROWSER_PREF)) {
97                 String[] arguments = { localhostURL };
98                 MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF));
99
100                 Runtime runtime = Runtime.getRuntime();
101                 String command = form.format(arguments);
102                 console.write("External Browser command: " + command + "\n");
103                 runtime.exec(command);
104                 //                      runtime.exec(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF) + " " + fileName);
105                 //                                                              runtime.exec("command.com /c start iexplore " + fileName);
106               } else {
107                 //    MessageDialog.openInformation(shell, "localhostURL", "localhostURL: " + localhostURL);
108                 //  this doesn't work under win98 ?
109                 //     Program.launch(localhostURL);
110                 console.write("Internal Browser URL: " + localhostURL + "\n");
111                 open(new URL(localhostURL), shell, localhostURL);
112               }
113             } catch (MalformedURLException e) {
114               MessageDialog.openInformation(shell, "MalformedURLException: ", e.toString());
115             } catch (IOException e) {
116               MessageDialog.openInformation(shell, "IOException", "Cannot show: " + localhostURL);
117
118             }
119         }
120       }
121     }
122   }
123
124   /**
125    * @see IActionDelegate#selectionChanged(IAction, ISelection)
126    */
127   public void selectionChanged(IAction action, ISelection selection) {
128   }
129
130   public static String getLocalhostURL(IPreferenceStore store, IFile file) {
131     if (store == null) {
132       store = PHPeclipsePlugin.getDefault().getPreferenceStore();
133     }
134
135    // IPath path = file.getFullPath();
136
137     String localhostURL = file.getLocation().toString();
138     String lowerCaseFileName = localhostURL.toLowerCase();
139     String documentRoot = store.getString(PHPeclipsePlugin.DOCUMENTROOT_PREF);
140     documentRoot = documentRoot.replace('\\', '/');
141     documentRoot = documentRoot.toLowerCase();
142
143     if (lowerCaseFileName.startsWith(documentRoot)) {
144       localhostURL = localhostURL.substring(documentRoot.length());
145     } else {
146       return null;
147     }
148
149     return store.getString(PHPeclipsePlugin.LOCALHOST_PREF) + localhostURL; 
150
151   }
152
153   public static void open(final URL url, final Shell shell, final String dialogTitle) {
154     if (SWT.getPlatform().equals("win32")) {
155       IWorkbenchPage page = PHPeclipsePlugin.getActivePage();
156       try {
157         IViewPart part = page.findView(UpdatePerspective.ID_BROWSER);
158         if (part == null) {
159           part = page.showView(UpdatePerspective.ID_BROWSER);
160         } else
161           page.bringToTop(part);
162         ((IEmbeddedWebBrowser) part).openTo(url.toExternalForm());
163       } catch (PartInitException e) {
164         PHPeclipsePlugin.log(e);
165       }
166     } else {
167       IHelp help = WorkbenchHelp.getHelpSupport();
168       if (help != null) {
169         WorkbenchHelp.getHelpSupport().displayHelpResource(url.toExternalForm());
170       } else {
171         //   showMessage(shell, dialogTitle, ActionMessages.getString("OpenBrowserUtil.help_not_available"), false); //$NON-NLS-1$
172       }
173     }
174   }
175 }