Subversion Repositories eFlore/Applications.del

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
83 benjamin 1
package org.tela_botanica.del.client.utils;
2
 
3
import java.util.ArrayList;
4
import java.util.List;
5
 
6
public class GwtObservable {
7
	List<GwtObserver> observers = new ArrayList<GwtObserver>();
8
 
9
	boolean changed = false;
10
 
11
	/**
12
	 * Constructs a new {@code Observable} object.
13
	 */
14
	public GwtObservable() {
15
		super();
16
	}
17
 
18
	/**
19
	 * Adds the specified observer to the list of observers. If it is already
20
	 * registered, it is not added a second time.
21
	 *
22
	 * @param observer
23
	 *            the Observer to add.
24
	 */
25
	public void addObserver(GwtObserver observer) {
26
		if (observer == null) {
27
			throw new NullPointerException();
28
		}
29
		synchronized (this) {
30
			if (!observers.contains(observer))
31
				observers.add(observer);
32
		}
33
	}
34
 
35
	/**
36
	 * Clears the changed flag for this {@code Observable}. After calling
37
	 * {@code clearChanged()}, {@code hasChanged()} will return {@code false}.
38
	 */
39
	protected void clearChanged() {
40
		changed = false;
41
	}
42
 
43
	/**
44
	 * Returns the number of observers registered to this {@code Observable}.
45
	 *
46
	 * @return the number of observers.
47
	 */
48
	public int countObservers() {
49
		return observers.size();
50
	}
51
 
52
	/**
53
	 * Removes the specified observer from the list of observers. Passing null
54
	 * won't do anything.
55
	 *
56
	 * @param observer
57
	 *            the observer to remove.
58
	 */
59
	public synchronized void deleteObserver(GwtObserver observer) {
60
		observers.remove(observer);
61
	}
62
 
63
	/**
64
	 * Removes all observers from the list of observers.
65
	 */
66
	public synchronized void deleteObservers() {
67
		observers.clear();
68
	}
69
 
70
	/**
71
	 * Returns the changed flag for this {@code Observable}.
72
	 *
73
	 * @return {@code true} when the changed flag for this {@code Observable} is
74
	 *         set, {@code false} otherwise.
75
	 */
76
	public boolean hasChanged() {
77
		return changed;
78
	}
79
 
80
	/**
81
	 * If {@code hasChanged()} returns {@code true}, calls the {@code update()}
82
	 * method for every observer in the list of observers using null as the
83
	 * argument. Afterwards, calls {@code clearChanged()}.
84
	 * <p>
85
	 * Equivalent to calling {@code notifyObservers(null)}.
86
	 */
87
	public void notifyObservers() {
88
		notifyObservers(null);
89
	}
90
 
91
	/**
92
	 * If {@code hasChanged()} returns {@code true}, calls the {@code update()}
93
	 * method for every Observer in the list of observers using the specified
94
	 * argument. Afterwards calls {@code clearChanged()}.
95
	 *
96
	 * @param data
97
	 *            the argument passed to {@code update()}.
98
	 */
99
	public void notifyObservers(Object data) {
100
		int size = 0;
101
		GwtObserver[] arrays = null;
102
		synchronized (this) {
103
			if (hasChanged()) {
104
				clearChanged();
105
				size = observers.size();
106
				arrays = new GwtObserver[size];
107
				observers.toArray(arrays);
108
			}
109
		}
110
		if (arrays != null) {
111
			for (GwtObserver observer : arrays) {
112
				observer.update(this, data);
113
			}
114
		}
115
	}
116
 
117
	/**
118
	 * Sets the changed flag for this {@code Observable}. After calling
119
	 * {@code setChanged()}, {@code hasChanged()} will return {@code true}.
120
	 */
121
	protected void setChanged() {
122
		changed = true;
123
	}
124
 
125
}