|
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 javax.swing.event.*; |
|
29 import com.oracle.solaris.vp.util.misc.ObjectUtil; |
|
30 |
|
31 /** |
|
32 * The {@code PropertyPropertySynchronizer} class synchronizes a {@link |
|
33 * MutableProperty} with another {@link MutableProperty} so that changes in one |
|
34 * will automatically be reflected in the other. |
|
35 */ |
|
36 public class PropertyPropertySynchronizer<T> |
|
37 extends PropertySynchronizer<T, MutableProperty<T>> { |
|
38 |
|
39 // |
|
40 // Instance data |
|
41 // |
|
42 |
|
43 private ChangeListener listener = |
|
44 new ChangeListener() { |
|
45 @Override |
|
46 public void stateChanged(ChangeEvent e) { |
|
47 objectChanged(); |
|
48 } |
|
49 }; |
|
50 |
|
51 // |
|
52 // Constructors |
|
53 // |
|
54 |
|
55 public PropertyPropertySynchronizer(MutableProperty<T> property, |
|
56 MutableProperty<T> property2, boolean initFromProp) { |
|
57 |
|
58 super(property, property2, initFromProp); |
|
59 |
|
60 property2.addChangeListener(listener); |
|
61 } |
|
62 |
|
63 /** |
|
64 * Constructs a {@code PropertyPropertySynchronizer} with initial |
|
65 * synchronization from {@code property} to {@code property2}. |
|
66 */ |
|
67 public PropertyPropertySynchronizer(MutableProperty<T> property, |
|
68 MutableProperty<T> property2) { |
|
69 |
|
70 this(property, property2, true); |
|
71 } |
|
72 |
|
73 // |
|
74 // PropertySynchronizer methods |
|
75 // |
|
76 |
|
77 @Override |
|
78 protected boolean differ() { |
|
79 return super.differ() || !ObjectUtil.equals(getSavedValue(), |
|
80 getProperty().getSavedValue()); |
|
81 } |
|
82 |
|
83 @Override |
|
84 public void desynchronize() { |
|
85 super.desynchronize(); |
|
86 getObject().removeChangeListener(listener); |
|
87 } |
|
88 |
|
89 @Override |
|
90 public T getValue() { |
|
91 return getObject().getValue(); |
|
92 } |
|
93 |
|
94 @Override |
|
95 protected void syncObjectChanges() { |
|
96 super.syncObjectChanges(); |
|
97 T value = getSavedValue(); |
|
98 getProperty().setSavedValue(value); |
|
99 } |
|
100 |
|
101 @Override |
|
102 protected void syncPropertyChanges() { |
|
103 super.syncPropertyChanges(); |
|
104 T value = getProperty().getSavedValue(); |
|
105 setSavedValue(value); |
|
106 } |
|
107 |
|
108 @Override |
|
109 public void setValue(T value) { |
|
110 getObject().setValue(value); |
|
111 } |
|
112 |
|
113 // |
|
114 // PropertyPropertySynchronizer methods |
|
115 // |
|
116 |
|
117 public T getSavedValue() { |
|
118 return getObject().getSavedValue(); |
|
119 } |
|
120 |
|
121 public void setSavedValue(T SavedValue) { |
|
122 getObject().setSavedValue(SavedValue); |
|
123 } |
|
124 } |