deleted refs to org.eclipse.update.internal.ui.UpdateUIPlugin
[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 //            IPath path = file.getFullPath();
92 //
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();
99 //
100 //            if (lowerCaseFileName.startsWith(documentRoot)) {
101 //              localhostURL = localhostURL.substring(documentRoot.length());
102 //            } else {
103 //              MessageDialog.openInformation(shell, "Wrong DocumentRoot", "Adjust DocumentRoot: " + documentRoot);
104 //              return;
105 //            }
106 //
107 //            localhostURL = store.getString(PHPeclipsePlugin.LOCALHOST_PREF) + localhostURL.replaceAll(documentRoot, "");
108
109             if ((localhostURL=getLocalhostURL(store, (IFile) resource)) == null) {
110               MessageDialog.openInformation(shell, "Couldn't create localhost URL", "Please configure your localhost and documentRoot");
111               return;
112             }
113             try {
114               if (store.getBoolean(PHPeclipsePlugin.USE_EXTERNAL_BROWSER_PREF)) {
115                 String[] arguments = { localhostURL };
116                 MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF));
117
118                 Runtime runtime = Runtime.getRuntime();
119                 String command = form.format(arguments);
120                 console.write("External Browser command: " + command + "\n");
121                 runtime.exec(command);
122                 //                      runtime.exec(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF) + " " + fileName);
123                 //                                                              runtime.exec("command.com /c start iexplore " + fileName);
124               } else {
125                 //    MessageDialog.openInformation(shell, "localhostURL", "localhostURL: " + localhostURL);
126                 //  this doesn't work under win98 ?
127                 //     Program.launch(localhostURL);
128                 console.write("Internal Browser URL: " + localhostURL + "\n");
129                 open(new URL(localhostURL), shell, localhostURL);
130               }
131             } catch (MalformedURLException e) {
132               MessageDialog.openInformation(shell, "MalformedURLException: ", e.toString());
133             } catch (IOException e) {
134               MessageDialog.openInformation(shell, "IOException", "Cannot show: " + localhostURL);
135
136             }
137         }
138       }
139     }
140   }
141
142   /**
143    * @see IActionDelegate#selectionChanged(IAction, ISelection)
144    */
145   public void selectionChanged(IAction action, ISelection selection) {
146   }
147
148   public static String getLocalhostURL(IPreferenceStore store, IFile file) {
149     if (store == null) {
150       store = PHPeclipsePlugin.getDefault().getPreferenceStore();
151     }
152
153     IPath path = file.getFullPath();
154
155     String localhostURL = file.getLocation().toString();
156     String lowerCaseFileName = localhostURL.toLowerCase();
157     //       fileName = "http://localhost"+fileName.replaceAll("c:", "");
158     String documentRoot = store.getString(PHPeclipsePlugin.DOCUMENTROOT_PREF);
159     documentRoot = documentRoot.replace('\\', '/');
160     documentRoot = documentRoot.toLowerCase();
161
162     if (lowerCaseFileName.startsWith(documentRoot)) {
163       localhostURL = localhostURL.substring(documentRoot.length());
164     } else {
165       return null;
166     }
167
168     return store.getString(PHPeclipsePlugin.LOCALHOST_PREF) + localhostURL.replaceAll(documentRoot, "");
169
170   }
171
172   public static void open(final URL url, final Shell shell, final String dialogTitle) {
173     if (SWT.getPlatform().equals("win32")) {
174       IWorkbenchPage page = PHPeclipsePlugin.getActivePage();
175       try {
176         IViewPart part = page.findView(UpdatePerspective.ID_BROWSER);
177         if (part == null) {
178           part = page.showView(UpdatePerspective.ID_BROWSER);
179         } else
180           page.bringToTop(part);
181         ((IEmbeddedWebBrowser) part).openTo(url.toExternalForm());
182       } catch (PartInitException e) {
183         PHPeclipsePlugin.log(e);
184       }
185     } else {
186       IHelp help = WorkbenchHelp.getHelpSupport();
187       if (help != null) {
188         WorkbenchHelp.getHelpSupport().displayHelpResource(url.toExternalForm());
189       } else {
190         //   showMessage(shell, dialogTitle, ActionMessages.getString("OpenBrowserUtil.help_not_available"), false); //$NON-NLS-1$
191       }
192     }
193   }
194 }