improved syntax highligthing
[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 import org.eclipse.jdt.internal.ui.actions.OpenBrowserUtil;
34 // import org.eclipse.help.ui.browser.LaunchURL;
35
36 public class PHPEclipseShowAction implements IObjectActionDelegate {
37         private IWorkbenchPart workbenchPart;
38         /**
39          * Constructor for Action1.
40          */
41         public PHPEclipseShowAction() {
42                 super();
43         }
44
45         /**
46          * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
47          */
48         public void setActivePart(IAction action, IWorkbenchPart targetPart) {
49                 workbenchPart = targetPart;
50         }
51
52         //  public static void open(final URL url, final Shell shell, final String dialogTitle) {
53         //    IHelp help= WorkbenchHelp.getHelpSupport();
54         //    if (help != null) {
55         //      WorkbenchHelp.getHelpSupport().displayHelpResource(url.toExternalForm());
56         //    } else {
57         //      showMessage(shell, dialogTitle, ActionMessages.getString("OpenBrowserUtil.help_not_available"), false); //$NON-NLS-1$
58         //    }
59         //  }
60
61         public void run(IAction action) {
62                 ISelectionProvider selectionProvider = null;
63                 selectionProvider = workbenchPart.getSite().getSelectionProvider();
64
65                 StructuredSelection selection = null;
66                 selection = (StructuredSelection) selectionProvider.getSelection();
67
68                 IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
69
70                 Shell shell = null;
71                 Iterator iterator = null;
72                 iterator = selection.iterator();
73                 while (iterator.hasNext()) {
74                         //  obj => selected object in the view
75                         Object obj = iterator.next();
76
77                         // is it a resource
78                         if (obj instanceof IResource) {
79                                 IResource resource = (IResource) obj;
80
81                                 // check if it's a file resource
82                                 switch (resource.getType()) {
83
84                                         case IResource.FILE :
85                                                 // single file:
86                                                 IFile file = (IFile) resource;
87                                                 IPath path = file.getFullPath();
88                                                 //            if (j2h==null) {
89                                                 //              shell = new Shell();
90                                                 //              j2h = new Java2HTML(shell); 
91                                                 //            }
92
93                                                 String fileName = file.getLocation().toString();
94             String lowerCaseFileName = fileName.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             MessageDialog.openInformation(shell, "lowerCaseFileName", "lowerCaseFileName: " + lowerCaseFileName);
100               
101             if ( lowerCaseFileName.startsWith(documentRoot) ) {
102               fileName = fileName.substring(documentRoot.length());
103             } else {
104               MessageDialog.openInformation(shell, "Wrong DocumentRoot", "Adjust DocumentRoot: " + documentRoot);
105               return;
106             }
107             
108                                                 fileName = store.getString(PHPeclipsePlugin.LOCALHOST_PREF) + fileName.replaceAll(documentRoot, "");
109
110                                                 try {
111                                                         if (store.getBoolean(PHPeclipsePlugin.USE_EXTERNAL_BROWSER_PREF)) {
112                                                                 String[] arguments = { fileName };
113                                                                 MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF));
114
115                                                                 Runtime runtime = Runtime.getRuntime();
116                                                                 runtime.exec(form.format(arguments));
117                                                                 //                      runtime.exec(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF) + " " + fileName);
118                                                                 //                                                              runtime.exec("command.com /c start iexplore " + fileName);
119                                                         } else {
120                                                                 OpenBrowserUtil.open(new URL(fileName), shell, fileName);
121                                                         }
122                                                 } catch (MalformedURLException e) {
123                                                         MessageDialog.openInformation(shell, "MalformedURLException: ", e.toString());
124                                                 } catch (IOException e) {
125                                                         MessageDialog.openInformation(shell, "IOException", "Cannot show: " + fileName);
126
127                                                 }
128
129                                                 //            MessageDialog.openInformation(shell, "PHPEclipse", "FileName - " + fileName);
130                                                 //
131                                                 //            Runtime runtime = Runtime.getRuntime();
132                                                 //            try {
133                                                 //                                                      runtime.exec("command.com /c start iexplore "+fileName);
134
135                                                 //            MessageDialog.openInformation(shell, "J2h Plug-in", "FileName - " + fileName);
136                                                 //            j2h.convert( fileName );
137                                 }
138                         }
139                 }
140         } /**
141                                                  * @see IActionDelegate#selectionChanged(IAction, ISelection)
142                                                  */
143         public void selectionChanged(IAction action, ISelection selection) {
144         }
145
146 }