aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/com/vaadin/data/util/AbstractContainer.java
blob: 7d96c2d7579343aec4f0ed89a0d19c53fbe2d6da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
@VaadinApache2LicenseForJavaFiles@
 */
package com.vaadin.data.util;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.EventObject;
import java.util.LinkedList;

import com.vaadin.data.Container;

/**
 * Abstract container class that manages event listeners and sending events to
 * them ({@link PropertySetChangeNotifier}, {@link ItemSetChangeNotifier}).
 * 
 * Note that this class provides the internal implementations for both types of
 * events and notifiers as protected methods, but does not implement the
 * {@link PropertySetChangeNotifier} and {@link ItemSetChangeNotifier}
 * interfaces directly. This way, subclasses can choose not to implement them.
 * Subclasses implementing those interfaces should also override the
 * corresponding {@link #addListener()} and {@link #removeListener()} methods to
 * make them public.
 * 
 * @since 6.6
 */
public abstract class AbstractContainer implements Container {

    /**
     * List of all Property set change event listeners.
     */
    private Collection<Container.PropertySetChangeListener> propertySetChangeListeners = null;

    /**
     * List of all container Item set change event listeners.
     */
    private Collection<Container.ItemSetChangeListener> itemSetChangeListeners = null;

    /**
     * An <code>event</code> object specifying the container whose Property set
     * has changed.
     * 
     * This class does not provide information about which properties were
     * concerned by the change, but subclasses can provide additional
     * information about the changes.
     */
    protected static class BasePropertySetChangeEvent extends EventObject
            implements Container.PropertySetChangeEvent, Serializable {

        protected BasePropertySetChangeEvent(Container source) {
            super(source);
        }

        @Override
        public Container getContainer() {
            return (Container) getSource();
        }
    }

    /**
     * An <code>event</code> object specifying the container whose Item set has
     * changed.
     * 
     * This class does not provide information about the exact changes
     * performed, but subclasses can add provide additional information about
     * the changes.
     */
    protected static class BaseItemSetChangeEvent extends EventObject implements
            Container.ItemSetChangeEvent, Serializable {

        protected BaseItemSetChangeEvent(Container source) {
            super(source);
        }

        @Override
        public Container getContainer() {
            return (Container) getSource();
        }
    }

    // PropertySetChangeNotifier

    /**
     * Implementation of the corresponding method in
     * {@link PropertySetChangeNotifier}, override with the corresponding public
     * method and implement the interface to use this.
     * 
     * @see PropertySetChangeNotifier#addListener(com.vaadin.data.Container.PropertySetChangeListener)
     */
    protected void addListener(Container.PropertySetChangeListener listener) {
        if (getPropertySetChangeListeners() == null) {
            setPropertySetChangeListeners(new LinkedList<Container.PropertySetChangeListener>());
        }
        getPropertySetChangeListeners().add(listener);
    }

    /**
     * Implementation of the corresponding method in
     * {@link PropertySetChangeNotifier}, override with the corresponding public
     * method and implement the interface to use this.
     * 
     * @see PropertySetChangeNotifier#removeListener(com.vaadin.data.Container.
     *      PropertySetChangeListener)
     */
    protected void removeListener(Container.PropertySetChangeListener listener) {
        if (getPropertySetChangeListeners() != null) {
            getPropertySetChangeListeners().remove(listener);
        }
    }

    // ItemSetChangeNotifier

    /**
     * Implementation of the corresponding method in
     * {@link ItemSetChangeNotifier}, override with the corresponding public
     * method and implement the interface to use this.
     * 
     * @see ItemSetChangeNotifier#addListener(com.vaadin.data.Container.ItemSetChangeListener)
     */
    protected void addListener(Container.ItemSetChangeListener listener) {
        if (getItemSetChangeListeners() == null) {
            setItemSetChangeListeners(new LinkedList<Container.ItemSetChangeListener>());
        }
        getItemSetChangeListeners().add(listener);
    }

