KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > event > MessageEvent


1 /*
2  * $Id: MessageEvent.java,v 1.1.1.1 2004/06/16 01:43:39 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing.event;
9
10 import java.util.EventObject JavaDoc;
11
12 import java.util.logging.Level JavaDoc;
13
14 /**
15  * Represents a message passed from a MessageSource. This class contains
16  * properties which indicate the level of the message, a string which represents
17  * the user visible message text and an indication of when the message
18  * occured.
19  * <p>
20  * The message could represent text messages and exceptions. If this message
21  * represents an exception then the value of {@link #getThrowable} will be
22  * non null. Messages are categorized using the
23  * {@link java.util.logging.Level } constants.
24  * <p>
25  */

26 public class MessageEvent extends EventObject JavaDoc {
27
28     private Object JavaDoc value;
29     private long when;
30     private Level JavaDoc level = Level.INFO;
31
32     // XXX This is only defined so that subclasses can get access to
33
// EventObject(Object)
34
public MessageEvent(Object JavaDoc source) {
35     super(source);
36     }
37
38     /**
39      * Create a <code>Level.INFO</code> message.
40      */

41     public MessageEvent(Object JavaDoc source, Object JavaDoc message) {
42     this(source, message, Level.INFO);
43     }
44
45     
46     public MessageEvent(Object JavaDoc source, Object JavaDoc value, Level JavaDoc level) {
47     this(source, value, level, 0L);
48     }
49
50     /**
51      * Constructs a <code>MessageEvent</code>
52      *
53      * @param source the object that originated the event
54      * @param value an object which represents the contents of the event
55      * @param level indicate the level of the event
56      * @param when timestamp of the message
57      */

58     public MessageEvent(Object JavaDoc source, Object JavaDoc value, Level JavaDoc level, long when) {
59     super(source);
60     this.value = value;
61     this.level = level;
62     this.when = when;
63     }
64
65     /**
66      * Returns the value as a String message. If the value represents an
67      * exception, then the message from the exception is returned.
68      *
69      * @return the value as a String or the empty string if the value is null
70      */

71     public String JavaDoc getMessage() {
72     if (value != null) {
73         Throwable JavaDoc t = getThrowable();
74         if (t != null) {
75         return t.getMessage();
76         } else {
77         return value.toString();
78         }
79     } else {
80         return "";
81     }
82     }
83
84     /**
85      * Returns the value as a Throwable.
86      *
87      * @return the exception passed as a value or null if it is not an exception
88      */

89     public Throwable JavaDoc getThrowable() {
90     if (value != null && value instanceof Throwable JavaDoc) {
91         return (Throwable JavaDoc)value;
92     }
93     return null;
94     }
95
96     /**
97      * Returns the contents of the message. This level is based on the
98      * context of the message.
99      */

100     public Object JavaDoc getValue() {
101     return value;
102     }
103
104     /**
105      * Time in milliseconds when the event occured.
106      */

107     public long getWhen() {
108     return when;
109     }
110
111     /**
112      * Returns the level of message. This method will always return a valid
113      * value. The default is set to Level.INFO.
114      *
115      * @return the level of the message
116      */

117     public Level JavaDoc getLevel() {
118     if (level == null) {
119         level = Level.INFO;
120     }
121     return level;
122     }
123
124     public String JavaDoc toString() {
125     String JavaDoc message = "value=" + getMessage();
126     message += ", level=" + getLevel();
127     message += ", when=" + getWhen();
128
129     return message;
130     }
131 }
132
Popular Tags