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 @ 2651

History | View | Annotate | Download (3.21 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.tools.observer;
25

    
26
import java.util.ArrayList;
27
import java.util.List;
28
import org.gvsig.tools.lang.CloneableUtils;
29
import org.slf4j.Logger;
30
import org.slf4j.LoggerFactory;
31

    
32
public class ObservableHelper implements org.gvsig.tools.lang.Cloneable {
33

    
34
    private static final Logger logger = LoggerFactory.getLogger(ObservableHelper.class);
35

    
36
    private List<Observer> observers = new ArrayList();
37

    
38
    @Override
39
    public ObservableHelper clone() throws CloneNotSupportedException {
40
        ObservableHelper other = (ObservableHelper) super.clone();
41
        other.observers = (List<Observer>) CloneableUtils.cloneQuietly(this.observers);
42
        return other;
43
    }
44

    
45
    
46
    public synchronized void addObserver(Observer o) {
47
        if ( this.observers.contains(o) ) {
48
            return;
49
        }
50
        this.observers.add(o);
51
    }
52

    
53
    public synchronized void deleteObserver(Observer o) {
54
        this.observers.remove(o);
55
    }
56

    
57
    public synchronized void deleteObservers() {
58
        this.observers = new ArrayList();
59
    }
60

    
61
    public synchronized void notifyObservers(Observable observable, Object data) {
62
        for ( int i = 0; i < this.observers.size(); i++ ) {
63
            Observer o = (Observer) this.observers.get(i);
64
            try {
65
                o.update(observable, data);
66
            } catch (Exception ex) {
67
                logger.warn("Error notifying to the observers.", ex);
68
            }
69
        }
70

    
71
    }
72

    
73
    public Notification notifyObservers(Observable observable, String type, Object value) {
74
        Notification notification = new BaseNotification(type, new Object[]{value});
75
        this.notifyObservers(observable, notification);
76
        return notification;
77
    }
78

    
79
    public Notification notifyObservers(Observable observable, String type, Object value1, Object value2) {
80
        Notification notification = new BaseNotification(type, new Object[]{value1, value2});
81
        this.notifyObservers(observable, notification);
82
        return notification;
83
    }
84

    
85
    public Notification notifyObservers(Observable observable, String type, Object value1, Object value2, Object value3) {
86
        Notification notification = new BaseNotification(type, new Object[]{value1, value2, value3});
87
        this.notifyObservers(observable, notification);
88
        return notification;
89
    }
90
}