1 package fr.ifremer.quadrige3.ui.swing.model;
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 import org.jdesktop.beans.AbstractBean;
27 import org.jdesktop.beans.AbstractSerializableBean;
28 import org.nuiton.jaxx.application.listener.PropagatePropertyChangeListener;
29 import org.nuiton.util.CollectionUtil;
30 import org.nuiton.util.beans.Binder;
31
32 import java.beans.PropertyChangeListener;
33 import java.util.Collection;
34 import java.util.List;
35
36
37
38
39
40
41
42
43
44
45
46 public abstract class AbstractBeanUIModel<B, M extends AbstractBeanUIModel<B, M>> extends AbstractSerializableBean implements BeanPropertyChangeListener {
47
48
49
50
51 public static final String PROPERTY_MODIFY = "modify";
52
53
54
55 public static final String PROPERTY_VALID = "valid";
56
57
58
59 public static final String PROPERTY_LOADING = "loading";
60 private static final long serialVersionUID = 1L;
61 private final Binder<B, M> fromBeanBinder;
62 private final Binder<M, B> toBeanBinder;
63 protected boolean modify;
64 protected boolean valid;
65 protected boolean loading;
66 protected B delegateObject = newBean();
67
68
69
70
71
72
73
74 protected AbstractBeanUIModel(Binder<B, M> fromBeanBinder, Binder<M, B> toBeanBinder) {
75 this.fromBeanBinder = fromBeanBinder;
76 this.toBeanBinder = toBeanBinder;
77
78
79 addPropagatePropertyChangeListener();
80 }
81
82
83
84
85
86
87 @SuppressWarnings("unchecked")
88 public void fromBean(B bean) {
89 fromBeanBinder.copy(bean, (M) this);
90 }
91
92
93
94
95
96
97 @SuppressWarnings("unchecked")
98 public B toBean() {
99 B result = newBean();
100 toBeanBinder.copy((M) this, result);
101 return result;
102 }
103
104
105
106
107
108
109 public void setBean(B bean) {
110
111
112 removePropagatePropertyChangeListener();
113
114
115 delegateObject = bean;
116
117
118 addPropagatePropertyChangeListener();
119 }
120
121 private void addPropagatePropertyChangeListener() {
122 if (delegateObject instanceof AbstractBean) {
123 PropagatePropertyChangeListener.listenAndPropagateAll((AbstractBean) delegateObject, this);
124 }
125 }
126
127 private void removePropagatePropertyChangeListener() {
128 if (delegateObject instanceof AbstractBean) {
129 for (PropertyChangeListener listener : ((AbstractBean) delegateObject).getPropertyChangeListeners()) {
130 ((AbstractBean) delegateObject).removePropertyChangeListener(listener);
131 }
132 }
133 }
134
135
136
137
138
139
140 protected abstract B newBean();
141
142
143
144
145
146
147 public boolean isModify() {
148 return modify;
149 }
150
151
152
153
154
155
156 public void setModify(boolean modify) {
157 boolean oldValue = isModify();
158 this.modify = modify;
159 firePropertyChange(PROPERTY_MODIFY, oldValue, modify);
160 }
161
162
163
164
165
166
167 public boolean isValid() {
168 return valid;
169 }
170
171
172
173
174
175
176 public void setValid(boolean valid) {
177 boolean oldValue = isValid();
178 this.valid = valid;
179 firePropertyChange(PROPERTY_VALID, oldValue, valid);
180 }
181
182
183
184
185
186
187 public boolean isLoading() {
188 return loading;
189 }
190
191
192
193
194
195
196 public void setLoading(boolean loading) {
197 boolean oldValue = isLoading();
198 this.loading = loading;
199 firePropertyChange(PROPERTY_LOADING, oldValue, loading);
200 }
201
202
203
204
205
206
207
208
209 @Override
210 public void firePropertyChanged(String propertyName, Object oldValue, Object newValue) {
211 firePropertyChange(propertyName, oldValue, newValue);
212 setModelModify(propertyName, oldValue, newValue);
213 }
214
215 @Override
216 public void fireIndexedPropertyChanged(String propertyName, int index, Object oldValue, Object newValue) {
217 fireIndexedPropertyChange(propertyName, index, oldValue, newValue);
218 setModelModify(propertyName, oldValue, newValue);
219 }
220
221 private void setModelModify(String propertyName, Object oldValue, Object newValue) {
222
223
224 if (!PROPERTY_MODIFY.equals(propertyName)
225 && !PROPERTY_LOADING.equals(propertyName)
226 && oldValue != newValue) {
227 setModify(true);
228 }
229 }
230
231
232
233
234
235
236
237
238 protected B getChild(Collection<B> child, int index) {
239 return CollectionUtil.getOrNull(child, index);
240 }
241
242
243
244
245
246
247
248
249 protected B getChild(List<B> child, int index) {
250 return CollectionUtil.getOrNull(child, index);
251 }
252
253 }