    /**
     * Implementation of the corresponding method in
     * {@link ItemSetChangeNotifier}, override with the corresponding public
     * method and implement the interface to use this.
     * 
     * @see ItemSetChangeNotifier#removeListener(com.vaadin.data.Container.ItemSetChangeListener)
     */
    protected void removeListener(Container.ItemSetChangeListener listener) {
        if (getItemSetChangeListeners() != null) {
            getItemSetChangeListeners().remove(listener);
        }
    }

    /**
     * Sends a simple Property set change event to all interested listeners.
     */
    protected void fireContainerPropertySetChange() {
        fireContainerPropertySetChange(new BasePropertySetChangeEvent(this));
    }

    /**
     * Sends a Property set change event to all interested listeners.
     * 
     * Use {@link #fireContainerPropertySetChange()} instead of this method
     * unless additional information about the exact changes is available and
     * should be included in the event.
     * 
     * @param event
     *            the property change event to send, optionally with additional
     *            information
     */
    protected void fireContainerPropertySetChange(
            Container.PropertySetChangeEvent event) {
        if (getPropertySetChangeListeners() != null) {
            final Object[] l = getPropertySetChangeListeners().toArray();
            for (int i = 0; i < l.length; i++) {
                ((Container.PropertySetChangeListener) l[i])
                        .containerPropertySetChange(event);
            }
        }
    }

    /**
     * Sends a simple Item set change event to all interested listeners,
     * indicating that anything in the contents may have changed (items added,
     * removed etc.).
     */
    protected void fireItemSetChange() {
        fireItemSetChange(new BaseItemSetChangeEvent(this));
    }

    /**
     * Sends an Item set change event to all registered interested listeners.
     * 
     * @param event
     *            the item set change event to send, optionally with additional
     *            information
     */
    protected void fireItemSetChange(ItemSetChangeEvent event) {
        if (getItemSetChangeListeners() != null) {
            final Object[] l = getItemSetChangeListeners().toArray();
            for (int i = 0; i < l.length; i++) {
                ((Container.ItemSetChangeListener) l[i])
                        .containerItemSetChange(event);
            }
        }
    }

    /**
     * Sets the property set change listener collection. For internal use only.
     * 
     * @param propertySetChangeListeners
     */
    protected void setPropertySetChangeListeners(
            Collection<Container.PropertySetChangeListener> propertySetChangeListeners) {
        this.propertySetChangeListeners = propertySetChangeListeners;
    }

    /**
     * Returns the property set change listener collection. For internal use
     * only.
     */
    protected Collection<Container.PropertySetChangeListener> getPropertySetChangeListeners() {
        return propertySetChangeListeners;
    }

    /**
     * Sets the item set change listener collection. For internal use only.
     * 
     * @param itemSetChangeListeners
     */
    protected void setItemSetChangeListeners(
            Collection<Container.ItemSetChangeListener> itemSetChangeListeners) {
        this.itemSetChangeListeners = itemSetChangeListeners;
    }

    /**
     * Returns the item set change listener collection. For internal use only.
     */
    protected Collection<Container.ItemSetChangeListener> getItemSetChangeListeners() {
        return itemSetChangeListeners;
    }

    public Collection<?> getListeners(Class<?> eventType) {
        if (Container.PropertySetChangeEvent.class.isAssignableFrom(eventType)) {
            if (propertySetChangeListeners == null) {
                return Collections.EMPTY_LIST;
            } else {
                return Collections
                        .unmodifiableCollection(propertySetChangeListeners);
            }
        } else if (Container.ItemSetChangeEvent.class
                .isAssignableFrom(eventType)) {
            if (itemSetChangeListeners == null) {
                return Collections.EMPTY_LIST;
            } else {
                return Collections
                        .unmodifiableCollection(itemSetChangeListeners);
            }
        }

        return Collections.EMPTY_LIST;
    }
}