1. /*
  2. * @(#)HyperlinkEvent.java 1.17 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.event;
  8. import java.util.EventObject;
  9. import java.net.URL;
  10. import javax.swing.text.Element;
  11. /**
  12. * HyperlinkEvent is used to notify interested parties that
  13. * something has happened with respect to a hypertext link.
  14. * <p>
  15. * <strong>Warning:</strong>
  16. * Serialized objects of this class will not be compatible with
  17. * future Swing releases. The current serialization support is
  18. * appropriate for short term storage or RMI between applications running
  19. * the same version of Swing. As of 1.4, support for long term storage
  20. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  21. * has been added to the <code>java.beans</code> package.
  22. * Please see {@link java.beans.XMLEncoder}.
  23. *
  24. * @version 1.17 01/23/03
  25. * @author Timothy Prinzing
  26. */
  27. public class HyperlinkEvent extends EventObject {
  28. /**
  29. * Creates a new object representing a hypertext link event.
  30. * The other constructor is preferred, as it provides more
  31. * information if a URL could not be formed. This constructor
  32. * is primarily for backward compatibility.
  33. *
  34. * @param source the object responsible for the event
  35. * @param type the event type
  36. * @param u the affected URL
  37. */
  38. public HyperlinkEvent(Object source, EventType type, URL u) {
  39. this(source, type, u, null);
  40. }
  41. /**
  42. * Creates a new object representing a hypertext link event.
  43. *
  44. * @param source the object responsible for the event
  45. * @param type the event type
  46. * @param u the affected URL. This may be null if a valid URL
  47. * could not be created.
  48. * @param desc the description of the link. This may be useful
  49. * when attempting to form a URL resulted in a MalformedURLException.
  50. * The description provides the text used when attempting to form the
  51. * URL.
  52. */
  53. public HyperlinkEvent(Object source, EventType type, URL u, String desc) {
  54. this(source, type, u, desc, null);
  55. }
  56. /**
  57. * Creates a new object representing a hypertext link event.
  58. *
  59. * @param source the object responsible for the event
  60. * @param type the event type
  61. * @param u the affected URL. This may be null if a valid URL
  62. * could not be created.
  63. * @param desc the description of the link. This may be useful
  64. * when attempting to form a URL resulted in a MalformedURLException.
  65. * The description provides the text used when attempting to form the
  66. * URL.
  67. * @param sourceElement Element in the Document representing the
  68. * anchor
  69. * @since 1.4
  70. */
  71. public HyperlinkEvent(Object source, EventType type, URL u, String desc,
  72. Element sourceElement) {
  73. super(source);
  74. this.type = type;
  75. this.u = u;
  76. this.desc = desc;
  77. this.sourceElement = sourceElement;
  78. }
  79. /**
  80. * Gets the type of event.
  81. *
  82. * @return the type
  83. */
  84. public EventType getEventType() {
  85. return type;
  86. }
  87. /**
  88. * Get the description of the link as a string.
  89. * This may be useful if a URL can't be formed
  90. * from the description, in which case the associated
  91. * URL would be null.
  92. */
  93. public String getDescription() {
  94. return desc;
  95. }
  96. /**
  97. * Gets the URL that the link refers to.
  98. *
  99. * @return the URL
  100. */
  101. public URL getURL() {
  102. return u;
  103. }
  104. /**
  105. * Returns the <code>Element</code> that corresponds to the source of the
  106. * event. This will typically be an <code>Element</code> representing
  107. * an anchor. If a constructur that is used that does not specify a source
  108. * <code>Element</code>, or null was specified as the source
  109. * <code>Element</code>, this will return null.
  110. *
  111. * @return Element indicating source of event, or null
  112. * @since 1.4
  113. */
  114. public Element getSourceElement() {
  115. return sourceElement;
  116. }
  117. private EventType type;
  118. private URL u;
  119. private String desc;
  120. private Element sourceElement;
  121. /**
  122. * Defines the ENTERED, EXITED, and ACTIVATED event types, along
  123. * with their string representations, returned by toString().
  124. */
  125. public static final class EventType {
  126. private EventType(String s) {
  127. typeString = s;
  128. }
  129. /**
  130. * Entered type.
  131. */
  132. public static final EventType ENTERED = new EventType("ENTERED");
  133. /**
  134. * Exited type.
  135. */
  136. public static final EventType EXITED = new EventType("EXITED");
  137. /**
  138. * Activated type.
  139. */
  140. public static final EventType ACTIVATED = new EventType("ACTIVATED");
  141. /**
  142. * Converts the type to a string.
  143. *
  144. * @return the string
  145. */
  146. public String toString() {
  147. return typeString;
  148. }
  149. private String typeString;
  150. }
  151. }