18cdc659ef714703ce68511b98703356810912e1
[phpeclipse.git] /
1 /**
2  * Copyright (c) 2003 IBM Corporation 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 - Initial API and implementation
10  */
11 package net.sourceforge.phpeclipse.webbrowser.internal;
12
13 import java.net.URL;
14 import java.util.Iterator;
15
16 import net.sourceforge.phpeclipse.webbrowser.WebBrowser;
17 import net.sourceforge.phpeclipse.webbrowser.WebBrowserEditorInput;
18
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.jface.action.IAction;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.ui.IActionDelegate;
24
25 /**
26  * Action to open the Web broswer on a resource.
27  */
28 public class OpenWithBrowserActionDelegate implements IActionDelegate {
29         private IResource resource;
30
31         /**
32          * OpenBrowserAction constructor comment.
33          */
34         public OpenWithBrowserActionDelegate() {
35                 super();
36         }
37
38         /**
39          * Performs this action.
40          * <p>
41          * This method is called when the delegating action has been triggered.
42          * Implement this method to do the actual work.
43          * </p>
44          * 
45          * @param action
46          *            the action proxy that handles the presentation portion of the
47          *            action
48          */
49         public void run(IAction action) {
50                 URL url = null;
51                 try {
52                         url = new URL("file://" + resource.getLocation());
53                         WebBrowser.openURL(new WebBrowserEditorInput(url,
54                                         WebBrowserEditorInput.SHOW_ALL
55                                                         | WebBrowserEditorInput.FORCE_NEW_PAGE));
56                 } catch (Exception e) {
57                         Trace.trace(Trace.SEVERE, "Error opening browser on file", e);
58                 }
59         }
60
61         /**
62          * Notifies this action delegate that the selection in the workbench has
63          * changed.
64          * <p>
65          * Implementers can use this opportunity to change the availability of the
66          * action or to modify other presentation properties.
67          * </p>
68          * 
69          * @param action
70          *            the action proxy that handles presentation portion of the
71          *            action
72          * @param selection
73          *            the current selection in the workbench
74          */
75         public void selectionChanged(IAction action, ISelection sel) {
76                 if (sel.isEmpty() || !(sel instanceof IStructuredSelection)) {
77                         action.setEnabled(false);
78                         return;
79                 }
80
81                 IStructuredSelection select = (IStructuredSelection) sel;
82                 Iterator iterator = select.iterator();
83                 Object selection = iterator.next();
84                 if (iterator.hasNext()) { // more than one selection (should never
85                                                                         // happen)
86                         action.setEnabled(false);
87                         return;
88                 }
89
90                 if (!(selection instanceof IResource)) {
91                         action.setEnabled(false);
92                         return;
93                 }
94
95                 resource = (IResource) selection;
96                 action.setEnabled(true);
97         }
98 }