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.misc.property; |
|
27 |
|
28 import java.beans.*; |
|
29 import javax.swing.event.*; |
|
30 import com.oracle.solaris.vp.util.misc.*; |
|
31 import com.oracle.solaris.vp.util.misc.converter.DualConverter; |
|
32 import com.oracle.solaris.vp.util.misc.event.PropertyChangeListeners; |
|
33 import com.oracle.solaris.vp.util.swing.event.ChangeListeners; |
|
34 |
|
35 public class BasicMutableProperty<T> implements MutableProperty<T> { |
|
36 // |
|
37 // Instance data |
|
38 // |
|
39 |
|
40 private String name; |
|
41 private DualConverter<String, T> converter; |
|
42 private T value; |
|
43 private T saved; |
|
44 |
|
45 private ChangeListeners changeListeners = new ChangeListeners(); |
|
46 private ChangeEvent event = new ChangeEvent(this); |
|
47 |
|
48 private PropertyChangeListeners propListeners = |
|
49 new PropertyChangeListeners(); |
|
50 |
|
51 // |
|
52 // Constructors |
|
53 // |
|
54 |
|
55 public BasicMutableProperty(String name, DualConverter<String, T> converter) |
|
56 { |
|
57 this.name = name; |
|
58 this.converter = converter; |
|
59 } |
|
60 |
|
61 public BasicMutableProperty(String name) { |
|
62 this(name, null); |
|
63 } |
|
64 |
|
65 public BasicMutableProperty(DualConverter<String, T> converter) { |
|
66 this(null, converter); |
|
67 } |
|
68 |
|
69 public BasicMutableProperty() { |
|
70 this(null, null); |
|
71 } |
|
72 |
|
73 // |
|
74 // Changeable methods |
|
75 // |
|
76 |
|
77 @Override |
|
78 public void addChangeListener(ChangeListener listener) { |
|
79 changeListeners.add(listener); |
|
80 } |
|
81 |
|
82 @Override |
|
83 public boolean removeChangeListener(ChangeListener listener) { |
|
84 return changeListeners.remove(listener); |
|
85 } |
|
86 |
|
87 @Override |
|
88 public void reset() { |
|
89 setValue(getSavedValue()); |
|
90 } |
|
91 |
|
92 @Override |
|
93 public void save() { |
|
94 setSavedValue(getValue()); |
|
95 } |
|
96 |
|
97 // |
|
98 // MutableProperty methods |
|
99 // |
|
100 |
|
101 @Override |
|
102 public void addPropertyChangeListener(PropertyChangeListener listener) { |
|
103 propListeners.add(listener); |
|
104 } |
|
105 |
|
106 @Override |
|
107 public DualConverter<String, T> getConverter() { |
|
108 return converter; |
|
109 } |
|
110 |
|
111 @Override |
|
112 public String getPropertyName() { |
|
113 return name; |
|
114 } |
|
115 |
|
116 @Override |
|
117 public T getSavedValue() { |
|
118 return saved; |
|
119 } |
|
120 |
|
121 @Override |
|
122 public T getValue() { |
|
123 return value; |
|
124 } |
|
125 |
|
126 @Override |
|
127 public boolean isChanged() { |
|
128 return !ObjectUtil.equals(getValue(), getSavedValue()); |
|
129 } |
|
130 |
|
131 @Override |
|
132 public boolean removePropertyChangeListener(PropertyChangeListener listener) |
|
133 { |
|
134 return propListeners.remove(listener); |
|
135 } |
|
136 |
|
137 @Override |
|
138 public void setSavedValue(T saved) { |
|
139 T old = this.saved; |
|
140 if (!ObjectUtil.equals(old, saved)) { |
|
141 this.saved = saved; |
|
142 propertyChange(old, saved); |
|
143 stateChanged(); |
|
144 } |
|
145 } |
|
146 |
|
147 @Override |
|
148 public void setValue(T value) { |
|
149 T old = this.value; |
|
150 if (!ObjectUtil.equals(old, value)) { |
|
151 this.value = value; |
|
152 propertyChange(old, value); |
|
153 stateChanged(); |
|
154 } |
|
155 } |
|
156 |
|
157 @Override |
|
158 public void update(T value, boolean force) { |
|
159 boolean changed = isChanged(); |
|
160 setSavedValue(value); |
|
161 if (force || !changed) { |
|
162 reset(); |
|
163 } |
|
164 } |
|
165 |
|
166 // |
|
167 // Object methods |
|
168 // |
|
169 |
|
170 @Override |
|
171 public String toString() { |
|
172 return String.format("%s: name=[%s] saved=[%s] value=[%s]", |
|
173 DebugUtil.toBaseName(this), getPropertyName(), getSavedValue(), |
|
174 getValue()); |
|
175 } |
|
176 |
|
177 // |
|
178 // BasicMutableProperty methods |
|
179 // |
|
180 |
|
181 /** |
|
182 * Notifies registered {@code PropertyChangeListener}s of a change in the |
|
183 * current or saved value this {@code MutableProperty}. |
|
184 */ |
|
185 protected void propertyChange(T old, T value) { |
|
186 PropertyChangeEvent event = new PropertyChangeEvent(this, |
|
187 getPropertyName(), old, value); |
|
188 propListeners.propertyChange(event); |
|
189 } |
|
190 |
|
191 public void setConverter(DualConverter<String, T> converter) { |
|
192 this.converter = converter; |
|
193 } |
|
194 |
|
195 /** |
|
196 * Notifies registered {@code ChangeListener}s of a change in this {@code |
|
197 * MutableProperty}. |
|
198 */ |
|
199 protected void stateChanged() { |
|
200 changeListeners.stateChanged(event); |
|
201 } |
|
202 } |
|