1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package javax.swing.event;
  26 
  27 import java.awt.event.InputEvent;
  28 import java.util.EventObject;
  29 import java.net.URL;
  30 import javax.swing.text.Element;
  31 
  32 
  33 /**
  34  * HyperlinkEvent is used to notify interested parties that
  35  * something has happened with respect to a hypertext link.
  36  * <p>
  37  * <strong>Warning:</strong>
  38  * Serialized objects of this class will not be compatible with
  39  * future Swing releases. The current serialization support is
  40  * appropriate for short term storage or RMI between applications running
  41  * the same version of Swing.  As of 1.4, support for long term storage
  42  * of all JavaBeans&trade;
  43  * has been added to the <code>java.beans</code> package.
  44  * Please see {@link java.beans.XMLEncoder}.
  45  *
  46  * @author  Timothy Prinzing
  47  */
  48 @SuppressWarnings("serial") // Same-version serialization only
  49 public class HyperlinkEvent extends EventObject {
  50 
  51     /**
  52      * Creates a new object representing a hypertext link event.
  53      * The other constructor is preferred, as it provides more
  54      * information if a URL could not be formed.  This constructor
  55      * is primarily for backward compatibility.
  56      *
  57      * @param source the object responsible for the event
  58      * @param type the event type
  59      * @param u the affected URL
  60      */
  61     public HyperlinkEvent(Object source, EventType type, URL u) {
  62         this(source, type, u, null);
  63     }
  64 
  65     /**
  66      * Creates a new object representing a hypertext link event.
  67      *
  68      * @param source the object responsible for the event
  69      * @param type the event type
  70      * @param u the affected URL.  This may be null if a valid URL
  71      *   could not be created.
  72      * @param desc the description of the link.  This may be useful
  73      *   when attempting to form a URL resulted in a MalformedURLException.
  74      *   The description provides the text used when attempting to form the
  75      *   URL.
  76      */
  77     public HyperlinkEvent(Object source, EventType type, URL u, String desc) {
  78         this(source, type, u, desc, null);
  79     }
  80 
  81     /**
  82      * Creates a new object representing a hypertext link event.
  83      *
  84      * @param source the object responsible for the event
  85      * @param type the event type
  86      * @param u the affected URL.  This may be null if a valid URL
  87      *   could not be created.
  88      * @param desc the description of the link.  This may be useful
  89      *   when attempting to form a URL resulted in a MalformedURLException.
  90      *   The description provides the text used when attempting to form the
  91      *   URL.
  92      * @param sourceElement Element in the Document representing the
  93      *   anchor
  94      * @since 1.4
  95      */
  96     public HyperlinkEvent(Object source, EventType type, URL u, String desc,
  97                           Element sourceElement) {
  98         super(source);
  99         this.type = type;
 100         this.u = u;
 101         this.desc = desc;
 102         this.sourceElement = sourceElement;
 103     }
 104 
 105     /**
 106      * Creates a new object representing a hypertext link event.
 107      *
 108      * @param source the object responsible for the event
 109      * @param type the event type
 110      * @param u the affected URL.  This may be null if a valid URL
 111      *   could not be created.
 112      * @param desc the description of the link.  This may be useful
 113      *   when attempting to form a URL resulted in a MalformedURLException.
 114      *   The description provides the text used when attempting to form the
 115      *   URL.
 116      * @param sourceElement Element in the Document representing the
 117      *   anchor
 118      * @param inputEvent  InputEvent that triggered the hyperlink event
 119      * @since 1.7
 120      */
 121     public HyperlinkEvent(Object source, EventType type, URL u, String desc,
 122                           Element sourceElement, InputEvent inputEvent) {
 123         super(source);
 124         this.type = type;
 125         this.u = u;
 126         this.desc = desc;
 127         this.sourceElement = sourceElement;
 128         this.inputEvent = inputEvent;
 129     }
 130 
 131     /**
 132      * Gets the type of event.
 133      *
 134      * @return the type
 135      */
 136     public EventType getEventType() {
 137         return type;
 138     }
 139 
 140     /**
 141      * Get the description of the link as a string.
 142      * This may be useful if a URL can't be formed
 143      * from the description, in which case the associated
 144      * URL would be null.
 145      *
 146      * @return the description of this link as a {@code String}
 147      */
 148     public String getDescription() {
 149         return desc;
 150     }
 151 
 152     /**
 153      * Gets the URL that the link refers to.
 154      *
 155      * @return the URL
 156      */
 157     public URL getURL() {
 158         return u;
 159     }
 160 
 161     /**
 162      * Returns the <code>Element</code> that corresponds to the source of the
 163      * event. This will typically be an <code>Element</code> representing
 164      * an anchor. If a constructor that is used that does not specify a source
 165      * <code>Element</code>, or null was specified as the source
 166      * <code>Element</code>, this will return null.
 167      *
 168      * @return Element indicating source of event, or null
 169      * @since 1.4
 170      */
 171     public Element getSourceElement() {
 172         return sourceElement;
 173     }
 174 
 175     /**
 176      * Returns the {@code InputEvent} that triggered the hyperlink event.
 177      * This will typically be a {@code MouseEvent}.  If a constructor is used
 178      * that does not specify an {@code InputEvent}, or @{code null}
 179      * was specified as the {@code InputEvent}, this returns {@code null}.
 180      *
 181      * @return  InputEvent that triggered the hyperlink event, or null
 182      * @since 1.7
 183      */
 184     public InputEvent getInputEvent() {
 185         return inputEvent;
 186     }
 187 
 188     private EventType type;
 189     private URL u;
 190     private String desc;
 191     private Element sourceElement;
 192     private InputEvent inputEvent;
 193 
 194 
 195     /**
 196      * Defines the ENTERED, EXITED, and ACTIVATED event types, along
 197      * with their string representations, returned by toString().
 198      */
 199     public static final class EventType {
 200 
 201         private EventType(String s) {
 202             typeString = s;
 203         }
 204 
 205         /**
 206          * Entered type.
 207          */
 208         public static final EventType ENTERED = new EventType("ENTERED");
 209 
 210         /**
 211          * Exited type.
 212          */
 213         public static final EventType EXITED = new EventType("EXITED");
 214 
 215         /**
 216          * Activated type.
 217          */
 218         public static final EventType ACTIVATED = new EventType("ACTIVATED");
 219 
 220         /**
 221          * Converts the type to a string.
 222          *
 223          * @return the string
 224          */
 225         public String toString() {
 226             return typeString;
 227         }
 228 
 229         private String typeString;
 230     }
 231 }