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