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