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