a0b64f353e3ebbdadacabda3808e75679c8d5fd7
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / actions / PHPEclipseShowAction.java
1 /***********************************************************************************************************************************
2  * Copyright (c) 2000, 2002 IBM Corp. and others. All rights reserved. This program and the accompanying materials are made
3  * available under the terms of the Common Public License v1.0 which accompanies this distribution, and is available at
4  * http://www.eclipse.org/legal/cpl-v10.html
5  *
6  * Contributors: IBM Corporation - Initial implementation
7  *               www.phpeclipse.de
8  **********************************************************************************************************************************/
9 package net.sourceforge.phpeclipse.actions;
10
11 import java.net.MalformedURLException;
12 import java.net.URL;
13 import java.util.Iterator;
14
15 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
16 import net.sourceforge.phpeclipse.ui.IPreferenceConstants;
17 import net.sourceforge.phpeclipse.ui.WebUI;
18 import net.sourceforge.phpeclipse.ui.editor.ShowExternalPreviewAction;
19 import net.sourceforge.phpeclipse.ui.overlaypages.ProjectPrefUtil;
20 import net.sourceforge.phpeclipse.webbrowser.IWebBrowser;
21 import net.sourceforge.phpeclipse.webbrowser.internal.BrowserManager;
22
23 import org.eclipse.core.resources.IFile;
24 import org.eclipse.core.resources.IResource;
25 import org.eclipse.jface.action.IAction;
26 import org.eclipse.jface.dialogs.MessageDialog;
27 import org.eclipse.jface.preference.IPreferenceStore;
28 import org.eclipse.jface.viewers.ISelection;
29 import org.eclipse.jface.viewers.ISelectionProvider;
30 import org.eclipse.jface.viewers.StructuredSelection;
31 import org.eclipse.swt.widgets.Shell;
32 import org.eclipse.ui.IActionDelegate;
33 import org.eclipse.ui.IObjectActionDelegate;
34 import org.eclipse.ui.IWorkbenchPart;
35
36 public class PHPEclipseShowAction implements IObjectActionDelegate {
37         private IWorkbenchPart workbenchPart;
38
39         /**
40          * Constructor for Action1.
41          */
42         public PHPEclipseShowAction() {
43                 super();
44         }
45
46         /**
47          * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
48          */
49         public void setActivePart(IAction action, IWorkbenchPart targetPart) {
50                 workbenchPart = targetPart;
51         }
52
53         public void run(IAction action) {
54                 ISelectionProvider selectionProvider = null;
55                 selectionProvider = workbenchPart.getSite().getSelectionProvider();
56                 StructuredSelection selection = null;
57                 selection = (StructuredSelection) selectionProvider.getSelection();
58                 IPreferenceStore store = WebUI.getDefault()
59                                 .getPreferenceStore();
60                 Shell shell = null;
61                 Iterator iterator = null;
62                 iterator = selection.iterator();
63                 while (iterator.hasNext()) {
64                         // obj => selected object in the view
65                         Object obj = iterator.next();
66                         // is it a resource
67                         if (obj instanceof IResource) {
68                                 IResource resource = (IResource) obj;
69                                 // check if it's a file resource
70                                 switch (resource.getType()) {
71                                 case IResource.FILE:
72                                         // single file:
73                                         IFile previewFile = (IFile) resource;
74                                         String extension = previewFile.getFileExtension()
75                                                         .toLowerCase();
76                                         boolean bringToTopPreview = ProjectPrefUtil
77                                                         .getPreviewBooleanValue(
78                                                                         previewFile,
79                                                                         IPreferenceConstants.PHP_BRING_TO_TOP_PREVIEW_DEFAULT);
80                                         // boolean showHTMLFilesLocal =
81                                         // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
82                                         // IPreferenceConstants.PHP_SHOW_HTML_FILES_LOCAL);
83                                         // boolean showXMLFilesLocal =
84                                         // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
85                                         // IPreferenceConstants.PHP_SHOW_XML_FILES_LOCAL);
86                                         boolean isHTMLFileName = "html".equals(extension)
87                                                         || "htm".equals(extension)
88                                                         || "xhtml".equals(extension);
89                                         boolean isXMLFileName = "xml".equals(extension)
90                                                         || "xsd".equals(extension)
91                                                         || "dtd".equals(extension);
92
93                                         String localhostURL;
94                                         // if (showHTMLFilesLocal && isHTMLFileName) {
95                                         // localhostURL =
96                                         // "file://"+previewFile.getLocation().toString();
97                                         // } else if (showXMLFilesLocal && isXMLFileName) {
98                                         // localhostURL =
99                                         // "file://"+previewFile.getLocation().toString();
100                                         // } else
101                                         if ((localhostURL = ShowExternalPreviewAction
102                                                         .getLocalhostURL(store, previewFile)) == null) {
103                                                 MessageDialog
104                                                                 .openInformation(shell,
105                                                                                 "Couldn't create localhost URL",
106                                                                                 "Please configure your localhost and documentRoot");
107                                                 return;
108                                         }
109
110                                         try {
111                                                 // if
112                                                 // (store.getBoolean(PHPeclipsePlugin.USE_EXTERNAL_BROWSER_PREF))
113                                                 // {
114                                                 // String[] arguments = { localhostURL };
115                                                 // MessageFormat form = new
116                                                 // MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF));
117                                                 // Runtime runtime = Runtime.getRuntime();
118                                                 // String command = form.format(arguments);
119                                                 // // console.write("External Browser command: " +
120                                                 // command + "\n");
121                                                 // runtime.exec(command);
122                                                 // } else {
123                                                 open(new URL(localhostURL), shell, localhostURL);
124                                                 // }
125                                         } catch (MalformedURLException e) {
126                                                 MessageDialog.openInformation(shell,
127                                                                 "MalformedURLException: ", e.toString());
128                                         }
129                                 }
130                         }
131                 }
132         }
133
134         /**
135          * @see IActionDelegate#selectionChanged(IAction, ISelection)
136          */
137         public void selectionChanged(IAction action, ISelection selection) {
138         }
139
140         public static void open(final URL url, final Shell shell,
141                         final String dialogTitle) {
142                 // if (WebBrowserUtil.canUseInternalWebBrowser()) {
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                 // } else {
155                 BrowserManager manager = BrowserManager.getInstance();
156                 IWebBrowser browser = manager.getCurrentWebBrowser();
157                 browser.openURL(url);
158                 // }
159         }
160 }