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