001 /*
002 * $Id: OpenBrowserAction.java 3418 2009-07-27 15:49:55Z 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 */
021 package org.jdesktop.swingx.action;
022
023 import java.awt.Desktop;
024 import java.awt.event.ActionEvent;
025 import java.io.IOException;
026 import java.net.URI;
027 import java.net.URISyntaxException;
028 import java.net.URL;
029 import java.util.logging.Level;
030 import java.util.logging.Logger;
031
032 import 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 */
041 public 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 public void actionPerformed(ActionEvent e) {
110 if (uri == null || !Desktop.isDesktopSupported()) {
111 return;
112 }
113
114 if (Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
115 try {
116 Desktop.getDesktop().browse(uri);
117 } catch (IOException ioe) {
118 log.log(Level.WARNING, "unable to browse: " + uri, ioe);
119 }
120 }
121 }
122 }