root / trunk / frameworks / _fwAndami / src / com / iver / andami / messages / NotificationManager.java @ 9211
History | View | Annotate | Download (7.33 KB)
1 | 1104 | fjp | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
18 | *
|
||
19 | * For more information, contact:
|
||
20 | *
|
||
21 | * Generalitat Valenciana
|
||
22 | * Conselleria d'Infraestructures i Transport
|
||
23 | * Av. Blasco Ib??ez, 50
|
||
24 | * 46010 VALENCIA
|
||
25 | * SPAIN
|
||
26 | *
|
||
27 | * +34 963862235
|
||
28 | * gvsig@gva.es
|
||
29 | * www.gvsig.gva.es
|
||
30 | *
|
||
31 | * or
|
||
32 | *
|
||
33 | * IVER T.I. S.A
|
||
34 | * Salamanca 50
|
||
35 | * 46005 Valencia
|
||
36 | * Spain
|
||
37 | *
|
||
38 | * +34 963163400
|
||
39 | * dac@iver.es
|
||
40 | */
|
||
41 | 598 | fernando | package com.iver.andami.messages; |
42 | |||
43 | import java.awt.event.ActionEvent; |
||
44 | import java.awt.event.ActionListener; |
||
45 | import java.util.ArrayList; |
||
46 | import java.util.Vector; |
||
47 | |||
48 | import javax.swing.Timer; |
||
49 | |||
50 | 3965 | fjp | import org.apache.log4j.Logger; |
51 | 598 | fernando | |
52 | 3965 | fjp | |
53 | 598 | fernando | /**
|
54 | * Clase que recibe los mensajes de error, warning e informaci?n y dispara los
|
||
55 | * eventos en los listeners que escuchan dichos eventos
|
||
56 | *
|
||
57 | * @version $Revision$
|
||
58 | */
|
||
59 | public class NotificationManager { |
||
60 | /** DOCUMENT ME! */
|
||
61 | private static Logger logger = Logger.getLogger(NotificationManager.class.getName()); |
||
62 | |||
63 | /** Timer de espera de nuevos mensajes */
|
||
64 | private static Timer timer; |
||
65 | |||
66 | /** Indica si se han a?adido mensajes desde la ?ltima vez que se comprob? */
|
||
67 | private static boolean addedMessage = false; |
||
68 | |||
69 | /** DOCUMENT ME! */
|
||
70 | private static ArrayList info = new ArrayList(); |
||
71 | private static ArrayList infoExceptions = new ArrayList(); |
||
72 | |||
73 | /** DOCUMENT ME! */
|
||
74 | private static ArrayList warnings = new ArrayList(); |
||
75 | private static ArrayList warningsExceptions = new ArrayList(); |
||
76 | |||
77 | /** DOCUMENT ME! */
|
||
78 | private static ArrayList errors = new ArrayList(); |
||
79 | private static ArrayList errorsExceptions = new ArrayList(); |
||
80 | |||
81 | /** DOCUMENT ME! */
|
||
82 | private static Vector listeners = new Vector(); |
||
83 | private static boolean isFirst = true; |
||
84 | |||
85 | /**
|
||
86 | * A?ade un objeto que escucha los mensajes de error, warning e informaci?n
|
||
87 | *
|
||
88 | * @param nl objeto que recibir? los eventos
|
||
89 | */
|
||
90 | public static synchronized void addNotificationListener(NotificationListener nl) { |
||
91 | logger.info("Se a?ade un listener al manager de notificaciones");
|
||
92 | listeners.add(nl); |
||
93 | } |
||
94 | |||
95 | /**
|
||
96 | * @see com.iver.mdiApp.Notification#addError(java.lang.String)
|
||
97 | */
|
||
98 | public static synchronized void addError(final String err, Throwable e) { |
||
99 | logger.debug(err,e); |
||
100 | errors.add(err); |
||
101 | errorsExceptions.add(e); |
||
102 | |||
103 | if (isFirst) {
|
||
104 | AddError((String[]) errors.toArray(new String[0]), |
||
105 | (Throwable[]) errorsExceptions.toArray(new Throwable[0])); |
||
106 | errors.clear(); |
||
107 | errorsExceptions.clear(); |
||
108 | isFirst = false;
|
||
109 | } |
||
110 | |||
111 | dispatchMessages(); |
||
112 | } |
||
113 | |||
114 | /**
|
||
115 | * @see com.iver.mdiApp.Notification#addWarning(java.lang.String)
|
||
116 | */
|
||
117 | public static synchronized void addWarning(final String warn, final Throwable e) { |
||
118 | logger.debug(warn,e); |
||
119 | warnings.add(warn); |
||
120 | warningsExceptions.add(e); |
||
121 | |||
122 | if (isFirst) {
|
||
123 | AddWarning((String[]) warnings.toArray(new String[0]), |
||
124 | (Throwable[]) warningsExceptions.toArray(new Throwable[0])); |
||
125 | warnings.clear(); |
||
126 | warningsExceptions.clear(); |
||
127 | isFirst = false;
|
||
128 | } |
||
129 | |||
130 | dispatchMessages(); |
||
131 | } |
||
132 | |||
133 | /**
|
||
134 | * @see com.iver.mdiApp.Consola#addInfo(java.lang.String)
|
||
135 | */
|
||
136 | public static synchronized void addInfo(final String inf, final Throwable e) { |
||
137 | logger.debug(inf,e); |
||
138 | info.add(inf); |
||
139 | infoExceptions.add(e); |
||
140 | |||
141 | if (isFirst) {
|
||
142 | AddInfo((String[]) info.toArray(new String[0]), |
||
143 | (Throwable[]) infoExceptions.toArray(new Throwable[0])); |
||
144 | info.clear(); |
||
145 | infoExceptions.clear(); |
||
146 | isFirst = false;
|
||
147 | } |
||
148 | |||
149 | dispatchMessages(); |
||
150 | } |
||
151 | |||
152 | /**
|
||
153 | * M?todo que es ejecutado en el thread de la interfaz y que se encarga de
|
||
154 | * avisar del mensaje de error a todos los listeners registrados
|
||
155 | *
|
||
156 | * @param error Mensaje de error
|
||
157 | * @param e s que van a recibir las notificaciones
|
||
158 | */
|
||
159 | private static void AddError(String[] error, Throwable[] e) { |
||
160 | for (int i = 0; i < listeners.size(); i++) { |
||
161 | ((NotificationListener) listeners.get(i)).errorEvent(new MessageEvent( |
||
162 | error, e)); |
||
163 | } |
||
164 | } |
||
165 | |||
166 | /**
|
||
167 | * M?todo que es ejecutado en el thread de la interfaz y que se encarga de
|
||
168 | * avisar del mensaje de error a todos los listeners registrados
|
||
169 | *
|
||
170 | * @param warn Mensaje de warning
|
||
171 | * @param e objetos que van a recibir las notificaciones
|
||
172 | */
|
||
173 | private static void AddWarning(String[] warn, Throwable[] e) { |
||
174 | for (int i = 0; i < listeners.size(); i++) { |
||
175 | ((NotificationListener) listeners.get(i)).warningEvent(new MessageEvent( |
||
176 | warn, e)); |
||
177 | } |
||
178 | } |
||
179 | |||
180 | /**
|
||
181 | * M?todo que es ejecutado en el thread de la interfaz y que se encarga de
|
||
182 | * avisar del mensaje de informaci?n a todos los listeners registrados
|
||
183 | *
|
||
184 | * @param info Mensaje de informaci?n
|
||
185 | * @param e objetos que van a recibir las notificaciones
|
||
186 | */
|
||
187 | private static void AddInfo(String[] info, Throwable[] e) { |
||
188 | for (int i = 0; i < listeners.size(); i++) { |
||
189 | ((NotificationListener) listeners.get(i)).infoEvent(new MessageEvent( |
||
190 | info, e)); |
||
191 | } |
||
192 | } |
||
193 | |||
194 | /**
|
||
195 | * DOCUMENT ME!
|
||
196 | */
|
||
197 | private static void dispatchMessages() { |
||
198 | addedMessage = true;
|
||
199 | |||
200 | if (timer == null) { |
||
201 | |||
202 | timer = new Timer(1000, new ActionListener() { |
||
203 | public void actionPerformed(ActionEvent e) { |
||
204 | |||
205 | if (errors.size() > 0) { |
||
206 | AddError((String[]) errors.toArray(new String[0]), |
||
207 | (Throwable[]) errorsExceptions.toArray(new Throwable[0])); |
||
208 | errors.clear(); |
||
209 | errorsExceptions.clear(); |
||
210 | } |
||
211 | |||
212 | if (warnings.size() > 0) { |
||
213 | AddWarning((String[]) warnings.toArray(new String[0]), |
||
214 | (Throwable[]) warningsExceptions.toArray(new Throwable[0])); |
||
215 | warnings.clear(); |
||
216 | warningsExceptions.clear(); |
||
217 | } |
||
218 | |||
219 | if (info.size() > 0) { |
||
220 | AddInfo((String[]) info.toArray(new String[0]), |
||
221 | (Throwable[]) infoExceptions.toArray(new Throwable[0])); |
||
222 | info.clear(); |
||
223 | infoExceptions.clear(); |
||
224 | } |
||
225 | |||
226 | if (!addedMessage) {
|
||
227 | if (timer != null) { |
||
228 | timer.stop(); |
||
229 | } |
||
230 | |||
231 | timer = null;
|
||
232 | } |
||
233 | |||
234 | addedMessage = false;
|
||
235 | isFirst = true;
|
||
236 | } |
||
237 | }); |
||
238 | timer.start(); |
||
239 | } |
||
240 | } |
||
241 | 3965 | fjp | |
242 | public static void addError(Throwable e1) { |
||
243 | 4067 | caballero | addError(e1.toString(), e1); |
244 | |||
245 | 3965 | fjp | } |
246 | 598 | fernando | } |