|
1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.util.swing.property; |
|
27 |
|
28 import javax.swing.event.*; |
|
29 import javax.swing.text.JTextComponent; |
|
30 import com.oracle.solaris.vp.util.misc.converter.StringConverter; |
|
31 import com.oracle.solaris.vp.util.misc.property.*; |
|
32 import com.oracle.solaris.vp.util.swing.DocumentAdapter; |
|
33 |
|
34 /** |
|
35 * The {@code TextComponentPropertySynchronizer} class synchronizes a {@link |
|
36 * MutableProperty} with a {@code JTextComponent} so that changes in one will |
|
37 * automatically be reflected in the other. |
|
38 */ |
|
39 public class TextComponentPropertySynchronizer<T, C extends JTextComponent> |
|
40 extends PropertySynchronizer<T, C> { |
|
41 |
|
42 // |
|
43 // Instance data |
|
44 // |
|
45 |
|
46 private DocumentListener listener = |
|
47 new DocumentAdapter() { |
|
48 @Override |
|
49 public void docUpdate(DocumentEvent e) { |
|
50 objectChanged(); |
|
51 } |
|
52 }; |
|
53 |
|
54 // |
|
55 // Constructors |
|
56 // |
|
57 |
|
58 public TextComponentPropertySynchronizer(MutableProperty<T> property, |
|
59 C comp, boolean initFromProp) { |
|
60 |
|
61 super(property, comp, initFromProp); |
|
62 comp.getDocument().addDocumentListener(listener); |
|
63 } |
|
64 |
|
65 /** |
|
66 * Constructs a {@code TextComponentPropertySynchronizer} with initial |
|
67 * synchronization from the property to the {@code JTextComponent}. |
|
68 */ |
|
69 public TextComponentPropertySynchronizer(MutableProperty<T> property, |
|
70 C comp) { |
|
71 |
|
72 this(property, comp, true); |
|
73 } |
|
74 |
|
75 // |
|
76 // PropertySynchronizer methods |
|
77 // |
|
78 |
|
79 @Override |
|
80 public void desynchronize() { |
|
81 super.desynchronize(); |
|
82 getObject().getDocument().removeDocumentListener(listener); |
|
83 } |
|
84 |
|
85 @Override |
|
86 public T getValue() { |
|
87 String text = getObject().getText(); |
|
88 return getProperty().getConverter().convert(text); |
|
89 } |
|
90 |
|
91 @Override |
|
92 public void setValue(T value) { |
|
93 String text = getProperty().getConverter().revert(value); |
|
94 getObject().setText(text); |
|
95 } |
|
96 } |