blob: ea612b2c2b706197ae698aac794cd33fa46702b6 (
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
/*
@ITMillApache2LicenseForJavaFiles@
*/
package com.vaadin.data.util;
import java.util.LinkedList;
import com.vaadin.data.Property;
/**
* Formatting proxy for a property.
*
* <p>
* This class can be used to implement formatting for any type of Property
* datasources. The idea is to connect this as proxy between UI component and
* the original datasource.
* </p>
*
* <p>
* For example <code>
* textfield.setPropertyDataSource(new PropertyFormatter(property) {
public String format(Object value) {
return ((Double) value).toString() + "000000000";
}
public Object parse(String formattedValue) throws Exception {
return Double.parseDouble(formattedValue);
}
});</code> adds formatter for Double-typed property that extends standard
* "1.0" notation with more zeroes.
* </p>
*
* @author IT Mill Ltd.
* @since 5.3.0
*/
@SuppressWarnings("serial")
public abstract class PropertyFormatter implements Property,
Property.ValueChangeNotifier, Property.ValueChangeListener,
Property.ReadOnlyStatusChangeListener,
Property.ReadOnlyStatusChangeNotifier {
/**
* Internal list of registered value change listeners.
*/
private LinkedList valueChangeListeners = null;
/**
* Internal list of registered read-only status change listeners.
*/
private LinkedList readOnlyStatusChangeListeners = null;
/** Datasource that stores the actual value. */
Property dataSource;
/**
* Construct a new formatter that is connected to given datasource.
*
* @param propertyDataSource
* to connect this property to.
*/
public PropertyFormatter(Property propertyDataSource) {
setPropertyDataSource(propertyDataSource);
}
/**
* Gets the current data source of the formatter, if any.
*
* @return the current data source as a Property, or <code>null</code> if
* none defined.
*/
public Property getPropertyDataSource() {
return dataSource;
}
/**
* Sets the specified Property as the data source for the formatter.
*
*
* <p>
* Remember that new data sources getValue() must return objects that are
* compatible with parse() and format() methods.
* </p>
*
* @param newDataSource
* the new data source Property.
*/
public void setPropertyDataSource(Property newDataSource) {
boolean readOnly = false;
String prevValue = null;
if (dataSource != null) {
if (dataSource instanceof Property.ValueChangeNotifier) {
((Property.ValueChangeNotifier) dataSource)
.removeListener(this);
}
if (dataSource instanceof Property.ReadOnlyStatusChangeListener) {
((Property.ReadOnlyStatusChangeNotifier) dataSource)
.removeListener(this);
}
readOnly = isReadOnly();
prevValue = toString();
}
dataSource = newDataSource;
if (dataSource != null) {
if (dataSource instanceof Property.ValueChangeNotifier) {
((Property.ValueChangeNotifier) dataSource).addListener(this);
}
if (dataSource instanceof Property.ReadOnlyStatusChangeListener) {
((Property.ReadOnlyStatusChangeNotifier) dataSource)
.addListener(this);
}
}
if (isReadOnly() != readOnly) {
fireReadOnlyStatusChange();
}
String newVal = toString();
if ((prevValue == null && newVal != null)
|| (prevValue != null && !prevValue.equals(newVal))) {
fireValueChange();
}
}
/* Documented in the interface */
public Class getType() {
return String.class;
}
/**
* Get the formatted value.
*
* @return If the datasource returns null, this is null. Otherwise this is
* String given by format().
*/
public Object getValue() {
return toString();
}
/**
* Get the formatted value.
*
* @return If the datasource returns null, this is null. Otherwise this is
* String given by format().
*/
@Override
public String toString() {
Object value = dataSource == null ? false : dataSource.getValue();
if (value == null) {
return null;
}
return format(value);
}
/** Reflects the read-only status of the datasource. */
public boolean isReadOnly() {
return dataSource == null ? false : dataSource.isReadOnly();
}
/**
* This method must be implemented to format the values received from
* DataSource.
*
* @param value
* Value object got from the datasource. This is guaranteed to be
* non-null and of the type compatible with getType() of the
* datasource.
* @return
*/
abstract public String format(Object value);
/**
* Parse string and convert it to format compatible with datasource.
*
* The method is required to assure that parse(format(x)) equals x.
*
* @param formattedValue
* This is guaranteed to be non-null string.
* @return Non-null value compatible with datasource.
* @throws Exception
* Any type of exception can be thrown to indicate that the
* conversion was not succesful.
*/
abstract public Object parse(String formattedValue) throws Exception;
/**
* Sets the Property's read-only mode to the specified status.
*
* @param newStatus
* the new read-only status of the Property.
*/
public void setReadOnly(boolean newStatus) {
if (dataSource != null) {
dataSource.setReadOnly(newStatus);
}
}
public void setValue(Object newValue) throws ReadOnlyException,
ConversionException {
if (dataSource == null) {
return;
}
if (newValue == null) {
dataSource.setValue(null);
}
try {
dataSource.setValue(parse((String) newValue));
if (!newValue.equals(toString())) {
fireValueChange();
}
} catch (Exception e) {
if (e instanceof ConversionException) {
throw (ConversionException) e;
} else {
throw new ConversionException(e);
}
}
}
/**
* An <code>Event</code> object specifying the ObjectProperty whose value
* has changed.
*
* @author IT Mill Ltd.
* @since 5.3.0
*/
private class ValueChangeEvent extends java.util.EventObject implements
Property.ValueChangeEvent {
/**
* Constructs a new value change event for this object.
*
* @param source
* the source object of the event.
*/
protected ValueChangeEvent(PropertyFormatter source) {
super(source);
}
/**
* Gets the Property whose read-only state has changed.
*
* @return source the Property of the event.
*/
public Property getProperty() {
return (Property) getSource();
}
}
/**
* An <code>Event</code> object specifying the Property whose read-only
* status has been changed.
*
* @author IT Mill Ltd.
* @since 5.3.0
*/
private class ReadOnlyStatusChangeEvent extends java.util.EventObject
implements Property.ReadOnlyStatusChangeEvent {
/**
* Constructs a new read-only status change event for this object.
*
* @param source
* source object of the event
*/
protected ReadOnlyStatusChangeEvent(PropertyFormatter source) {
super(source);
}
/**
* Gets the Property whose read-only state has changed.
*
* @return source Property of the event.
*/
public Property getProperty() {
return (Property) getSource();
}
}
/**
* Removes a previously registered value change listener.
*
* @param listener
* the listener to be removed.
*/
public void removeListener(Property.ValueChangeListener listener) {
if (valueChangeListeners != null) {
valueChangeListeners.remove(listener);
}
}
/**
* Registers a new value change listener for this ObjectProperty.
*
* @param listener
* the new Listener to be registered
*/
public void addListener(Property.ValueChangeListener listener) {
if (valueChangeListeners == null) {
valueChangeListeners = new LinkedList();
}
valueChangeListeners.add(listener);
}
/**
* Registers a new read-only status change listener for this Property.
*
* @param listener
* the new Listener to be registered
*/
public void addListener(Property.ReadOnlyStatusChangeListener listener) {
if (readOnlyStatusChangeListeners == null) {
readOnlyStatusChangeListeners = new LinkedList();
}
readOnlyStatusChangeListeners.add(listener);
}
/**
* Removes a previously registered read-only status change listener.
*
* @param listener
* the listener to be removed.
*/
public void removeListener(Property.ReadOnlyStatusChangeListener listener) {
if (readOnlyStatusChangeListeners != null) {
readOnlyStatusChangeListeners.remove(listener);
}
}
/**
* Sends a value change event to all registered listeners.
*/
private void fireValueChange() {
if (valueChangeListeners != null) {
final Object[] l = valueChangeListeners.toArray();
final Property.ValueChangeEvent event = new ValueChangeEvent(this);
for (int i = 0; i < l.length; i++) {
((Property.ValueChangeListener) l[i]).valueChange(event);
}
}
}
/**
* Sends a read only status change event to all registered listeners.
*/
private void fireReadOnlyStatusChange() {
if (readOnlyStatusChangeListeners != null) {
final Object[] l = readOnlyStatusChangeListeners.toArray();
final Property.ReadOnlyStatusChangeEvent event = new ReadOnlyStatusChangeEvent(
this);
for (int i = 0; i < l.length; i++) {
((Property.ReadOnlyStatusChangeListener) l[i])
.readOnlyStatusChange(event);
}
}
}
/**
* Listens for changes in the datasource.
*
* This should not be called directly.
*/
public void valueChange(com.vaadin.data.Property.ValueChangeEvent event) {
fireValueChange();
}
/**
* Listens for changes in the datasource.
*
* This should not be called directly.
*/
public void readOnlyStatusChange(
com.vaadin.data.Property.ReadOnlyStatusChangeEvent event) {
fireReadOnlyStatusChange();
}
}
|