svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.framework / org.gvsig.andami / src / main / java / org / gvsig / andami / messages / NotificationManager.java @ 40638
History | View | Annotate | Download (9.59 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program 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
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.andami.messages; |
25 |
|
26 |
import java.awt.Component; |
27 |
import java.awt.event.ActionEvent; |
28 |
import java.awt.event.ActionListener; |
29 |
import java.util.ArrayList; |
30 |
import java.util.Vector; |
31 |
|
32 |
import javax.swing.JOptionPane; |
33 |
import javax.swing.Timer; |
34 |
|
35 |
import org.slf4j.Logger; |
36 |
import org.slf4j.LoggerFactory; |
37 |
|
38 |
import org.gvsig.andami.PluginServices; |
39 |
import org.gvsig.tools.exception.IBaseException; |
40 |
|
41 |
|
42 |
|
43 |
/**
|
44 |
* Clase que recibe los mensajes de error, warning e informaci?n y dispara los
|
45 |
* eventos en los listeners que escuchan dichos eventos
|
46 |
*
|
47 |
* @version $Revision: 35350 $
|
48 |
*/
|
49 |
public class NotificationManager { |
50 |
private static int SIZE_MESSAGE=4; |
51 |
/** DOCUMENT ME! */
|
52 |
private static Logger logger = LoggerFactory.getLogger(NotificationManager.class.getName()); |
53 |
|
54 |
/** Timer de espera de nuevos mensajes */
|
55 |
private static Timer timer; |
56 |
|
57 |
/** Indica si se han a?adido mensajes desde la ?ltima vez que se comprob? */
|
58 |
private static boolean addedMessage = false; |
59 |
|
60 |
/** DOCUMENT ME! */
|
61 |
private static ArrayList info = new ArrayList(); |
62 |
private static ArrayList infoExceptions = new ArrayList(); |
63 |
|
64 |
/** DOCUMENT ME! */
|
65 |
private static ArrayList warnings = new ArrayList(); |
66 |
private static ArrayList warningsExceptions = new ArrayList(); |
67 |
|
68 |
/** DOCUMENT ME! */
|
69 |
private static ArrayList errors = new ArrayList(); |
70 |
private static ArrayList errorsExceptions = new ArrayList(); |
71 |
|
72 |
/** DOCUMENT ME! */
|
73 |
private static Vector listeners = new Vector(); |
74 |
private static boolean isFirst = true; |
75 |
|
76 |
/**
|
77 |
* A?ade un objeto que escucha los mensajes de error, warning e informaci?n
|
78 |
*
|
79 |
* @param nl objeto que recibir? los eventos
|
80 |
*/
|
81 |
public static synchronized void addNotificationListener(NotificationListener nl) { |
82 |
logger.info("Se a?ade un listener al manager de notificaciones (" + nl.getClass().getName() +"/"+ nl.toString() + ")"); |
83 |
listeners.add(nl); |
84 |
} |
85 |
|
86 |
/**
|
87 |
* @see com.iver.mdiApp.Notification#addError(java.lang.String)
|
88 |
*/
|
89 |
public static synchronized void addError(final String err, Throwable e) { |
90 |
logger.error(err, e); |
91 |
dispatchError(err, e); |
92 |
} |
93 |
|
94 |
public static synchronized void dispatchError(final String err, Throwable e) { |
95 |
errors.add(err); |
96 |
errorsExceptions.add(e); |
97 |
|
98 |
if (isFirst) {
|
99 |
AddError((String[]) errors.toArray(new String[0]), |
100 |
(Throwable[]) errorsExceptions.toArray(new Throwable[0])); |
101 |
errors.clear(); |
102 |
errorsExceptions.clear(); |
103 |
isFirst = false;
|
104 |
} |
105 |
|
106 |
dispatchMessages(); |
107 |
} |
108 |
|
109 |
/**
|
110 |
* @see com.iver.mdiApp.Notification#addWarning(java.lang.String)
|
111 |
*/
|
112 |
public static synchronized void addWarning(final String warn, |
113 |
final Throwable e) { |
114 |
logger.warn(warn, e); |
115 |
dispatchWarning(warn, e); |
116 |
} |
117 |
|
118 |
public static synchronized void dispatchWarning(final String warn, |
119 |
final Throwable e) { |
120 |
warnings.add(warn); |
121 |
warningsExceptions.add(e); |
122 |
|
123 |
if (isFirst) {
|
124 |
AddWarning((String[]) warnings.toArray(new String[0]), |
125 |
(Throwable[]) warningsExceptions.toArray(new Throwable[0])); |
126 |
warnings.clear(); |
127 |
warningsExceptions.clear(); |
128 |
isFirst = false;
|
129 |
} |
130 |
|
131 |
dispatchMessages(); |
132 |
} |
133 |
|
134 |
/*
|
135 |
* @see com.iver.mdiApp.Notification#addWarning(java.lang.String)
|
136 |
*/
|
137 |
public static synchronized void addWarning(final String warn) { |
138 |
addWarning(warn,null);
|
139 |
} |
140 |
|
141 |
/*
|
142 |
* @see com.iver.mdiApp.Consola#addInfo(java.lang.String)
|
143 |
*/
|
144 |
public static synchronized void addInfo(final String inf, final Throwable e) { |
145 |
logger.info(inf, e); |
146 |
info.add(inf); |
147 |
infoExceptions.add(e); |
148 |
|
149 |
if (isFirst) {
|
150 |
AddInfo((String[]) info.toArray(new String[0]), |
151 |
(Throwable[]) infoExceptions.toArray(new Throwable[0])); |
152 |
info.clear(); |
153 |
infoExceptions.clear(); |
154 |
isFirst = false;
|
155 |
} |
156 |
|
157 |
dispatchMessages(); |
158 |
} |
159 |
/*
|
160 |
* @see com.iver.mdiApp.Consola#addInfo(java.lang.String)
|
161 |
*/
|
162 |
public static synchronized void addInfo(final String inf) { |
163 |
addInfo(inf,null);
|
164 |
} |
165 |
/**
|
166 |
* M?todo que es ejecutado en el thread de la interfaz y que se encarga de
|
167 |
* avisar del mensaje de error a todos los listeners registrados
|
168 |
*
|
169 |
* @param error Mensaje de error
|
170 |
* @param e s que van a recibir las notificaciones
|
171 |
*/
|
172 |
private static void AddError(String[] error, Throwable[] e) { |
173 |
for (int i = 0; i < listeners.size(); i++) { |
174 |
((NotificationListener) listeners.get(i)).errorEvent(new MessageEvent( |
175 |
error, e)); |
176 |
} |
177 |
} |
178 |
|
179 |
/**
|
180 |
* M?todo que es ejecutado en el thread de la interfaz y que se encarga de
|
181 |
* avisar del mensaje de error a todos los listeners registrados
|
182 |
*
|
183 |
* @param warn Mensaje de warning
|
184 |
* @param e objetos que van a recibir las notificaciones
|
185 |
*/
|
186 |
private static void AddWarning(String[] warn, Throwable[] e) { |
187 |
for (int i = 0; i < listeners.size(); i++) { |
188 |
((NotificationListener) listeners.get(i)).warningEvent(new MessageEvent( |
189 |
warn, e)); |
190 |
} |
191 |
} |
192 |
|
193 |
/**
|
194 |
* M?todo que es ejecutado en el thread de la interfaz y que se encarga de
|
195 |
* avisar del mensaje de informaci?n a todos los listeners registrados
|
196 |
*
|
197 |
* @param info Mensaje de informaci?n
|
198 |
* @param e objetos que van a recibir las notificaciones
|
199 |
*/
|
200 |
private static void AddInfo(String[] info, Throwable[] e) { |
201 |
for (int i = 0; i < listeners.size(); i++) { |
202 |
((NotificationListener) listeners.get(i)).infoEvent(new MessageEvent( |
203 |
info, e)); |
204 |
} |
205 |
} |
206 |
|
207 |
/**
|
208 |
* DOCUMENT ME!
|
209 |
*/
|
210 |
private static void dispatchMessages() { |
211 |
addedMessage = true;
|
212 |
|
213 |
if (timer == null) { |
214 |
|
215 |
timer = new Timer(1000, new ActionListener() { |
216 |
public void actionPerformed(ActionEvent e) { |
217 |
|
218 |
if (errors.size() > 0) { |
219 |
AddError((String[]) errors.toArray(new String[0]), |
220 |
(Throwable[]) errorsExceptions.toArray(new Throwable[0])); |
221 |
errors.clear(); |
222 |
errorsExceptions.clear(); |
223 |
} |
224 |
|
225 |
if (warnings.size() > 0) { |
226 |
AddWarning((String[]) warnings.toArray(new String[0]), |
227 |
(Throwable[]) warningsExceptions.toArray(new Throwable[0])); |
228 |
warnings.clear(); |
229 |
warningsExceptions.clear(); |
230 |
} |
231 |
|
232 |
if (info.size() > 0) { |
233 |
AddInfo((String[]) info.toArray(new String[0]), |
234 |
(Throwable[]) infoExceptions.toArray(new Throwable[0])); |
235 |
info.clear(); |
236 |
infoExceptions.clear(); |
237 |
} |
238 |
|
239 |
if (!addedMessage) {
|
240 |
if (timer != null) { |
241 |
timer.stop(); |
242 |
} |
243 |
|
244 |
timer = null;
|
245 |
} |
246 |
|
247 |
addedMessage = false;
|
248 |
isFirst = true;
|
249 |
} |
250 |
}); |
251 |
timer.start(); |
252 |
} |
253 |
} |
254 |
|
255 |
public static void addError(Throwable e1) { |
256 |
if( e1 instanceof IBaseException ) { |
257 |
IBaseException ex = (IBaseException) e1; |
258 |
String msg;
|
259 |
try {
|
260 |
msg = ex.getLocalizedMessageStack(); |
261 |
} catch(Exception ex1) { |
262 |
try {
|
263 |
msg = ex.getMessageStack(); |
264 |
} catch(Exception ex2) { |
265 |
msg = ex.getMessage(); |
266 |
} |
267 |
} |
268 |
addError(msg, e1); |
269 |
} else {
|
270 |
addError(e1.toString(), e1); |
271 |
} |
272 |
|
273 |
} |
274 |
|
275 |
public static void showMessageError(String message,Exception e) { |
276 |
message=splitMessage(message); |
277 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),message,PluginServices.getText(NotificationManager.class,"error"),JOptionPane.ERROR_MESSAGE); |
278 |
NotificationManager.addWarning(message,e); |
279 |
} |
280 |
public static void showMessageWarning(String message,Exception e) { |
281 |
message=splitMessage(message); |
282 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),message,PluginServices.getText(NotificationManager.class,"warning"),JOptionPane.WARNING_MESSAGE); |
283 |
NotificationManager.addWarning(message,e); |
284 |
} |
285 |
public static void showMessageInfo(String message,Exception e) { |
286 |
message=splitMessage(message); |
287 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),message,PluginServices.getText(NotificationManager.class,"info"),JOptionPane.INFORMATION_MESSAGE); |
288 |
NotificationManager.addInfo(message,e); |
289 |
} |
290 |
private static String splitMessage(String message) { |
291 |
String[] messages=message.split("\n"); |
292 |
String resultMessage=""; |
293 |
for (int i=0;i<messages.length && i<=SIZE_MESSAGE;i++){ |
294 |
resultMessage+=(messages[i]); |
295 |
resultMessage+=("\n");
|
296 |
} |
297 |
return resultMessage;
|
298 |
} |
299 |
} |