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 net.sourceforge.phpeclipse.webbrowser.internal;
14 import java.util.Iterator;
16 import net.sourceforge.phpeclipse.webbrowser.WebBrowser;
17 import net.sourceforge.phpeclipse.webbrowser.WebBrowserEditorInput;
18 import net.sourceforge.phpeclipse.webbrowser.internal.Trace;
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;
26 * Action to open the Web broswer on a resource.
28 public class OpenWithBrowserActionDelegate implements IActionDelegate {
29 private IResource resource;
32 * OpenBrowserAction constructor comment.
34 public OpenWithBrowserActionDelegate() {
39 * Performs this action.
41 * This method is called when the delegating action has been triggered.
42 * Implement this method to do the actual work.
45 * @param action the action proxy that handles the presentation portion of the
48 public void run(IAction action) {
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);
59 * Notifies this action delegate that the selection in the workbench has changed.
61 * Implementers can use this opportunity to change the availability of the
62 * action or to modify other presentation properties.
65 * @param action the action proxy that handles presentation portion of the action
66 * @param selection the current selection in the workbench
68 public void selectionChanged(IAction action, ISelection sel) {
69 if (sel.isEmpty() || !(sel instanceof IStructuredSelection)) {
70 action.setEnabled(false);
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);
82 if (!(selection instanceof IResource)) {
83 action.setEnabled(false);
87 resource = (IResource) selection;
88 action.setEnabled(true);