5018ac650007dc97b55de8a07d697f7c44f108eb
[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 org.eclipse.core.resources.IFile;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.runtime.IPath;
24 import org.eclipse.jface.action.IAction;
25 import org.eclipse.jface.dialogs.MessageDialog;
26 import org.eclipse.jface.preference.IPreferenceStore;
27 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.jface.viewers.ISelectionProvider;
29 import org.eclipse.jface.viewers.StructuredSelection;
30 import org.eclipse.swt.widgets.Shell;
31 import org.eclipse.ui.IObjectActionDelegate;
32 import org.eclipse.ui.IWorkbenchPart;
33
34 import org.eclipse.ui.help.WorkbenchHelp;
35 import org.eclipse.help.IHelp;
36 //import org.eclipse.jdt.internal.ui.actions.OpenBrowserUtil;
37 // import org.eclipse.help.ui.browser.LaunchURL;
38
39 public class PHPEclipseShowAction implements IObjectActionDelegate {
40   private IWorkbenchPart workbenchPart;
41   /**
42    * Constructor for Action1.
43    */
44   public PHPEclipseShowAction() {
45     super();
46   }
47
48   /**
49    * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
50    */
51   public void setActivePart(IAction action, IWorkbenchPart targetPart) {
52     workbenchPart = targetPart;
53   }
54
55   //  public static void open(final URL url, final Shell shell, final String dialogTitle) {
56   //    IHelp help= WorkbenchHelp.getHelpSupport();
57   //    if (help != null) {
58   //      WorkbenchHelp.getHelpSupport().displayHelpResource(url.toExternalForm());
59   //    } else {
60   //      showMessage(shell, dialogTitle, ActionMessages.getString("OpenBrowserUtil.help_not_available"), false); //$NON-NLS-1$
61   //    }
62   //  }
63
64   public void run(IAction action) {
65     ISelectionProvider selectionProvider = null;
66     selectionProvider = workbenchPart.getSite().getSelectionProvider();
67
68     StructuredSelection selection = null;
69     selection = (StructuredSelection) selectionProvider.getSelection();
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             IPath path = file.getFullPath();
91             //            if (j2h==null) {
92             //              shell = new Shell();
93             //              j2h = new Java2HTML(shell); 
94             //            }
95
96             String fileName = file.getLocation().toString();
97             String lowerCaseFileName = fileName.toLowerCase();
98             //       fileName = "http://localhost"+fileName.replaceAll("c:", "");
99             String documentRoot = store.getString(PHPeclipsePlugin.DOCUMENTROOT_PREF);
100             documentRoot = documentRoot.replace('\\', '/');
101             documentRoot = documentRoot.toLowerCase();
102             MessageDialog.openInformation(shell, "lowerCaseFileName", "lowerCaseFileName: " + lowerCaseFileName);
103
104             if (lowerCaseFileName.startsWith(documentRoot)) {
105               fileName = fileName.substring(documentRoot.length());
106             } else {
107               MessageDialog.openInformation(shell, "Wrong DocumentRoot", "Adjust DocumentRoot: " + documentRoot);
108               return;
109             }
110
111             fileName = store.getString(PHPeclipsePlugin.LOCALHOST_PREF) + fileName.replaceAll(documentRoot, "");
112
113             try {
114               if (store.getBoolean(PHPeclipsePlugin.USE_EXTERNAL_BROWSER_PREF)) {
115                 String[] arguments = { fileName };
116                 MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF));
117
118                 Runtime runtime = Runtime.getRuntime();
119                 runtime.exec(form.format(arguments));
120                 //                      runtime.exec(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF) + " " + fileName);
121                 //                                                              runtime.exec("command.com /c start iexplore " + fileName);
122               } else {
123                 open(new URL(fileName), shell, fileName);
124               }
125             } catch (MalformedURLException e) {
126               MessageDialog.openInformation(shell, "MalformedURLException: ", e.toString());
127             } catch (IOException e) {
128               MessageDialog.openInformation(shell, "IOException", "Cannot show: " + fileName);
129
130             }
131
132             //            MessageDialog.openInformation(shell, "PHPEclipse", "FileName - " + fileName);
133             //
134             //            Runtime runtime = Runtime.getRuntime();
135             //            try {
136             //                                                  runtime.exec("command.com /c start iexplore "+fileName);
137
138             //            MessageDialog.openInformation(shell, "J2h Plug-in", "FileName - " + fileName);
139             //            j2h.convert( fileName );
140         }
141       }
142     }
143   } /**
144                                                  * @see IActionDelegate#selectionChanged(IAction, ISelection)
145                                                  */
146   public void selectionChanged(IAction action, ISelection selection) {
147   }
148
149   public static void open(final URL url, final Shell shell, final String dialogTitle) {
150     IHelp help = WorkbenchHelp.getHelpSupport();
151     if (help != null) {
152       WorkbenchHelp.getHelpSupport().displayHelpResource(url.toExternalForm());
153     } else {
154       //   showMessage(shell, dialogTitle, ActionMessages.getString("OpenBrowserUtil.help_not_available"), false); //$NON-NLS-1$
155     }
156   }
157 }