Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / observer / ObservableHelper.java @ 1739

History | View | Annotate | Download (2.85 KB)

1 802 cordinyana
/**
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 1008 jjdelcerro
 * as published by the Free Software Foundation; either version 3
9 802 cordinyana
 * 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 1008 jjdelcerro
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 802 cordinyana
 * 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 1008 jjdelcerro
 * MA 02110-1301, USA.
20 802 cordinyana
 *
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 445 jjdelcerro
package org.gvsig.tools.observer;
25
26
import java.util.ArrayList;
27
import java.util.List;
28 1008 jjdelcerro
import org.slf4j.Logger;
29
import org.slf4j.LoggerFactory;
30 445 jjdelcerro
31
public class ObservableHelper {
32
33 1008 jjdelcerro
    private static final Logger logger = LoggerFactory.getLogger(ObservableHelper.class);
34 445 jjdelcerro
35 1008 jjdelcerro
    private List observers = new ArrayList();
36
37
    public synchronized void addObserver(Observer o) {
38
        if ( this.observers.contains(o) ) {
39
            return;
40
        }
41
        this.observers.add(o);
42
    }
43
44
    public synchronized void deleteObserver(Observer o) {
45
        this.observers.remove(o);
46
    }
47
48
    public synchronized void deleteObservers() {
49
        this.observers = new ArrayList();
50
    }
51
52
    public synchronized void notifyObservers(Observable observable, Object data) {
53
        for ( int i = 0; i < this.observers.size(); i++ ) {
54
            Observer o = (Observer) this.observers.get(i);
55
            try {
56
                o.update(observable, data);
57
            } catch (Exception ex) {
58
                logger.warn("Error notifying to the observers.", ex);
59
            }
60
        }
61
62
    }
63
64
    public Notification notifyObservers(Observable observable, String type, Object value) {
65
        Notification notification = new BaseNotification(type, new Object[]{value});
66
        this.notifyObservers(observable, notification);
67
        return notification;
68
    }
69
70
    public Notification notifyObservers(Observable observable, String type, Object value1, Object value2) {
71
        Notification notification = new BaseNotification(type, new Object[]{value1, value2});
72
        this.notifyObservers(observable, notification);
73
        return notification;
74
    }
75
76
    public Notification notifyObservers(Observable observable, String type, Object value1, Object value2, Object value3) {
77
        Notification notification = new BaseNotification(type, new Object[]{value1, value2, value3});
78
        this.notifyObservers(observable, notification);
79
        return notification;
80
    }
81 445 jjdelcerro
}