svn-gvsig-desktop / trunk / frameworks / _fwAndami / src / com / iver / andami / messages / NotificationManager.java @ 598
History | View | Annotate | Download (6.11 KB)
1 |
package com.iver.andami.messages; |
---|---|
2 |
|
3 |
import org.apache.log4j.Logger; |
4 |
|
5 |
import java.awt.event.ActionEvent; |
6 |
import java.awt.event.ActionListener; |
7 |
|
8 |
import java.util.ArrayList; |
9 |
import java.util.Vector; |
10 |
|
11 |
import javax.swing.Timer; |
12 |
|
13 |
|
14 |
/**
|
15 |
* Clase que recibe los mensajes de error, warning e informaci?n y dispara los
|
16 |
* eventos en los listeners que escuchan dichos eventos
|
17 |
*
|
18 |
* @version $Revision: 598 $
|
19 |
*/
|
20 |
public class NotificationManager { |
21 |
/** DOCUMENT ME! */
|
22 |
private static Logger logger = Logger.getLogger(NotificationManager.class.getName()); |
23 |
|
24 |
/** Timer de espera de nuevos mensajes */
|
25 |
private static Timer timer; |
26 |
|
27 |
/** Indica si se han a?adido mensajes desde la ?ltima vez que se comprob? */
|
28 |
private static boolean addedMessage = false; |
29 |
|
30 |
/** DOCUMENT ME! */
|
31 |
private static ArrayList info = new ArrayList(); |
32 |
private static ArrayList infoExceptions = new ArrayList(); |
33 |
|
34 |
/** DOCUMENT ME! */
|
35 |
private static ArrayList warnings = new ArrayList(); |
36 |
private static ArrayList warningsExceptions = new ArrayList(); |
37 |
|
38 |
/** DOCUMENT ME! */
|
39 |
private static ArrayList errors = new ArrayList(); |
40 |
private static ArrayList errorsExceptions = new ArrayList(); |
41 |
|
42 |
/** DOCUMENT ME! */
|
43 |
private static Vector listeners = new Vector(); |
44 |
private static boolean isFirst = true; |
45 |
|
46 |
/**
|
47 |
* A?ade un objeto que escucha los mensajes de error, warning e informaci?n
|
48 |
*
|
49 |
* @param nl objeto que recibir? los eventos
|
50 |
*/
|
51 |
public static synchronized void addNotificationListener(NotificationListener nl) { |
52 |
logger.info("Se a?ade un listener al manager de notificaciones");
|
53 |
listeners.add(nl); |
54 |
} |
55 |
|
56 |
/**
|
57 |
* @see com.iver.mdiApp.Notification#addError(java.lang.String)
|
58 |
*/
|
59 |
public static synchronized void addError(final String err, Throwable e) { |
60 |
logger.debug(err,e); |
61 |
errors.add(err); |
62 |
errorsExceptions.add(e); |
63 |
|
64 |
if (isFirst) {
|
65 |
AddError((String[]) errors.toArray(new String[0]), |
66 |
(Throwable[]) errorsExceptions.toArray(new Throwable[0])); |
67 |
errors.clear(); |
68 |
errorsExceptions.clear(); |
69 |
isFirst = false;
|
70 |
} |
71 |
|
72 |
dispatchMessages(); |
73 |
} |
74 |
|
75 |
/**
|
76 |
* @see com.iver.mdiApp.Notification#addWarning(java.lang.String)
|
77 |
*/
|
78 |
public static synchronized void addWarning(final String warn, final Throwable e) { |
79 |
logger.debug(warn,e); |
80 |
warnings.add(warn); |
81 |
warningsExceptions.add(e); |
82 |
|
83 |
if (isFirst) {
|
84 |
AddWarning((String[]) warnings.toArray(new String[0]), |
85 |
(Throwable[]) warningsExceptions.toArray(new Throwable[0])); |
86 |
warnings.clear(); |
87 |
warningsExceptions.clear(); |
88 |
isFirst = false;
|
89 |
} |
90 |
|
91 |
dispatchMessages(); |
92 |
} |
93 |
|
94 |
/**
|
95 |
* @see com.iver.mdiApp.Consola#addInfo(java.lang.String)
|
96 |
*/
|
97 |
public static synchronized void addInfo(final String inf, final Throwable e) { |
98 |
logger.debug(inf,e); |
99 |
info.add(inf); |
100 |
infoExceptions.add(e); |
101 |
|
102 |
if (isFirst) {
|
103 |
AddInfo((String[]) info.toArray(new String[0]), |
104 |
(Throwable[]) infoExceptions.toArray(new Throwable[0])); |
105 |
info.clear(); |
106 |
infoExceptions.clear(); |
107 |
isFirst = false;
|
108 |
} |
109 |
|
110 |
dispatchMessages(); |
111 |
} |
112 |
|
113 |
/**
|
114 |
* M?todo que es ejecutado en el thread de la interfaz y que se encarga de
|
115 |
* avisar del mensaje de error a todos los listeners registrados
|
116 |
*
|
117 |
* @param error Mensaje de error
|
118 |
* @param e s que van a recibir las notificaciones
|
119 |
*/
|
120 |
private static void AddError(String[] error, Throwable[] e) { |
121 |
for (int i = 0; i < listeners.size(); i++) { |
122 |
((NotificationListener) listeners.get(i)).errorEvent(new MessageEvent( |
123 |
error, e)); |
124 |
} |
125 |
} |
126 |
|
127 |
/**
|
128 |
* M?todo que es ejecutado en el thread de la interfaz y que se encarga de
|
129 |
* avisar del mensaje de error a todos los listeners registrados
|
130 |
*
|
131 |
* @param warn Mensaje de warning
|
132 |
* @param e objetos que van a recibir las notificaciones
|
133 |
*/
|
134 |
private static void AddWarning(String[] warn, Throwable[] e) { |
135 |
for (int i = 0; i < listeners.size(); i++) { |
136 |
((NotificationListener) listeners.get(i)).warningEvent(new MessageEvent( |
137 |
warn, e)); |
138 |
} |
139 |
} |
140 |
|
141 |
/**
|
142 |
* M?todo que es ejecutado en el thread de la interfaz y que se encarga de
|
143 |
* avisar del mensaje de informaci?n a todos los listeners registrados
|
144 |
*
|
145 |
* @param info Mensaje de informaci?n
|
146 |
* @param e objetos que van a recibir las notificaciones
|
147 |
*/
|
148 |
private static void AddInfo(String[] info, Throwable[] e) { |
149 |
for (int i = 0; i < listeners.size(); i++) { |
150 |
((NotificationListener) listeners.get(i)).infoEvent(new MessageEvent( |
151 |
info, e)); |
152 |
} |
153 |
} |
154 |
|
155 |
/**
|
156 |
* DOCUMENT ME!
|
157 |
*/
|
158 |
private static void dispatchMessages() { |
159 |
addedMessage = true;
|
160 |
|
161 |
if (timer == null) { |
162 |
System.err.println("Se pone el timer"); |
163 |
|
164 |
timer = new Timer(1000, new ActionListener() { |
165 |
public void actionPerformed(ActionEvent e) { |
166 |
|
167 |
if (errors.size() > 0) { |
168 |
AddError((String[]) errors.toArray(new String[0]), |
169 |
(Throwable[]) errorsExceptions.toArray(new Throwable[0])); |
170 |
errors.clear(); |
171 |
errorsExceptions.clear(); |
172 |
} |
173 |
|
174 |
if (warnings.size() > 0) { |
175 |
AddWarning((String[]) warnings.toArray(new String[0]), |
176 |
(Throwable[]) warningsExceptions.toArray(new Throwable[0])); |
177 |
warnings.clear(); |
178 |
warningsExceptions.clear(); |
179 |
} |
180 |
|
181 |
if (info.size() > 0) { |
182 |
AddInfo((String[]) info.toArray(new String[0]), |
183 |
(Throwable[]) infoExceptions.toArray(new Throwable[0])); |
184 |
info.clear(); |
185 |
infoExceptions.clear(); |
186 |
} |
187 |
|
188 |
if (!addedMessage) {
|
189 |
if (timer != null) { |
190 |
timer.stop(); |
191 |
} |
192 |
|
193 |
timer = null;
|
194 |
} |
195 |
|
196 |
addedMessage = false;
|
197 |
isFirst = true;
|
198 |
} |
199 |
}); |
200 |
timer.start(); |
201 |
} |
202 |
} |
203 |
} |