42 |
import java.io.Serializable; |
import java.io.Serializable; |
43 |
import java.text.Format; |
import java.text.Format; |
44 |
import java.text.ParseException; |
import java.text.ParseException; |
45 |
|
import java.util.Date; |
46 |
|
|
47 |
|
import javax.swing.text.DateFormatter; |
48 |
|
import javax.swing.text.DefaultFormatter; |
49 |
import javax.swing.text.Document; |
import javax.swing.text.Document; |
50 |
import javax.swing.text.DocumentFilter; |
import javax.swing.text.DocumentFilter; |
51 |
import javax.swing.text.NavigationFilter; |
import javax.swing.text.NavigationFilter; |
301 |
return; |
return; |
302 |
|
|
303 |
// format value |
// format value |
304 |
AbstractFormatter formatter = formatterFactory.getFormatter(this); |
AbstractFormatter formatter = createFormatter(newValue); |
305 |
try |
try |
306 |
{ |
{ |
307 |
setText(formatter.valueToString(newValue)); |
setText(formatter.valueToString(newValue)); |
315 |
value = newValue; |
value = newValue; |
316 |
firePropertyChange("value", oldValue, newValue); |
firePropertyChange("value", oldValue, newValue); |
317 |
} |
} |
318 |
|
|
319 |
|
/** |
320 |
|
* A helper method that attempts to create a formatter that is suitable |
321 |
|
* to format objects of the type like <code>value</code>. |
322 |
|
* |
323 |
|
* If <code>formatterFactory</code> is not null and the returned formatter |
324 |
|
* is also not <code>null</code> then this formatter is used. Otherwise we |
325 |
|
* try to create one based on the type of <code>value</code>. |
326 |
|
* |
327 |
|
* @param value an object which should be formatted by the formatter |
328 |
|
* |
329 |
|
* @return a formatter able to format objects of the class of |
330 |
|
* <code>value</code> |
331 |
|
*/ |
332 |
|
AbstractFormatter createFormatter(Object value) |
333 |
|
{ |
334 |
|
AbstractFormatter formatter = null; |
335 |
|
if (formatterFactory != null |
336 |
|
&& formatterFactory.getFormatter(this) != null) |
337 |
|
formatter = formatterFactory.getFormatter(this); |
338 |
|
else |
339 |
|
{ |
340 |
|
if (value instanceof Date) |
341 |
|
formatter = new DateFormatter(); |
342 |
|
else |
343 |
|
formatter = new DefaultFormatter(); |
344 |
|
} |
345 |
|
return formatter; |
346 |
|
} |
347 |
} |
} |