Source code: com/virtuosotechnologies/lib/basiccommand/BasicRadioGroupCommandNode.java
1 /*
2 ================================================================================
3
4 FILE: BasicRadioGroupCommandNode.java
5
6 PROJECT:
7
8 Virtuoso Utilities
9
10 CONTENTS:
11
12 A node representing a radio group node. These nodes are used for
13 enclosing and implementing mutual exclusion for radio groups.
14
15 PROGRAMMERS:
16
17 Daniel Azuma (DA) <dazuma@kagi.com>
18
19 COPYRIGHT:
20
21 Copyright (C) 2003 Daniel Azuma (dazuma@kagi.com)
22
23 This program is free software; you can redistribute it and/or
24 modify it under the terms of the GNU General Public License as
25 published by the Free Software Foundation; either version 2
26 of the License, or (at your option) any later version.
27
28 This program is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU General Public License for more details.
32
33 You should have received a copy of the GNU General Public
34 License along with this program; if not, write to
35 Free Software Foundation, Inc.
36 59 Temple Place, Suite 330
37 Boston, MA 02111-1307 USA
38
39 ================================================================================
40 */
41
42
43 package com.virtuosotechnologies.lib.basiccommand;
44
45
46 import com.virtuosotechnologies.lib.command.CommandNode;
47 import com.virtuosotechnologies.lib.command.CommandNodeFlavor;
48 import com.virtuosotechnologies.lib.command.CommandEvent;
49 import com.virtuosotechnologies.lib.propertyset.PropertySetListener;
50 import com.virtuosotechnologies.lib.propertyset.PropertySetEvent;
51
52
53 /**
54 * A node representing a radio group node. These nodes are used for
55 * enclosing and implementing mutual exclusion for radio groups.
56 */
57 public class BasicRadioGroupCommandNode
58 extends BasicCommandNode
59 {
60 private PropertySetListener listener_;
61
62
63 /**
64 * Constructor.
65 */
66 public BasicRadioGroupCommandNode()
67 {
68 addPropertySetListener(
69 listener_ = new PropertySetListener()
70 {
71 public void propertySetChanged(
72 PropertySetEvent ev)
73 {
74 if (ev.getKey().equals(SELECTION_STATE_PROPERTY))
75 {
76 childStateChanged((CommandNode)ev.getSource(),
77 Boolean.TRUE.equals(ev.getNewValue()));
78 }
79 else if (ev.getKey().equals(SELECTED_CHILD_PROPERTY))
80 {
81 groupStateChanged((CommandNode)ev.getOldValue(),
82 (CommandNode)ev.getNewValue());
83 }
84 }
85 });
86 }
87
88
89 /**
90 * Get the flavor of CommandNode
91 */
92 public CommandNodeFlavor getFlavor()
93 {
94 return RADIOGROUP_FLAVOR;
95 }
96
97
98 /**
99 * Invoke the command
100 */
101 public void commandInvoked(
102 CommandEvent ev)
103 {
104 }
105
106
107 /**
108 * Add a child node.
109 * Overriding this to listen on the child's state.
110 */
111 public void addChild(
112 CommandNode child)
113 {
114 super.addChild(child);
115 if (child.getFlavor().equalsOrExtends(TOGGLEITEM_FLAVOR))
116 {
117 if (Boolean.TRUE.equals(child.getValue(SELECTION_STATE_PROPERTY)))
118 {
119 putValue(SELECTED_CHILD_PROPERTY, child);
120 }
121 child.addPropertySetListener(listener_);
122 }
123 }
124
125
126 /**
127 * Insert a child node at the given index
128 * Overriding this to listen on the child's state.
129 */
130 public void insertChild(
131 int index,
132 CommandNode child)
133 {
134 super.insertChild(index, child);
135 if (child.getFlavor().equalsOrExtends(TOGGLEITEM_FLAVOR))
136 {
137 if (Boolean.TRUE.equals(child.getValue(SELECTION_STATE_PROPERTY)))
138 {
139 putValue(SELECTED_CHILD_PROPERTY, child);
140 }
141 child.addPropertySetListener(listener_);
142 }
143 }
144
145
146 /**
147 * Remove a child node.
148 * Overriding this to listen on the child's state.
149 */
150 public void removeNthChild(
151 int index)
152 {
153 CommandNode child = getNthChild(index);
154 child.removePropertySetListener(listener_);
155 super.removeNthChild(index);
156 if (getValue(SELECTED_CHILD_PROPERTY) == child)
157 {
158 putValue(SELECTED_CHILD_PROPERTY, null);
159 }
160 }
161
162
163 /**
164 * Remove all children.
165 * Overriding this to listen on the child's state.
166 */
167 public void removeAllChildren()
168 {
169 for (int i=0; i<getNumChildren(); ++i)
170 {
171 getNthChild(i).removePropertySetListener(listener_);
172 }
173 super.removeAllChildren();
174 putValue(SELECTED_CHILD_PROPERTY, null);
175 }
176
177
178 /**
179 * Group state changed
180 */
181 private void groupStateChanged(
182 CommandNode oldState,
183 CommandNode newState)
184 {
185 if (oldState != null)
186 {
187 // Don't set the child's state to false if it's being removed
188 if (isChild(oldState))
189 {
190 oldState.putValue(SELECTION_STATE_PROPERTY, Boolean.FALSE);
191 }
192 }
193 if (newState != null)
194 {
195 newState.putValue(SELECTION_STATE_PROPERTY, Boolean.TRUE);
196 }
197 }
198
199
200 /**
201 * Child state changed
202 */
203 private void childStateChanged(
204 CommandNode childChanged,
205 boolean newState)
206 {
207 CommandNode state = (CommandNode)getValue(SELECTED_CHILD_PROPERTY);
208 if (!newState && childChanged == state)
209 {
210 putValue(SELECTED_CHILD_PROPERTY, null);
211 }
212 else if (newState && childChanged != state)
213 {
214 putValue(SELECTED_CHILD_PROPERTY, childChanged);
215 }
216 }
217 }