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
9 * IBM - Initial API and implementation
11 package org.eclipse.webbrowser.internal;
14 import java.util.Iterator;
15 import org.eclipse.jface.viewers.ISelection;
16 import org.eclipse.jface.viewers.IStructuredSelection;
17 import org.eclipse.jface.action.IAction;
18 import org.eclipse.ui.*;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.webbrowser.WebBrowser;
21 import org.eclipse.webbrowser.WebBrowserEditorInput;
22 import org.eclipse.webbrowser.internal.Trace;
24 * Action to open the Web broswer on a resource.
26 public class OpenWithBrowserActionDelegate implements IActionDelegate {
27 private IResource resource;
30 * OpenBrowserAction constructor comment.
32 public OpenWithBrowserActionDelegate() {
37 * Performs this action.
39 * This method is called when the delegating action has been triggered.
40 * Implement this method to do the actual work.
43 * @param action the action proxy that handles the presentation portion of the
46 public void run(IAction action) {
49 url = new URL("file://" + resource.getLocation());
50 WebBrowser.openURL(new WebBrowserEditorInput(url, WebBrowserEditorInput.SHOW_ALL | WebBrowserEditorInput.FORCE_NEW_PAGE));
51 } catch (Exception e) {
52 Trace.trace(Trace.SEVERE, "Error opening browser on file", e);
57 * Notifies this action delegate that the selection in the workbench has changed.
59 * Implementers can use this opportunity to change the availability of the
60 * action or to modify other presentation properties.
63 * @param action the action proxy that handles presentation portion of the action
64 * @param selection the current selection in the workbench
66 public void selectionChanged(IAction action, ISelection sel) {
67 if (sel.isEmpty() || !(sel instanceof IStructuredSelection)) {
68 action.setEnabled(false);
72 IStructuredSelection select = (IStructuredSelection) sel;
73 Iterator iterator = select.iterator();
74 Object selection = iterator.next();
75 if (iterator.hasNext()) { // more than one selection (should never happen)
76 action.setEnabled(false);
80 if (!(selection instanceof IResource)) {
81 action.setEnabled(false);
85 resource = (IResource) selection;
86 action.setEnabled(true);