|
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 java.awt.event.*; |
|
29 import java.util.Map; |
|
30 import javax.swing.*; |
|
31 import com.oracle.solaris.vp.util.misc.ObjectUtil; |
|
32 import com.oracle.solaris.vp.util.misc.property.*; |
|
33 |
|
34 /** |
|
35 * The {@code RadioButtonPropertySynchronizer} class synchronizes a {@link |
|
36 * MutableProperty} with a {@code Map} of {@code AbstractButton}s so that |
|
37 * changes in one will automatically be reflected in the other. |
|
38 */ |
|
39 public class RadioButtonPropertySynchronizer<T> |
|
40 extends PropertySynchronizer<T, Map<AbstractButton, T>> { |
|
41 |
|
42 // |
|
43 // Instance data |
|
44 // |
|
45 |
|
46 private ButtonGroup group = new ButtonGroup(); |
|
47 |
|
48 private ItemListener listener = |
|
49 new ItemListener() { |
|
50 @Override |
|
51 public void itemStateChanged(ItemEvent e) { |
|
52 objectChanged(); |
|
53 } |
|
54 }; |
|
55 |
|
56 // |
|
57 // Constructors |
|
58 // |
|
59 |
|
60 /** |
|
61 * Constructs a {@code RadioButtonPropertySynchronizer}. The buttons in the |
|
62 * given map will be added to a new {@link #getGroup ButtonGroup}. |
|
63 * |
|
64 * @param property |
|
65 * the property to synchronize with the button group |
|
66 * |
|
67 * @param map |
|
68 * a {@code Map} from buttons to the values they represent |
|
69 * |
|
70 * @param initFromProp |
|
71 * if {@code true}, initial synchronization will be from |
|
72 * the property to the buttons; if {@code false}, |
|
73 * initial synchronization will go in the other direction |
|
74 */ |
|
75 public RadioButtonPropertySynchronizer(MutableProperty<T> property, |
|
76 Map<AbstractButton, T> map, boolean initFromProp) { |
|
77 |
|
78 super(property, map, initFromProp); |
|
79 |
|
80 // Re-initialize now that group has been set |
|
81 init(initFromProp); |
|
82 |
|
83 for (AbstractButton button : map.keySet()) { |
|
84 button.addItemListener(listener); |
|
85 group.add(button); |
|
86 } |
|
87 } |
|
88 |
|
89 /** |
|
90 * Constructs a {@code RadioButtonPropertySynchronizer} with initial |
|
91 * synchronization from the property to the button map. |
|
92 */ |
|
93 public RadioButtonPropertySynchronizer(MutableProperty<T> property, |
|
94 Map<AbstractButton, T> map) { |
|
95 |
|
96 this(property, map, true); |
|
97 } |
|
98 |
|
99 // |
|
100 // PropertySynchronizer methods |
|
101 // |
|
102 |
|
103 @Override |
|
104 public void desynchronize() { |
|
105 super.desynchronize(); |
|
106 for (AbstractButton button : getObject().keySet()) { |
|
107 button.removeItemListener(listener); |
|
108 } |
|
109 } |
|
110 |
|
111 @Override |
|
112 public T getValue() { |
|
113 ButtonModel selected = group.getSelection(); |
|
114 if (selected != null) { |
|
115 Map<AbstractButton, T> map = getObject(); |
|
116 for (AbstractButton button : map.keySet()) { |
|
117 if (button.getModel().equals(selected)) { |
|
118 return map.get(button); |
|
119 } |
|
120 } |
|
121 } |
|
122 return null; |
|
123 } |
|
124 |
|
125 @Override |
|
126 public void init(boolean initFromProp) { |
|
127 // Wait until group has been set to initialize |
|
128 if (group != null) { |
|
129 super.init(initFromProp); |
|
130 } |
|
131 } |
|
132 |
|
133 @Override |
|
134 public void setValue(T value) { |
|
135 Map<AbstractButton, T> map = getObject(); |
|
136 for (AbstractButton button : map.keySet()) { |
|
137 Object mapValue = map.get(button); |
|
138 if (ObjectUtil.equals(value, mapValue)) { |
|
139 button.setSelected(true); |
|
140 return; |
|
141 } |
|
142 } |
|
143 group.clearSelection(); |
|
144 } |
|
145 |
|
146 // |
|
147 // RadioButtonPropertySynchronizer methods |
|
148 // |
|
149 |
|
150 public ButtonGroup getGroup() { |
|
151 return group; |
|
152 } |
|
153 } |