source is now submenu in edit menu
[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.io.IOException;
12 import java.net.MalformedURLException;
13 import java.net.URL;
14 import java.text.MessageFormat;
15 import java.util.Iterator;
16
17 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
18 import net.sourceforge.phpeclipse.ui.IPreferenceConstants;
19 import net.sourceforge.phpeclipse.ui.editor.ShowExternalPreviewAction;
20 import net.sourceforge.phpeclipse.ui.overlaypages.ProjectPrefUtil;
21 import net.sourceforge.phpeclipse.webbrowser.IWebBrowser;
22 import net.sourceforge.phpeclipse.webbrowser.internal.BrowserManager;
23 import net.sourceforge.phpeclipse.webbrowser.internal.WebBrowserUtil;
24 import net.sourceforge.phpeclipse.webbrowser.views.BrowserView;
25
26 import org.eclipse.core.resources.IFile;
27 import org.eclipse.core.resources.IResource;
28 import org.eclipse.jface.action.IAction;
29 import org.eclipse.jface.dialogs.MessageDialog;
30 import org.eclipse.jface.preference.IPreferenceStore;
31 import org.eclipse.jface.viewers.ISelection;
32 import org.eclipse.jface.viewers.ISelectionProvider;
33 import org.eclipse.jface.viewers.StructuredSelection;
34 import org.eclipse.swt.widgets.Shell;
35 import org.eclipse.ui.IActionDelegate;
36 import org.eclipse.ui.IObjectActionDelegate;
37 import org.eclipse.ui.IViewPart;
38 import org.eclipse.ui.IWorkbenchPage;
39 import org.eclipse.ui.IWorkbenchPart;
40
41
42 public class PHPEclipseShowAction implements IObjectActionDelegate {
43   private IWorkbenchPart workbenchPart;
44
45   /**
46    * Constructor for Action1.
47    */
48   public PHPEclipseShowAction() {
49     super();
50   }
51
52   /**
53    * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
54    */
55   public void setActivePart(IAction action, IWorkbenchPart targetPart) {
56     workbenchPart = targetPart;
57   }
58
59   public void run(IAction action) {
60     ISelectionProvider selectionProvider = null;
61     selectionProvider = workbenchPart.getSite().getSelectionProvider();
62     StructuredSelection selection = null;
63     selection = (StructuredSelection) selectionProvider.getSelection();
64     //    PHPConsole console = PHPConsole.getInstance();
65     IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
66     Shell shell = null;
67     Iterator iterator = null;
68     iterator = selection.iterator();
69     while (iterator.hasNext()) {
70       //  obj => selected object in the view
71       Object obj = iterator.next();
72       // is it a resource
73       if (obj instanceof IResource) {
74         IResource resource = (IResource) obj;
75         // check if it's a file resource
76         switch (resource.getType()) {
77         case IResource.FILE:
78           // single file:
79           IFile previewFile = (IFile) resource;
80           String extension = previewFile.getFileExtension().toLowerCase();
81           boolean bringToTopPreview = ProjectPrefUtil.getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_BRING_TO_TOP_PREVIEW_DEFAULT);
82           boolean showHTMLFilesLocal = ProjectPrefUtil.getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_SHOW_HTML_FILES_LOCAL);
83           boolean showXMLFilesLocal = ProjectPrefUtil.getPreviewBooleanValue(previewFile, IPreferenceConstants.PHP_SHOW_XML_FILES_LOCAL);
84           boolean isHTMLFileName = "html".equals(extension) || "htm".equals(extension) || "xhtml".equals(extension);
85           boolean isXMLFileName = "xml".equals(extension) || "xsd".equals(extension) || "dtd".equals(extension);
86
87           String localhostURL;
88           if (showHTMLFilesLocal && isHTMLFileName) {
89             localhostURL = "file://"+previewFile.getLocation().toString();
90           } else if (showXMLFilesLocal && isXMLFileName) {
91             localhostURL = "file://"+previewFile.getLocation().toString();
92           } else if ((localhostURL = ShowExternalPreviewAction.getLocalhostURL(store, previewFile)) == null) {
93             MessageDialog.openInformation(shell, "Couldn't create localhost URL",
94             "Please configure your localhost and documentRoot");
95             return;
96           }
97         
98           try {
99 //            if (store.getBoolean(PHPeclipsePlugin.USE_EXTERNAL_BROWSER_PREF)) {
100 //              String[] arguments = { localhostURL };
101 //              MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_BROWSER_PREF));
102 //              Runtime runtime = Runtime.getRuntime();
103 //              String command = form.format(arguments);
104 //              //                console.write("External Browser command: " + command + "\n");
105 //              runtime.exec(command);
106 //            } else {
107               open(new URL(localhostURL), shell, localhostURL);
108 //            }
109           } catch (MalformedURLException e) {
110             MessageDialog.openInformation(shell, "MalformedURLException: ", e.toString());
111           }
112         }
113       }
114     }
115   }
116
117   /**
118    * @see IActionDelegate#selectionChanged(IAction, ISelection)
119    */
120   public void selectionChanged(IAction action, ISelection selection) {
121   }
122
123   public static void open(final URL url, final Shell shell, final String dialogTitle) {
124 //    if (WebBrowserUtil.canUseInternalWebBrowser()) {
125 //      IWorkbenchPage page = PHPeclipsePlugin.getActivePage();
126 //      try {
127 //        IViewPart part = page.findView(BrowserView.ID_BROWSER);
128 //        if (part == null) {
129 //          part = page.showView(BrowserView.ID_BROWSER);
130 //        } else {
131 //          page.bringToTop(part);
132 //        }
133 //        ((BrowserView) part).setUrl(url.toExternalForm());
134 //      } catch (Exception e) {
135 //      }
136 //    } else {
137       BrowserManager manager = BrowserManager.getInstance();
138       IWebBrowser browser = manager.getCurrentWebBrowser();
139       browser.openURL(url);
140 //    }
141   }
142 }