1 /*
2 * Copyright 1997-2001 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any 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<sup><font size="-2">TM</font></sup>
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 public class HyperlinkEvent extends EventObject {
49
50 /**
51 * Creates a new object representing a hypertext link event.
52 * The other constructor is preferred, as it provides more
53 * information if a URL could not be formed. This constructor
54 * is primarily for backward compatibility.
55 *
56 * @param source the object responsible for the event
57 * @param type the event type
58 * @param u the affected URL
59 */
60 public HyperlinkEvent(Object source, EventType type, URL u) {
61 this(source, type, u, null);
62 }
63
64 /**
65 * Creates a new object representing a hypertext link event.
66 *
67 * @param source the object responsible for the event
68 * @param type the event type
69 * @param u the affected URL. This may be null if a valid URL
70 * could not be created.
71 * @param desc the description of the link. This may be useful
72 * when attempting to form a URL resulted in a MalformedURLException.
73 * The description provides the text used when attempting to form the
74 * URL.
75 */
76 public HyperlinkEvent(Object source, EventType type, URL u, String desc) {
77 this(source, type, u, desc, null);
78 }
79
80 /**
81 * Creates a new object representing a hypertext link event.
82 *
83 * @param source the object responsible for the event
84 * @param type the event type
85 * @param u the affected URL. This may be null if a valid URL
86 * could not be created.
87 * @param desc the description of the link. This may be useful
88 * when attempting to form a URL resulted in a MalformedURLException.
89 * The description provides the text used when attempting to form the
90 * URL.
91 * @param sourceElement Element in the Document representing the
92 * anchor
93 * @since 1.4
94 */
95 public HyperlinkEvent(Object source, EventType type, URL u, String desc,
96 Element sourceElement) {
97 super(source);
98 this.type = type;
99 this.u = u;
100 this.desc = desc;
101 this.sourceElement = sourceElement;
102 }
103
104 /**
105 * Creates a new object representing a hypertext link event.
106 *
107 * @param source the object responsible for the event
108 * @param type the event type
109 * @param u the affected URL. This may be null if a valid URL
110 * could not be created.
111 * @param desc the description of the link. This may be useful
112 * when attempting to form a URL resulted in a MalformedURLException.
113 * The description provides the text used when attempting to form the
114 * URL.
115 * @param sourceElement Element in the Document representing the
116 * anchor
117 * @param inputEvent InputEvent that triggered the hyperlink event
118 * @since 1.7
119 */
120 public HyperlinkEvent(Object source, EventType type, URL u, String desc,
121 Element sourceElement, InputEvent inputEvent) {
122 super(source);
123 this.type = type;
124 this.u = u;
125 this.desc = desc;
126 this.sourceElement = sourceElement;
127 this.inputEvent = inputEvent;
128 }
129
130 /**
131 * Gets the type of event.
132 *
133 * @return the type
134 */
135 public EventType getEventType() {
136 return type;
137 }
138
139 /**
140 * Get the description of the link as a string.
141 * This may be useful if a URL can't be formed
142 * from the description, in which case the associated
143 * URL would be null.
144 */
145 public String getDescription() {
146 return desc;
147 }
148
149 /**
150 * Gets the URL that the link refers to.
151 *
152 * @return the URL
153 */
154 public URL getURL() {
155 return u;
156 }
157
158 /**
159 * Returns the <code>Element</code> that corresponds to the source of the
160 * event. This will typically be an <code>Element</code> representing
161 * an anchor. If a constructur that is used that does not specify a source
162 * <code>Element</code>, or null was specified as the source
163 * <code>Element</code>, this will return null.
164 *
165 * @return Element indicating source of event, or null
166 * @since 1.4
167 */
168 public Element getSourceElement() {
169 return sourceElement;
170 }
171
172 /**
173 * Returns the {@code InputEvent} that triggered the hyperlink event.
174 * This will typically be a {@code MouseEvent}. If a constructor is used
175 * that does not specify an {@code InputEvent}, or @{code null}
176 * was specified as the {@code InputEvent}, this returns {@code null}.
177 *
178 * @return InputEvent that triggered the hyperlink event, or null
179 * @since 1.7
180 */
181 public InputEvent getInputEvent() {
182 return inputEvent;
183 }
184
185 private EventType type;
186 private URL u;
187 private String desc;
188 private Element sourceElement;
189 private InputEvent inputEvent;
190
191
192 /**
193 * Defines the ENTERED, EXITED, and ACTIVATED event types, along
194 * with their string representations, returned by toString().
195 */
196 public static final class EventType {
197
198 private EventType(String s) {
199 typeString = s;
200 }
201
202 /**
203 * Entered type.
204 */
205 public static final EventType ENTERED = new EventType("ENTERED");
206
207 /**
208 * Exited type.
209 */
210 public static final EventType EXITED = new EventType("EXITED");
211
212 /**
213 * Activated type.
214 */
215 public static final EventType ACTIVATED = new EventType("ACTIVATED");
216
217 /**
218 * Converts the type to a string.
219 *
220 * @return the string
221 */
222 public String toString() {
223 return typeString;
224 }
225
226 private String typeString;
227 }
228 }