committing phphelp to Plug-ins for feature to fix build dependence for phpmanual.
[phpeclipse.git] / net.sourceforge.phpeclipse / 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.editor.ShowExternalPreviewAction;
18 import net.sourceforge.phpeclipse.ui.overlaypages.ProjectPrefUtil;
19 import net.sourceforge.phpeclipse.webbrowser.IWebBrowser;
20 import net.sourceforge.phpeclipse.webbrowser.internal.BrowserManager;
21
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.IResource;
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.IActionDelegate;
32 import org.eclipse.ui.IObjectActionDelegate;
33 import org.eclipse.ui.IWorkbenchPart;
34
35 public class PHPEclipseShowAction implements IObjectActionDelegate {
36         private IWorkbenchPart workbenchPart;
37
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 void run(IAction action) {
53                 ISelectionProvider selectionProvider = null;
54                 selectionProvider = workbenchPart.getSite().getSelectionProvider();
55                 StructuredSelection selection = null;
56                 selection = (StructuredSelection) selectionProvider.getSelection();
57                 IPreferenceStore store = PHPeclipsePlugin.getDefault()
58                                 .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 previewFile = (IFile) resource;
73                                         String extension = previewFile.getFileExtension()
74                                                         .toLowerCase();
75                                         boolean bringToTopPreview = ProjectPrefUtil
76                                                         .getPreviewBooleanValue(
77                                                                         previewFile,
78                                                                         IPreferenceConstants.PHP_BRING_TO_TOP_PREVIEW_DEFAULT);
79                                         // boolean showHTMLFilesLocal =
80                                         // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
81                                         // IPreferenceConstants.PHP_SHOW_HTML_FILES_LOCAL);
82                                         // boolean showXMLFilesLocal =
83                                         // ProjectPrefUtil.getPreviewBooleanValue(previewFile,
84                                         // IPreferenceConstants.PHP_SHOW_XML_FILES_LOCAL);
85                                         boolean isHTMLFileName = "html".equals(extension)
86                                                         || "htm".equals(extension)
87                                                         || "xhtml".equals(extension);
88                                         boolean isXMLFileName = "xml".equals(extension)
89                                                         || "xsd".equals(extension)
90                                                         || "dtd".equals(extension);
91
92                                         String localhostURL;
93                                         // if (showHTMLFilesLocal && isHTMLFileName) {
94                                         // localhostURL =
95                                         // "file://"+previewFile.getLocation().toString();
96                                         // } else if (showXMLFilesLocal && isXMLFileName) {
97                                         // localhostURL =
98                                         // "file://"+previewFile.getLocation().toString();
99                                         // } else
100                                         if ((localhostURL = ShowExternalPreviewAction
101                                                         .getLocalhostURL(store, previewFile)) == null) {
102                                                 MessageDialog
103                                                                 .openInformation(shell,
104                                                                                 "Couldn't create localhost URL",
105                                                                                 "Please configure your localhost and documentRoot");
106                                                 return;
107                                         }
108
109                                         try {
110                                                 // if
111                                                 // (store.getBoolean(PHPeclipsePlugin.USE_EXTERNAL_BROWSER_PREF))
112                                                 // {
113                                                 // String[] arguments = { localhostURL };
114                                                 // MessageFormat form = new
115                                                 // MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF));
116                                                 // Runtime runtime = Runtime.getRuntime();
117                                                 // String command = form.format(arguments);
118                                                 // // console.write("External Browser command: " +
119                                                 // command + "\n");
120                                                 // runtime.exec(command);
121                                                 // } else {
122                                                 open(new URL(localhostURL), shell, localhostURL);
123                                                 // }
124                                         } catch (MalformedURLException e) {
125                                                 MessageDialog.openInformation(shell,
126                                                                 "MalformedURLException: ", e.toString());
127                                         }
128                                 }
129                         }
130                 }
131         }
132
133         /**
134          * @see IActionDelegate#selectionChanged(IAction, ISelection)
135          */
136         public void selectionChanged(IAction action, ISelection selection) {
137         }
138
139         public static void open(final URL url, final Shell shell,
140                         final String dialogTitle) {
141                 // if (WebBrowserUtil.canUseInternalWebBrowser()) {
142                 // IWorkbenchPage page = PHPeclipsePlugin.getActivePage();
143                 // try {
144                 // IViewPart part = page.findView(BrowserView.ID_BROWSER);
145                 // if (part == null) {
146                 // part = page.showView(BrowserView.ID_BROWSER);
147                 // } else {
148                 // page.bringToTop(part);
149                 // }
150                 // ((BrowserView) part).setUrl(url.toExternalForm());
151                 // } catch (Exception e) {
152                 // }
153                 // } else {
154                 BrowserManager manager = BrowserManager.getInstance();
155                 IWebBrowser browser = manager.getCurrentWebBrowser();
156                 browser.openURL(url);
157                 // }
158         }
159 }