Better event handling for browser preview refreshs in Wikipedia plugin
[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 import net.sourceforge.phpeclipse.webbrowser.internal.Trace;
19
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.jface.action.IAction;
23 import org.eclipse.ui.*;
24 import org.eclipse.core.resources.IResource;
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 the action proxy that handles the presentation portion of the
46          *   action
47          */
48         public void run(IAction action) {
49                 URL url = null;
50                 try {
51                         url = new URL("file://" + resource.getLocation());
52                         WebBrowser.openURL(new WebBrowserEditorInput(url, WebBrowserEditorInput.SHOW_ALL | WebBrowserEditorInput.FORCE_NEW_PAGE));
53                 } catch (Exception e) {
54                         Trace.trace(Trace.SEVERE, "Error opening browser on file", e);
55                 }
56         }
57
58         /**
59          * Notifies this action delegate that the selection in the workbench has changed.
60          * <p>
61          * Implementers can use this opportunity to change the availability of the
62          * action or to modify other presentation properties.
63          * </p>
64          *
65          * @param action the action proxy that handles presentation portion of the action
66          * @param selection the current selection in the workbench
67          */
68         public void selectionChanged(IAction action, ISelection sel) {
69                 if (sel.isEmpty() || !(sel instanceof IStructuredSelection)) {
70                         action.setEnabled(false);
71                         return;
72                 }
73         
74                 IStructuredSelection select = (IStructuredSelection) sel;
75                 Iterator iterator = select.iterator();
76                 Object selection = iterator.next();
77                 if (iterator.hasNext()) { // more than one selection (should never happen)
78                         action.setEnabled(false);
79                         return;
80                 }
81         
82                 if (!(selection instanceof IResource)) {
83                         action.setEnabled(false);
84                         return;
85                 }
86         
87                 resource = (IResource) selection;
88                 action.setEnabled(true);
89         }
90 }