1 | /* |
2 | * jDTAUS Core API |
3 | * Copyright (C) 2005 Christian Schulte |
4 | * <cs@schulte.it> |
5 | * |
6 | * This library is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU Lesser General Public |
8 | * License as published by the Free Software Foundation; either |
9 | * version 2.1 of the License, or any later version. |
10 | * |
11 | * This library is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | * Lesser General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU Lesser General Public |
17 | * License along with this library; if not, write to the Free Software |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
19 | * |
20 | */ |
21 | package org.jdtaus.core.text; |
22 | |
23 | import java.io.InvalidObjectException; |
24 | import java.io.ObjectStreamException; |
25 | import java.util.EventObject; |
26 | import java.util.Locale; |
27 | |
28 | /** |
29 | * Event holding messages. |
30 | * |
31 | * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> |
32 | * @version $JDTAUS: MessageEvent.java 8641 2012-09-27 06:45:17Z schulte $ |
33 | * |
34 | * @see MessageEventSource |
35 | */ |
36 | public class MessageEvent extends EventObject |
37 | { |
38 | //--Constants--------------------------------------------------------------- |
39 | |
40 | /** Event constant for information messages. */ |
41 | public static final int INFORMATION = 2001; |
42 | |
43 | /** Event constant for notification messages. */ |
44 | public static final int NOTIFICATION = 2002; |
45 | |
46 | /** Event constant for warning messages. */ |
47 | public static final int WARNING = 2003; |
48 | |
49 | /** Event constant for error messages. */ |
50 | public static final int ERROR = 2004; |
51 | |
52 | /** Serial version UID for backwards compatibility with 1.0.x classes. */ |
53 | private static final long serialVersionUID = 7335694054201632443L; |
54 | |
55 | //---------------------------------------------------------------Constants-- |
56 | //--Constructors------------------------------------------------------------ |
57 | |
58 | /** |
59 | * Creates a new {@code MessageEvent} instance taking a single message. |
60 | * |
61 | * @param source the source of the new event. |
62 | * @param message the message for the new event. |
63 | * @param type constant indicating the type of the events' messages. |
64 | * |
65 | * @throws NullPointerException if {@code message} is {@code null}. |
66 | * @throws IllegalArgumentException if {@code source} is {@code null} or |
67 | * {@code type} is not equal to one of the constants {@code INFORMATION}, |
68 | * {@code NOTIFICATION}, {@code WARNING} or {@code ERROR}. |
69 | */ |
70 | public MessageEvent( final Object source, final Message message, |
71 | final int type ) |
72 | { |
73 | this( source, new Message[] |
74 | { |
75 | message |
76 | }, type ); |
77 | } |
78 | |
79 | /** |
80 | * Creates a new {@code MessageEvent} instance taking multiple messages. |
81 | * |
82 | * @param source the source of the new event. |
83 | * @param messages the messages for the new event. |
84 | * @param type constant indicating the type of the events' messages. |
85 | * |
86 | * @throws NullPointerException if {@code messages} is {@code null}. |
87 | * @throws IllegalArgumentException if {@code source} is {@code null} or |
88 | * {@code type} is not equal to one of the constants {@code INFORMATION}, |
89 | * {@code NOTIFICATION}, {@code WARNING} or {@code ERROR} or the length of |
90 | * {@code messages} is zero. |
91 | */ |
92 | public MessageEvent( final Object source, final Message[] messages, |
93 | final int type ) |
94 | { |
95 | super( source ); |
96 | |
97 | if ( messages == null ) |
98 | { |
99 | 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 | } |