001/*
002 * $Id: OpenBrowserAction.java 3972 2011-03-17 20:31:58Z kschaefe $
003 *
004 * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle,
005 * Santa Clara, California 95054, U.S.A. All rights reserved.
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015 * Lesser General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this library; if not, write to the Free Software
019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020 */
021package org.jdesktop.swingx.action;
022
023import java.awt.Desktop;
024import java.awt.event.ActionEvent;
025import java.io.IOException;
026import java.net.URI;
027import java.net.URISyntaxException;
028import java.net.URL;
029import java.util.logging.Level;
030import java.util.logging.Logger;
031
032import javax.swing.AbstractAction;
033
034/**
035 * An action for opening a {@link URI} in a browser. The URI may be {@code null} and if so this
036 * action does nothing.
037 * 
038 * @author Karl Schaefer
039 * @author joshy (original version)
040 */
041public class OpenBrowserAction extends AbstractAction {
042    private static Logger log = Logger.getLogger(OpenBrowserAction.class.getName());
043
044    private URI uri;
045    
046    /** Creates a new instance of OpenBrowserAction */
047    public OpenBrowserAction() {
048        this((URI) null);
049    }
050
051    /**
052     * Creates a new action for the specified URI.
053     * 
054     * @param uri
055     *            the URI
056     * @throws NullPointerException
057     *             if {@code uri} is {@code null}
058     * @throws IllegalArgumentException
059     *             if the given string violates RFC 2396
060     */
061    public OpenBrowserAction(String uri) {
062        this(URI.create(uri));
063    }
064    
065    /**
066     * Creates a new action for the specified URL.
067     * 
068     * @param url
069     *            the URL
070     * @throws URISyntaxException
071     *             if the URL cannot be converted to a valid URI
072     */
073    public OpenBrowserAction(URL url) throws URISyntaxException {
074        this(url.toURI());
075    }
076    
077    /**
078     * Creates a new action for the specified URI.
079     * 
080     * @param uri
081     *            the URI
082     */
083    public OpenBrowserAction(URI uri) {
084        setURI(uri);
085    }
086    
087    /**
088     * Gets the current URI.
089     * 
090     * @return the URI
091     */
092    public URI getURI() {
093        return uri;
094    }
095
096    /**
097     * Sets the current URI.
098     * 
099     * @param uri
100     *            the new URI
101     */
102    public void setURI(URI uri) {
103        this.uri = uri;
104    }
105    
106    /**
107     * {@inheritDoc}
108     */
109    @Override
110    public void actionPerformed(ActionEvent e) {
111        if (uri == null || !Desktop.isDesktopSupported()) {
112            return;
113        }
114        
115        if (Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
116            try {
117                Desktop.getDesktop().browse(uri);
118            } catch (IOException ioe) {
119                log.log(Level.WARNING, "unable to browse: " + uri, ioe);
120            }
121        }
122    }
123}