Eclipse 3.x compatible;
[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
43 public class PHPEclipseShowAction implements IObjectActionDelegate {
44   private IWorkbenchPart workbenchPart;
45   /**
46    * Constructor for Action1.
47    */
48   public PHPEclipseShowAction() {
49     super();
50   }
51
52   /**
53    * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
54    */
55   public void setActivePart(IAction action, IWorkbenchPart targetPart) {
56     workbenchPart = targetPart;
57   }
58
59   public void run(IAction action) {
60     ISelectionProvider selectionProvider = null;
61     selectionProvider = workbenchPart.getSite().getSelectionProvider();
62
63     StructuredSelection selection = null;
64     selection = (StructuredSelection) selectionProvider.getSelection();
65     PHPConsole console = PHPConsole.getInstance();
66
67     IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
68
69     Shell shell = null;
70     Iterator iterator = null;
71     iterator = selection.iterator();
72     while (iterator.hasNext()) {
73       //  obj => selected object in the view
74       Object obj = iterator.next();
75
76       // is it a resource
77       if (obj instanceof IResource) {
78         IResource resource = (IResource) obj;
79
80         // check if it's a file resource
81         switch (resource.getType()) {
82
83           case IResource.FILE :
84             // single file:
85             IFile file = (IFile) resource;
86             String localhostURL;
87             if ((localhostURL = getLocalhostURL(store, (IFile) resource)) == null) {
88               MessageDialog.openInformation(
89                 shell,
90                 "Couldn't create localhost URL",
91                 "Please configure your localhost and documentRoot");
92               return;
93             }
94             try {
95               if (store.getBoolean(PHPeclipsePlugin.USE_EXTERNAL_BROWSER_PREF)) {
96                 String[] arguments = { localhostURL };
97                 MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF));
98
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);
105               } else {
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);
111               }
112             } catch (MalformedURLException e) {
113               MessageDialog.openInformation(shell, "MalformedURLException: ", e.toString());
114             } catch (IOException e) {
115               MessageDialog.openInformation(shell, "IOException", "Cannot show: " + localhostURL);
116
117             }
118         }
119       }
120     }
121   }
122
123   /**
124    * @see IActionDelegate#selectionChanged(IAction, ISelection)
125    */
126   public void selectionChanged(IAction action, ISelection selection) {
127   }
128
129   public static String getLocalhostURL(IPreferenceStore store, IFile file) {
130     if (store == null) {
131       store = PHPeclipsePlugin.getDefault().getPreferenceStore();
132     }
133
134     // IPath path = file.getFullPath();
135
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();
141
142     if (lowerCaseFileName.startsWith(documentRoot)) {
143       localhostURL = localhostURL.substring(documentRoot.length());
144     } else {
145       return null;
146     }
147
148     return store.getString(PHPeclipsePlugin.LOCALHOST_PREF) + localhostURL;
149
150   }
151
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();
155     //      try {
156     //        IViewPart part = page.findView(UpdatePerspective.ID_BROWSER);
157     //        if (part == null) {
158     //          part = page.showView(UpdatePerspective.ID_BROWSER);
159     //        } else
160     //          page.bringToTop(part);
161     //        ((IEmbeddedWebBrowser) part).openTo(url.toExternalForm());
162     //      } catch (PartInitException e) {
163     //        PHPeclipsePlugin.log(e);
164     //      }
165     //    } else {
166     IHelp help = WorkbenchHelp.getHelpSupport();
167     if (help != null) {
168       WorkbenchHelp.getHelpSupport().displayHelpResource(url.toExternalForm());
169     } else {
170       //   showMessage(shell, dialogTitle, ActionMessages.getString("OpenBrowserUtil.help_not_available"), false); //$NON-NLS-1$
171     }
172   }
173   //  }
174 }