001 /* 002 * $Id: MessageEvent.java,v 1.3 2005/10/26 14:29:56 kleopatra Exp $ 003 * 004 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle, 005 * Santa Clara, California 95054, U.S.A. All rights reserved. 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * You should have received a copy of the GNU Lesser General Public 018 * License along with this library; if not, write to the Free Software 019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 020 */ 021 022 package org.jdesktop.swingx.event; 023 024 import java.util.EventObject; 025 import java.util.logging.Level; 026 027 /** 028 * Represents a message passed from a MessageSource. This class contains 029 * properties which indicate the level of the message, a string which represents 030 * the user visible message text and an indication of when the message 031 * occured. 032 * <p> 033 * The message could represent text messages and exceptions. If this message 034 * represents an exception then the value of {@link #getThrowable} will be 035 * non null. Messages are categorized using the 036 * {@link java.util.logging.Level } constants. 037 * <p> 038 */ 039 public class MessageEvent extends EventObject { 040 041 private Object value; 042 private long when; 043 private Level level = Level.INFO; 044 045 // XXX This is only defined so that subclasses can get access to 046 // EventObject(Object) 047 public MessageEvent(Object source) { 048 super(source); 049 } 050 051 /** 052 * Create a <code>Level.INFO</code> message. 053 */ 054 public MessageEvent(Object source, Object message) { 055 this(source, message, Level.INFO); 056 } 057 058 059 public MessageEvent(Object source, Object value, Level level) { 060 this(source, value, level, 0L); 061 } 062 063 /** 064 * Constructs a <code>MessageEvent</code> 065 * 066 * @param source the object that originated the event 067 * @param value an object which represents the contents of the event 068 * @param level indicate the level of the event 069 * @param when timestamp of the message 070 */ 071 public MessageEvent(Object source, Object value, Level level, long when) { 072 super(source); 073 this.value = value; 074 this.level = level; 075 this.when = when; 076 } 077 078 /** 079 * Returns the value as a String message. If the value represents an 080 * exception, then the message from the exception is returned. 081 * 082 * @return the value as a String or the empty string if the value is null 083 */ 084 public String getMessage() { 085 if (value != null) { 086 Throwable t = getThrowable(); 087 if (t != null) { 088 return t.getMessage(); 089 } else { 090 return value.toString(); 091 } 092 } else { 093 return ""; 094 } 095 } 096 097 /** 098 * Returns the value as a Throwable. 099 * 100 * @return the exception passed as a value or null if it is not an exception 101 */ 102 public Throwable getThrowable() { 103 if (value != null && value instanceof Throwable) { 104 return (Throwable)value; 105 } 106 return null; 107 } 108 109 /** 110 * Returns the contents of the message. This level is based on the 111 * context of the message. 112 */ 113 public Object getValue() { 114 return value; 115 } 116 117 /** 118 * Time in milliseconds when the event occured. 119 */ 120 public long getWhen() { 121 return when; 122 } 123 124 /** 125 * Returns the level of message. This method will always return a valid 126 * value. The default is set to Level.INFO. 127 * 128 * @return the level of the message 129 */ 130 public Level getLevel() { 131 if (level == null) { 132 level = Level.INFO; 133 } 134 return level; 135 } 136 137 public String toString() { 138 String message = "value=" + getMessage(); 139 message += ", level=" + getLevel(); 140 message += ", when=" + getWhen(); 141 142 return message; 143 } 144 }