001/* 002 * jDTAUS Core API 003 * Copyright (C) 2005 Christian Schulte 004 * <cs@schulte.it> 005 * 006 * This library is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 2.1 of the License, or any later version. 010 * 011 * This library is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with this library; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 019 * 020 */ 021package org.jdtaus.core.text; 022 023import java.io.InvalidObjectException; 024import java.io.ObjectStreamException; 025import java.util.EventObject; 026import java.util.Locale; 027 028/** 029 * Event holding messages. 030 * 031 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 032 * @version $JDTAUS: MessageEvent.java 8641 2012-09-27 06:45:17Z schulte $ 033 * 034 * @see MessageEventSource 035 */ 036public class MessageEvent extends EventObject 037{ 038 //--Constants--------------------------------------------------------------- 039 040 /** Event constant for information messages. */ 041 public static final int INFORMATION = 2001; 042 043 /** Event constant for notification messages. */ 044 public static final int NOTIFICATION = 2002; 045 046 /** Event constant for warning messages. */ 047 public static final int WARNING = 2003; 048 049 /** Event constant for error messages. */ 050 public static final int ERROR = 2004; 051 052 /** Serial version UID for backwards compatibility with 1.0.x classes. */ 053 private static final long serialVersionUID = 7335694054201632443L; 054 055 //---------------------------------------------------------------Constants-- 056 //--Constructors------------------------------------------------------------ 057 058 /** 059 * Creates a new {@code MessageEvent} instance taking a single message. 060 * 061 * @param source the source of the new event. 062 * @param message the message for the new event. 063 * @param type constant indicating the type of the events' messages. 064 * 065 * @throws NullPointerException if {@code message} is {@code null}. 066 * @throws IllegalArgumentException if {@code source} is {@code null} or 067 * {@code type} is not equal to one of the constants {@code INFORMATION}, 068 * {@code NOTIFICATION}, {@code WARNING} or {@code ERROR}. 069 */ 070 public MessageEvent( final Object source, final Message message, 071 final int type ) 072 { 073 this( source, new Message[] 074 { 075 message 076 }, type ); 077 } 078 079 /** 080 * Creates a new {@code MessageEvent} instance taking multiple messages. 081 * 082 * @param source the source of the new event. 083 * @param messages the messages for the new event. 084 * @param type constant indicating the type of the events' messages. 085 * 086 * @throws NullPointerException if {@code messages} is {@code null}. 087 * @throws IllegalArgumentException if {@code source} is {@code null} or 088 * {@code type} is not equal to one of the constants {@code INFORMATION}, 089 * {@code NOTIFICATION}, {@code WARNING} or {@code ERROR} or the length of 090 * {@code messages} is zero. 091 */ 092 public MessageEvent( final Object source, final Message[] messages, 093 final int type ) 094 { 095 super( source ); 096 097 if ( messages == null ) 098 { 099 throw new NullPointerException( "messages" ); 100 } 101 if ( messages.length == 0 ) 102 { 103 throw new IllegalArgumentException( 104 Integer.toString( messages.length ) ); 105 106 } 107 108 this.assertValidType( type ); 109 110 this.message = messages[0]; 111 this.messages = messages; 112 this.type = type; 113 } 114 115 /** 116 * Checks an integer to be equal to one of the constants 117 * {@code INFORMATION}, {@code NOTIFICATION}, {@code WARNING} or 118 * {@code ERROR}. 119 * 120 * @param type the integer to check. 121 * 122 * @throws IllegalArgumentException if {@code type} is not equal to 123 * one of the constants {@code INFORMATION}, {@code NOTIFICATION}, 124 * {@code WARNING} and {@code ERROR}. 125 */ 126 private void assertValidType( final int type ) 127 { 128 if ( type != INFORMATION && type != NOTIFICATION && 129 type != WARNING && type != ERROR ) 130 { 131 throw new IllegalArgumentException( Integer.toString( type ) ); 132 } 133 } 134 135 //------------------------------------------------------------Constructors-- 136 //--MessageEvent------------------------------------------------------------ 137 138 /** 139 * Single message of the event. 140 * <p>Kept for backward compatibility with 1.0.x object streams.</p> 141 * @serial 142 */ 143 private final Message message; 144 145 /** 146 * Messages of the event. 147 * @serial 148 */ 149 private Message[] messages; 150 151 /** 152 * Type of the event. 153 * @serial 154 */ 155 private final int type; 156 157 /** 158 * Getter for property {@code message}. 159 * 160 * @return the single message of the event. 161 * 162 * @deprecated Replaced by {@link #getMessages()}. 163 */ 164 public final Message getMessage() 165 { 166 return this.message; 167 } 168 169 /** 170 * Getter for property {@code messages}. 171 * 172 * @return the messages of the event. 173 */ 174 public final Message[] getMessages() 175 { 176 return this.messages; 177 } 178 179 /** 180 * Getter for property {@code type}. 181 * 182 * @return the type of the events' messages. 183 */ 184 public final int getType() 185 { 186 return this.type; 187 } 188 189 /** 190 * Creates a string representing the properties of the instance. 191 * 192 * @return a string representing the properties of the instance. 193 */ 194 private String internalString() 195 { 196 final StringBuffer buf = new StringBuffer( 500 ).append( '{' ); 197 buf.append( "type=" ).append( this.type ); 198 199 for ( int i = 0; i < this.messages.length; i++ ) 200 { 201 buf.append( ", messages[" ).append( i ).append( "]=" ). 202 append( this.messages[i] ); 203 204 } 205 206 return buf.append( '}' ).toString(); 207 } 208 209 //------------------------------------------------------------MessageEvent-- 210 //--Serializable------------------------------------------------------------ 211 212 /** 213 * Takes care of initializing the {@code messages} field when constructed 214 * from an 1.0.x object stream. 215 * 216 * @throws ObjectStreamException if no messages can be resolved. 217 */ 218 private Object readResolve() throws ObjectStreamException 219 { 220 if ( this.message != null && this.messages == null ) 221 { 222 this.messages = new Message[] 223 { 224 this.message 225 }; 226 } 227 else if ( !( this.message != null && this.messages != null ) ) 228 { 229 throw new InvalidObjectException( 230 MessageEventBundle.getInstance(). 231 getMissingMessagesMessage( Locale.getDefault() ) ); 232 233 } 234 235 return this; 236 } 237 238 //------------------------------------------------------------Serializable-- 239 //--Object------------------------------------------------------------------ 240 241 /** 242 * Returns a string representation of the object. 243 * 244 * @return a string representation of the object. 245 */ 246 public String toString() 247 { 248 return super.toString() + this.internalString(); 249 } 250 251 //------------------------------------------------------------------Object-- 252}