Statistics
| Revision:

root / branches / v10 / frameworks / _fwAndami / src / com / iver / andami / messages / NotificationManager.java @ 15769

History | View | Annotate | Download (8 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 com.iver.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.apache.log4j.Logger;
53

    
54
import com.iver.andami.PluginServices;
55

    
56

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

    
67
    /** Timer de espera de nuevos mensajes */
68
    private static Timer timer;
69

    
70
    /** Indica si se han a?adido mensajes desde la ?ltima vez que se comprob? */
71
    private static boolean addedMessage = false;
72

    
73
    /** DOCUMENT ME! */
74
    private static ArrayList info = new ArrayList();
75
    private static ArrayList infoExceptions = new ArrayList();
76

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

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

    
85
    /** DOCUMENT ME! */
86
    private static Vector listeners = new Vector();
87
    private static boolean isFirst = true;
88

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

    
99
    /**
100
     * @see com.iver.mdiApp.Notification#addError(java.lang.String)
101
     */
102
    public static synchronized void addError(final String err, Throwable e) {
103
            logger.debug(err,e);
104
        errors.add(err);
105
        errorsExceptions.add(e);
106

    
107
        if (isFirst) {
108
            AddError((String[]) errors.toArray(new String[0]),
109
                (Throwable[]) errorsExceptions.toArray(new Throwable[0]));
110
            errors.clear();
111
            errorsExceptions.clear();
112
            isFirst = false;
113
        }
114

    
115
        dispatchMessages();
116
    }
117

    
118
    /**
119
     * @see com.iver.mdiApp.Notification#addWarning(java.lang.String)
120
     */
121
    public static synchronized void addWarning(final String warn, final Throwable e) {
122
                logger.debug(warn,e);
123
        warnings.add(warn);
124
        warningsExceptions.add(e);
125

    
126
        if (isFirst) {
127
            AddWarning((String[]) warnings.toArray(new String[0]),
128
                (Throwable[]) warningsExceptions.toArray(new Throwable[0]));
129
            warnings.clear();
130
            warningsExceptions.clear();
131
            isFirst = false;
132
        }
133

    
134
        dispatchMessages();
135
    }
136

    
137
    /**
138
     * @see com.iver.mdiApp.Consola#addInfo(java.lang.String)
139
     */
140
    public static synchronized void addInfo(final String inf, final Throwable e) {
141
                logger.debug(inf,e);
142
        info.add(inf);
143
        infoExceptions.add(e);
144

    
145
        if (isFirst) {
146
            AddInfo((String[]) info.toArray(new String[0]),
147
                (Throwable[]) infoExceptions.toArray(new Throwable[0]));
148
            info.clear();
149
            infoExceptions.clear();
150
            isFirst = false;
151
        }
152

    
153
        dispatchMessages();
154
    }
155

    
156
    /**
157
     * M?todo que es ejecutado en el thread de la interfaz y que se encarga de
158
     * avisar del mensaje de error a todos los listeners registrados
159
     *
160
     * @param error Mensaje de error
161
     * @param e s que van a recibir las notificaciones
162
     */
163
    private static void AddError(String[] error, Throwable[] e) {
164
        for (int i = 0; i < listeners.size(); i++) {
165
            ((NotificationListener) listeners.get(i)).errorEvent(new MessageEvent(
166
                    error, e));
167
        }
168
    }
169

    
170
    /**
171
     * M?todo que es ejecutado en el thread de la interfaz y que se encarga de
172
     * avisar del mensaje de error a todos los listeners registrados
173
     *
174
     * @param warn Mensaje de warning
175
     * @param e objetos que van a recibir las notificaciones
176
     */
177
    private static void AddWarning(String[] warn, Throwable[] e) {
178
        for (int i = 0; i < listeners.size(); i++) {
179
            ((NotificationListener) listeners.get(i)).warningEvent(new MessageEvent(
180
                    warn, e));
181
        }
182
    }
183

    
184
    /**
185
     * M?todo que es ejecutado en el thread de la interfaz y que se encarga de
186
     * avisar del mensaje de informaci?n a todos los listeners registrados
187
     *
188
     * @param info Mensaje de informaci?n
189
     * @param e objetos que van a recibir las notificaciones
190
     */
191
    private static void AddInfo(String[] info, Throwable[] e) {
192
        for (int i = 0; i < listeners.size(); i++) {
193
            ((NotificationListener) listeners.get(i)).infoEvent(new MessageEvent(
194
                    info, e));
195
        }
196
    }
197

    
198
    /**
199
     * DOCUMENT ME!
200
     */
201
    private static void dispatchMessages() {
202
        addedMessage = true;
203

    
204
        if (timer == null) {
205

    
206
            timer = new Timer(1000, new ActionListener() {
207
                                public void actionPerformed(ActionEvent e) {
208

    
209
                                if (errors.size() > 0) {
210
                                    AddError((String[]) errors.toArray(new String[0]),
211
                                        (Throwable[]) errorsExceptions.toArray(new Throwable[0]));
212
                                    errors.clear();
213
                                    errorsExceptions.clear();
214
                                }
215

    
216
                                if (warnings.size() > 0) {
217
                                    AddWarning((String[]) warnings.toArray(new String[0]),
218
                                        (Throwable[]) warningsExceptions.toArray(new Throwable[0]));
219
                                    warnings.clear();
220
                                    warningsExceptions.clear();
221
                                }
222

    
223
                                if (info.size() > 0) {
224
                                    AddInfo((String[]) info.toArray(new String[0]),
225
                                        (Throwable[]) infoExceptions.toArray(new Throwable[0]));
226
                                    info.clear();
227
                                    infoExceptions.clear();
228
                                }
229

    
230
                                if (!addedMessage) {
231
                                    if (timer != null) {
232
                                        timer.stop();
233
                                    }
234

    
235
                                    timer = null;
236
                                }
237

    
238
                                addedMessage = false;
239
                                isFirst = true;
240
                                }
241
                        });
242
            timer.start();
243
        }
244
    }
245

    
246
        public static void addError(Throwable e1) {
247
                addError(e1.toString(), e1);
248

    
249
        }
250
        public static void showMessageError(String message,Exception e) {
251
                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),message);
252
                NotificationManager.addWarning(message,e);
253
        }
254
        public static void showMessageWarning(String message,Exception e) {
255
                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),message);
256
                NotificationManager.addWarning(message,e);
257
        }
258
        public static void showMessageInfo(String message,Exception e) {
259
                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),message);
260
                NotificationManager.addInfo(message,e);
261
        }
262
}