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