Fixed imports to refer to Quantum DB 2.4.5 instead of the Quantum DB CVS HEAD.
[phpeclipse.git] / net.sourceforge.phpeclipse.webbrowser / src / net / sourceforge / phpeclipse / webbrowser / internal / OpenWithBrowserActionDelegate.java
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  * Action to open the Web broswer on a resource.
26  */
27 public class OpenWithBrowserActionDelegate implements IActionDelegate {
28         private IResource resource;
29
30         /**
31          * OpenBrowserAction constructor comment.
32          */
33         public OpenWithBrowserActionDelegate() {
34                 super();
35         }
36
37         /**
38          * Performs this action.
39          * <p>
40          * This method is called when the delegating action has been triggered.
41          * Implement this method to do the actual work.
42          * </p>
43          *
44          * @param action the action proxy that handles the presentation portion of the
45          *   action
46          */
47         public void run(IAction action) {
48                 URL url = null;
49                 try {
50                         url = new URL("file://" + resource.getLocation());
51                         WebBrowser.openURL(new WebBrowserEditorInput(url, WebBrowserEditorInput.SHOW_ALL | WebBrowserEditorInput.FORCE_NEW_PAGE));
52                 } catch (Exception e) {
53                         Trace.trace(Trace.SEVERE, "Error opening browser on file", e);
54                 }
55         }
56
57         /**
58          * Notifies this action delegate that the selection in the workbench has changed.
59          * <p>
60          * Implementers can use this opportunity to change the availability of the
61          * action or to modify other presentation properties.
62          * </p>
63          *
64          * @param action the action proxy that handles presentation portion of the action
65          * @param selection the current selection in the workbench
66          */
67         public void selectionChanged(IAction action, ISelection sel) {
68                 if (sel.isEmpty() || !(sel instanceof IStructuredSelection)) {
69                         action.setEnabled(false);
70                         return;
71                 }
72         
73                 IStructuredSelection select = (IStructuredSelection) sel;
74                 Iterator iterator = select.iterator();
75                 Object selection = iterator.next();
76                 if (iterator.hasNext()) { // more than one selection (should never happen)
77                         action.setEnabled(false);
78                         return;
79                 }
80         
81                 if (!(selection instanceof IResource)) {
82                         action.setEnabled(false);
83                         return;
84                 }
85         
86                 resource = (IResource) selection;
87                 action.setEnabled(true);
88         }
89 }