KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > commands > RadioState


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jface.commands;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.eclipse.core.commands.IStateListener;
20 import org.eclipse.core.commands.State;
21 import org.eclipse.jface.menus.IMenuStateIds;
22
23 /**
24  * <p>
25  * A piece of boolean state grouped with other boolean states. Of these states,
26  * only one may have a value of {@link Boolean#TRUE} at any given point in time.
27  * The values of all other states must be {@link Boolean#FALSE}.
28  * </p>
29  * <p>
30  * If this state is registered using {@link IMenuStateIds#STYLE}, then it will
31  * control the presentation of the command if displayed in the menus, tool bars
32  * or status line.
33  * </p>
34  * <p>
35  * Clients may instantiate or extend this interface.
36  * </p>
37  *
38  * @since 3.2
39  */

40 public class RadioState extends ToggleState {
41
42     /**
43      * The manager of radio groups within the application. This ensures that
44      * only one member of a radio group is active at any one time, and tracks
45      * group memberships.
46      */

47     private static final class RadioStateManager {
48
49         /**
50          * A group of radio states with the same identifier.
51          */

52         private static final class RadioGroup implements IStateListener {
53
54             /**
55              * The active state. If there is no active state, then this value is
56              * <code>null</code>.
57              */

58             private RadioState active = null;
59
60             /**
61              * The current members in this group. If there are no members, then
62              * this value is <code>nlistenerull</code>.
63              */

64             private Set JavaDoc members = null;
65
66             /**
67              * Activates a memeber. This checks to see if there are any other
68              * active members. If there are, they are deactivated.
69              *
70              * @param state
71              * The state that should become active; must not be
72              * <code>null</code>.
73              */

74             private final void activateMember(final RadioState state) {
75                 if (active!=null && active != state) {
76                     active.setValue(Boolean.FALSE);
77                 }
78                 active = state;
79             }
80
81             /**
82              * Adds a member to this radio group. If the state being added is
83              * active, then it replaces the currently active group memeber as
84              * the active state.
85              *
86              * @param state
87              * The state to add; must not be <code>null</code>.
88              */

89             private final void addMember(final RadioState state) {
90                 if (members == null) {
91                     members = new HashSet JavaDoc(5);
92                 }
93
94                 members.add(state);
95                 state.addListener(this);
96
97                 final Object JavaDoc value = state.getValue();
98                 if (value instanceof Boolean JavaDoc) {
99                     if (((Boolean JavaDoc) value).booleanValue()) {
100                         activateMember(state);
101                     }
102                 }
103             }
104
105             public final void handleStateChange(final State state,
106                     final Object JavaDoc oldValue) {
107                 final Object JavaDoc newValue = state.getValue();
108                 if (newValue instanceof Boolean JavaDoc) {
109                     if (((Boolean JavaDoc) newValue).booleanValue()) {
110                         activateMember((RadioState) state);
111                     }
112                 }
113             }
114
115             /**
116              * Removes a member from this radio group. If the state was the
117              * active state, then there will be no active state.
118              *
119              * @param state
120              * The state to remove; must not be <code>null</code>.
121              */

122             private final void removeMember(final RadioState state) {
123                 state.removeListener(this);
124                 if (active == state) {
125                     active = null;
126                 }
127
128                 if (members == null) {
129                     return;
130                 }
131                 members.remove(state);
132             }
133         }
134
135         /**
136          * The map of radio states indexed by identifier (<code>String</code>).
137          * The radio states is either a single <code>RadioState</code>
138          * instance or a <code>Collection</code> of <code>RadioState</code>
139          * instances.
140          */

141         private static Map JavaDoc radioStatesById = null;
142
143         /**
144          * Activates a particular state within a given group.
145          *
146          * @param identifier
147          * The identifier of the group to which the state belongs;
148          * must not be <code>null</code>.
149          * @param state
150          * The state to activate; must not be <code>null</code>.
151          */

152         private static final void activateGroup(final String JavaDoc identifier,
153                 final RadioState state) {
154             if (radioStatesById == null) {
155                 return;
156             }
157
158             final Object JavaDoc currentValue = radioStatesById.get(identifier);
159             if (currentValue instanceof RadioGroup) {
160                 final RadioGroup radioGroup = (RadioGroup) currentValue;
161                 radioGroup.activateMember(state);
162             }
163         }
164
165         /**
166          * Registers a piece of state with the radio manager.
167          *
168          * @param identifier
169          * The identifier of the radio group; must not be
170          * <code>null</code>.
171          * @param state
172          * The state to register; must not be <code>null</code>.
173          */

174         private static final void registerState(final String JavaDoc identifier,
175                 final RadioState state) {
176             if (radioStatesById == null) {
177                 radioStatesById = new HashMap JavaDoc();
178             }
179
180             final Object JavaDoc currentValue = radioStatesById.get(identifier);
181             final RadioGroup radioGroup;
182             if (currentValue instanceof RadioGroup) {
183                 radioGroup = (RadioGroup) currentValue;
184             } else {
185                 radioGroup = new RadioGroup();
186             }
187             radioGroup.addMember(state);
188         }
189
190         /**
191          * Unregisters a piece of state from the radio manager.
192          *
193          * @param identifier
194          * The identifier of the radio group; must not be
195          * <code>null</code>.
196          * @param state
197          * The state to unregister; must not be <code>null</code>.
198          */

199         private static final void unregisterState(final String JavaDoc identifier,
200                 final RadioState state) {
201             if (radioStatesById == null) {
202                 return;
203             }
204
205             final Object JavaDoc currentValue = radioStatesById.get(identifier);
206             if (currentValue instanceof RadioGroup) {
207                 final RadioGroup radioGroup = (RadioGroup) currentValue;
208                 radioGroup.removeMember(state);
209             }
210         }
211     }
212
213     /**
214      * The identifier of the radio group to which this state belongs. This value
215      * may be <code>null</code> if this state doesn't really belong to a group
216      * (yet).
217      */

218     private String JavaDoc radioGroupIdentifier = null;
219
220     /**
221      * Unregisters this state from the manager, which detaches the listeners.
222      */

223     public void dispose() {
224         setRadioGroupIdentifier(null);
225     }
226
227     /**
228      * Sets the identifier of the radio group for this piece of state. If the
229      * identifier is cleared, then the state is unregistered.
230      *
231      * @param identifier
232      * The identifier of the radio group for this state; may be
233      * <code>null</code> if the identifier is being cleared.
234      *
235      */

236     public final void setRadioGroupIdentifier(final String JavaDoc identifier) {
237         if (identifier == null) {
238             RadioStateManager.unregisterState(radioGroupIdentifier, this);
239             radioGroupIdentifier = null;
240         } else {
241             radioGroupIdentifier = identifier;
242             RadioStateManager.registerState(identifier, this);
243         }
244     }
245
246     /**
247      * Sets the value for this object. This notifies the radio state manager of
248      * the change.
249      *
250      * @param value
251      * The new value; should be a <code>Boolean</code>.
252      */

253     public void setValue(final Object JavaDoc value) {
254         if (!(value instanceof Boolean JavaDoc)) {
255             throw new IllegalArgumentException JavaDoc(
256                     "RadioState takes a Boolean as a value"); //$NON-NLS-1$
257
}
258
259         if (((Boolean JavaDoc) value).booleanValue() && (radioGroupIdentifier != null)) {
260             RadioStateManager.activateGroup(radioGroupIdentifier, this);
261         }
262
263         super.setValue(value);
264     }
265 }
266
Popular Tags