0366b57a7a37d898c99aeb56f73ebb9e48ed76ca
[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 import java.io.IOException;
14 import java.net.MalformedURLException;
15 import java.net.URL;
16 import java.text.MessageFormat;
17 import java.util.Iterator;
18
19 import net.sourceforge.phpeclipse.IPreferenceConstants;
20 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
21 import net.sourceforge.phpeclipse.overlaypages.Util;
22 import net.sourceforge.phpeclipse.views.PHPConsole;
23 import net.sourceforge.phpeclipse.views.browser.BrowserView;
24 import org.eclipse.core.resources.IFile;
25 import org.eclipse.core.resources.IResource;
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.IActionDelegate;
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.update.internal.ui.UpdatePerspective;
40 //import org.eclipse.update.internal.ui.views.IEmbeddedWebBrowser;
41 public class PHPEclipseShowAction implements IObjectActionDelegate {
42   private IWorkbenchPart workbenchPart;
43   /**
44    * Constructor for Action1.
45    */
46   public PHPEclipseShowAction() {
47     super();
48   }
49   /**
50    * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
51    */
52   public void setActivePart(IAction action, IWorkbenchPart targetPart) {
53     workbenchPart = targetPart;
54   }
55   public void run(IAction action) {
56     ISelectionProvider selectionProvider = null;
57     selectionProvider = workbenchPart.getSite().getSelectionProvider();
58     StructuredSelection selection = null;
59     selection = (StructuredSelection) selectionProvider.getSelection();
60     PHPConsole console = PHPConsole.getInstance();
61     IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
62     Shell shell = null;
63     Iterator iterator = null;
64     iterator = selection.iterator();
65     while (iterator.hasNext()) {
66       //  obj => selected object in the view
67       Object obj = iterator.next();
68       // is it a resource
69       if (obj instanceof IResource) {
70         IResource resource = (IResource) obj;
71         // check if it's a file resource
72         switch (resource.getType()) {
73           case IResource.FILE :
74             // single file:
75             IFile file = (IFile) resource;
76             String localhostURL;
77             if ((localhostURL = getLocalhostURL(store, (IFile) resource)) == null) {
78               MessageDialog.openInformation(shell,
79                   "Couldn't create localhost URL",
80                   "Please configure your localhost and documentRoot");
81               return;
82             }
83             try {
84               if (store.getBoolean(PHPeclipsePlugin.USE_EXTERNAL_BROWSER_PREF)) {
85                 String[] arguments = {localhostURL};
86                 MessageFormat form = new MessageFormat(store
87                     .getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF));
88                 Runtime runtime = Runtime.getRuntime();
89                 String command = form.format(arguments);
90                 console.write("External Browser command: " + command + "\n");
91                 runtime.exec(command);
92                 //                      runtime.exec(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF)
93                 // + " " + fileName);
94                 //                                                              runtime.exec("command.com /c start iexplore " + fileName);
95               } else {
96                 //    MessageDialog.openInformation(shell, "localhostURL",
97                 // "localhostURL: " + localhostURL);
98                 //  this doesn't work under win98 ?
99                 //     Program.launch(localhostURL);
100                 console.write("Internal Browser URL: " + localhostURL + "\n");
101                 open(new URL(localhostURL), shell, localhostURL);
102               }
103             } catch (MalformedURLException e) {
104               MessageDialog.openInformation(shell, "MalformedURLException: ", e
105                   .toString());
106             } catch (IOException e) {
107               MessageDialog.openInformation(shell, "IOException",
108                   "Cannot show: " + localhostURL);
109             }
110         }
111       }
112     }
113   }
114   /**
115    * @see IActionDelegate#selectionChanged(IAction, ISelection)
116    */
117   public void selectionChanged(IAction action, ISelection selection) {
118   }
119   public static String getLocalhostURL(IPreferenceStore store, IFile file) {
120     if (store == null) {
121       store = PHPeclipsePlugin.getDefault().getPreferenceStore();
122     }
123     // IPath path = file.getFullPath();
124     String localhostURL = file.getLocation().toString();
125     String lowerCaseFileName = localhostURL.toLowerCase();
126   //  String documentRoot = store.getString(PHPeclipsePlugin.DOCUMENTROOT_PREF);
127     String documentRoot = Util.getMiscProjectsPreferenceValue(
128                 file.getProject(), IPreferenceConstants.PHP_DOCUMENTROOT_PREF);
129     
130     documentRoot = documentRoot.replace('\\', '/');
131     documentRoot = documentRoot.toLowerCase();
132     
133     if (lowerCaseFileName.startsWith(documentRoot)) {
134       localhostURL = localhostURL.substring(documentRoot.length());
135     } else {
136       return null;  
137     }
138 //    return store.getString(PHPeclipsePlugin.LOCALHOST_PREF) + localhostURL;
139     return Util.getMiscProjectsPreferenceValue(file.getProject(), IPreferenceConstants.PHP_LOCALHOST_PREF) + localhostURL;
140   }
141   
142   public static void open(final URL url, final Shell shell,
143       final String dialogTitle) {
144     IWorkbenchPage page = PHPeclipsePlugin.getActivePage();
145     try {
146       IViewPart part = page.findView(BrowserView.ID_BROWSER);
147       if (part == null) {
148         part = page.showView(BrowserView.ID_BROWSER);
149       } else {
150         page.bringToTop(part);
151       }
152       ((BrowserView) part).setUrl(url.toExternalForm());
153     } catch (Exception e) {
154     }
155   }
156 }