MessageEvent.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:4k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: MessageEvent.java,v 1.2 2005/10/10 18:03:09 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx.event;
  22. import java.util.EventObject;
  23. import java.util.logging.Level;
  24. /**
  25.  * Represents a message passed from a MessageSource. This class contains
  26.  * properties which indicate the level of the message, a string which represents
  27.  * the user visible message text and an indication of when the message
  28.  * occured.
  29.  * <p>
  30.  * The message could represent text messages and exceptions. If this message
  31.  * represents an exception then the value of {@link #getThrowable} will be 
  32.  * non null. Messages are categorized using the
  33.  * {@link java.util.logging.Level } constants. 
  34.  * <p>
  35.  */
  36. public class MessageEvent extends EventObject {
  37.     private Object value;
  38.     private long when;
  39.     private Level level = Level.INFO;
  40.     // XXX This is only defined so that subclasses can get access to
  41.     // EventObject(Object)
  42.     public MessageEvent(Object source) {
  43. super(source);
  44.     }
  45.     /**
  46.      * Create a <code>Level.INFO</code> message.
  47.      */
  48.     public MessageEvent(Object source, Object message) {
  49. this(source, message, Level.INFO);
  50.     }
  51.     
  52.     public MessageEvent(Object source, Object value, Level level) {
  53. this(source, value, level, 0L);
  54.     }
  55.     /**
  56.      * Constructs a <code>MessageEvent</code>
  57.      *
  58.      * @param source the object that originated the event
  59.      * @param value an object which represents the contents of the event
  60.      * @param level indicate the level of the event
  61.      * @param when timestamp of the message
  62.      */
  63.     public MessageEvent(Object source, Object value, Level level, long when) {
  64. super(source);
  65. this.value = value;
  66. this.level = level;
  67. this.when = when;
  68.     }
  69.     /**
  70.      * Returns the value as a String message. If the value represents an
  71.      * exception, then the message from the exception is returned.
  72.      * 
  73.      * @return the value as a String or the empty string if the value is null
  74.      */
  75.     public String getMessage() {
  76. if (value != null) {
  77.     Throwable t = getThrowable();
  78.     if (t != null) {
  79. return t.getMessage();
  80.     } else {
  81. return value.toString();
  82.     }
  83. } else {
  84.     return "";
  85. }
  86.     }
  87.     /**
  88.      * Returns the value as a Throwable. 
  89.      *
  90.      * @return the exception passed as a value or null if it is not an exception
  91.      */
  92.     public Throwable getThrowable() {
  93. if (value != null && value instanceof Throwable) {
  94.     return (Throwable)value;
  95. }
  96. return null;
  97.     }
  98.     /**
  99.      * Returns the contents of the message. This level is based on the
  100.      * context of the message.
  101.      */
  102.     public Object getValue() {
  103. return value;
  104.     }
  105.     /**
  106.      * Time in milliseconds when the event occured.
  107.      */
  108.     public long getWhen() {
  109. return when;
  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 getLevel() {
  118. if (level == null) {
  119.     level = Level.INFO;
  120. }
  121. return level;
  122.     }
  123.     public String toString() {
  124. String message = "value=" + getMessage();
  125. message += ", level=" + getLevel();
  126. message += ", when=" + getWhen();
  127. return message;
  128.     }
  129. }