Statistics
| Revision:

root / branches / v2_0_0_prep / frameworks / _fwAndami / src / org / gvsig / andami / messages / NotificationManager.java @ 35350

History | View | Annotate | Download (9.79 KB)

1
/* 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
package org.gvsig.andami.messages;
42

    
43
import java.awt.Component;
44
import java.awt.event.ActionEvent;
45
import java.awt.event.ActionListener;
46
import java.util.ArrayList;
47
import java.util.Vector;
48

    
49
import javax.swing.JOptionPane;
50
import javax.swing.Timer;
51

    
52
import org.slf4j.Logger;
53
import org.slf4j.LoggerFactory;
54

    
55
import org.gvsig.andami.PluginServices;
56
import org.gvsig.tools.exception.IBaseException;
57

    
58

    
59

    
60
/**
61
 * Clase que recibe los mensajes de error, warning e informaci?n y dispara los
62
 * eventos en los listeners que escuchan dichos eventos
63
 *
64
 * @version $Revision: 35350 $
65
 */
66
public class NotificationManager {
67
        private static int SIZE_MESSAGE=4;
68
    /** DOCUMENT ME! */
69
    private static Logger logger = LoggerFactory.getLogger(NotificationManager.class.getName());
70

    
71
    /** Timer de espera de nuevos mensajes */
72
    private static Timer timer;
73

    
74
    /** Indica si se han a?adido mensajes desde la ?ltima vez que se comprob? */
75
    private static boolean addedMessage = false;
76

    
77
    /** DOCUMENT ME! */
78
    private static ArrayList info = new ArrayList();
79
    private static ArrayList infoExceptions = new ArrayList();
80

    
81
    /** DOCUMENT ME! */
82
    private static ArrayList warnings = new ArrayList();
83
    private static ArrayList warningsExceptions = new ArrayList();
84

    
85
    /** DOCUMENT ME! */
86
    private static ArrayList errors = new ArrayList();
87
    private static ArrayList errorsExceptions = new ArrayList();
88

    
89
    /** DOCUMENT ME! */
90
    private static Vector listeners = new Vector();
91
    private static boolean isFirst = true;
92

    
93
    /**
94
     * A?ade un objeto que escucha los mensajes de error, warning e informaci?n
95
     *
96
     * @param nl objeto que recibir? los eventos
97
     */
98
    public static synchronized void addNotificationListener(NotificationListener nl) {
99
        logger.info("Se a?ade un listener al manager de notificaciones");
100
        listeners.add(nl);
101
    }
102

    
103
    /**
104
     * @see com.iver.mdiApp.Notification#addError(java.lang.String)
105
     */
106
    public static synchronized void addError(final String err, Throwable e) {
107
            logger.error(err, e);
108
        dispatchError(err, e);
109
    }
110
    
111
    public static synchronized void dispatchError(final String err, Throwable e) {
112
        errors.add(err);
113
        errorsExceptions.add(e);
114

    
115
        if (isFirst) {
116
            AddError((String[]) errors.toArray(new String[0]),
117
                (Throwable[]) errorsExceptions.toArray(new Throwable[0]));
118
            errors.clear();
119
            errorsExceptions.clear();
120
            isFirst = false;
121
        }
122

    
123
        dispatchMessages();
124
    }
125

    
126
    /**
127
     * @see com.iver.mdiApp.Notification#addWarning(java.lang.String)
128
     */
129
    public static synchronized void addWarning(final String warn,
130
        final Throwable e) {
131
        logger.warn(warn, e);
132
        dispatchWarning(warn, e);
133
    }
134

    
135
    public static synchronized void dispatchWarning(final String warn,
136
        final Throwable e) {
137
        warnings.add(warn);
138
        warningsExceptions.add(e);
139

    
140
        if (isFirst) {
141
            AddWarning((String[]) warnings.toArray(new String[0]),
142
                (Throwable[]) warningsExceptions.toArray(new Throwable[0]));
143
            warnings.clear();
144
            warningsExceptions.clear();
145
            isFirst = false;
146
        }
147

    
148
        dispatchMessages();
149
    }
150
    
151
    /*
152
     * @see com.iver.mdiApp.Notification#addWarning(java.lang.String)
153
     */
154
    public static synchronized void addWarning(final String warn) {
155
                addWarning(warn,null);
156
    }
157

    
158
    /*
159
     * @see com.iver.mdiApp.Consola#addInfo(java.lang.String)
160
     */
161
    public static synchronized void addInfo(final String inf, final Throwable e) {
162
                logger.info(inf, e);
163
        info.add(inf);
164
        infoExceptions.add(e);
165

    
166
        if (isFirst) {
167
            AddInfo((String[]) info.toArray(new String[0]),
168
                (Throwable[]) infoExceptions.toArray(new Throwable[0]));
169
            info.clear();
170
            infoExceptions.clear();
171
            isFirst = false;
172
        }
173

    
174
        dispatchMessages();
175
    }
176
    /*
177
     * @see com.iver.mdiApp.Consola#addInfo(java.lang.String)
178
     */
179
    public static synchronized void addInfo(final String inf) {
180
                addInfo(inf,null);
181
    }
182
    /**
183
     * M?todo que es ejecutado en el thread de la interfaz y que se encarga de
184
     * avisar del mensaje de error a todos los listeners registrados
185
     *
186
     * @param error Mensaje de error
187
     * @param e s que van a recibir las notificaciones
188
     */
189
    private static void AddError(String[] error, Throwable[] e) {
190
        for (int i = 0; i < listeners.size(); i++) {
191
            ((NotificationListener) listeners.get(i)).errorEvent(new MessageEvent(
192
                    error, e));
193
        }
194
    }
195

    
196
    /**
197
     * M?todo que es ejecutado en el thread de la interfaz y que se encarga de
198
     * avisar del mensaje de error a todos los listeners registrados
199
     *
200
     * @param warn Mensaje de warning
201
     * @param e objetos que van a recibir las notificaciones
202
     */
203
    private static void AddWarning(String[] warn, Throwable[] e) {
204
        for (int i = 0; i < listeners.size(); i++) {
205
            ((NotificationListener) listeners.get(i)).warningEvent(new MessageEvent(
206
                    warn, e));
207
        }
208
    }
209

    
210
    /**
211
     * M?todo que es ejecutado en el thread de la interfaz y que se encarga de
212
     * avisar del mensaje de informaci?n a todos los listeners registrados
213
     *
214
     * @param info Mensaje de informaci?n
215
     * @param e objetos que van a recibir las notificaciones
216
     */
217
    private static void AddInfo(String[] info, Throwable[] e) {
218
        for (int i = 0; i < listeners.size(); i++) {
219
            ((NotificationListener) listeners.get(i)).infoEvent(new MessageEvent(
220
                    info, e));
221
        }
222
    }
223

    
224
    /**
225
     * DOCUMENT ME!
226
     */
227
    private static void dispatchMessages() {
228
        addedMessage = true;
229

    
230
        if (timer == null) {
231

    
232
            timer = new Timer(1000, new ActionListener() {
233
                                public void actionPerformed(ActionEvent e) {
234

    
235
                                if (errors.size() > 0) {
236
                                    AddError((String[]) errors.toArray(new String[0]),
237
                                        (Throwable[]) errorsExceptions.toArray(new Throwable[0]));
238
                                    errors.clear();
239
                                    errorsExceptions.clear();
240
                                }
241

    
242
                                if (warnings.size() > 0) {
243
                                    AddWarning((String[]) warnings.toArray(new String[0]),
244
                                        (Throwable[]) warningsExceptions.toArray(new Throwable[0]));
245
                                    warnings.clear();
246
                                    warningsExceptions.clear();
247
                                }
248

    
249
                                if (info.size() > 0) {
250
                                    AddInfo((String[]) info.toArray(new String[0]),
251
                                        (Throwable[]) infoExceptions.toArray(new Throwable[0]));
252
                                    info.clear();
253
                                    infoExceptions.clear();
254
                                }
255

    
256
                                if (!addedMessage) {
257
                                    if (timer != null) {
258
                                        timer.stop();
259
                                    }
260

    
261
                                    timer = null;
262
                                }
263

    
264
                                addedMessage = false;
265
                                isFirst = true;
266
                                }
267
                        });
268
            timer.start();
269
        }
270
    }
271

    
272
        public static void addError(Throwable e1) {
273
            if( e1 instanceof IBaseException ) {
274
            IBaseException ex = (IBaseException) e1;
275
                String msg;
276
                try {
277
                    msg = ex.getLocalizedMessageStack();
278
                } catch(Exception ex1) {
279
                    try {
280
                        msg = ex.getMessageStack();
281
                    } catch(Exception ex2) {
282
                        msg = ex.getMessage();
283
                    }
284
                }
285
            addError(msg, e1);
286
            } else {
287
                addError(e1.toString(), e1);
288
            }
289

    
290
        }
291

    
292
        public static void showMessageError(String message,Exception e) {
293
                message=splitMessage(message);
294
                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),message,PluginServices.getText(NotificationManager.class,"error"),JOptionPane.ERROR_MESSAGE);
295
                NotificationManager.addWarning(message,e);
296
        }
297
        public static void showMessageWarning(String message,Exception e) {
298
                message=splitMessage(message);
299
                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),message,PluginServices.getText(NotificationManager.class,"warning"),JOptionPane.WARNING_MESSAGE);
300
                NotificationManager.addWarning(message,e);
301
        }
302
        public static void showMessageInfo(String message,Exception e) {
303
                message=splitMessage(message);
304
                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),message,PluginServices.getText(NotificationManager.class,"info"),JOptionPane.INFORMATION_MESSAGE);
305
                NotificationManager.addInfo(message,e);
306
        }
307
        private static String splitMessage(String message) {
308
                String[] messages=message.split("\n");
309
                String resultMessage="";
310
                for (int i=0;i<messages.length && i<=SIZE_MESSAGE;i++){
311
                        resultMessage+=(messages[i]);
312
                        resultMessage+=("\n");
313
                }
314
                return resultMessage;
315
        }
316
}