comparison org.eclipse.jface/src/org/eclipse/jface/commands/RadioState.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
1 /*******************************************************************************
2 * Copyright (c) 2005, 2007 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 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13
14 module org.eclipse.jface.commands.RadioState;
15
16
17 import org.eclipse.core.commands.IStateListener;
18 import org.eclipse.core.commands.State;
19 import org.eclipse.jface.menus.IMenuStateIds;
20
21 import org.eclipse.jface.commands.ToggleState;
22
23 import java.lang.all;
24 import java.util.Collection;
25 import java.util.Map;
26 import java.util.HashMap;
27 import java.util.Set;
28 import java.util.HashSet;
29
30 /**
31 * <p>
32 * A piece of bool state grouped with other bool states. Of these states,
33 * only one may have a value of {@link Boolean#TRUE} at any given point in time.
34 * The values of all other states must be {@link Boolean#FALSE}.
35 * </p>
36 * <p>
37 * If this state is registered using {@link IMenuStateIds#STYLE}, then it will
38 * control the presentation of the command if displayed in the menus, tool bars
39 * or status line.
40 * </p>
41 * <p>
42 * Clients may instantiate or extend this interface.
43 * </p>
44 *
45 * @since 3.2
46 */
47 public class RadioState : ToggleState {
48
49 /**
50 * The manager of radio groups within the application. This ensures that
51 * only one member of a radio group is active at any one time, and tracks
52 * group memberships.
53 */
54 private static final class RadioStateManager {
55
56 /**
57 * A group of radio states with the same identifier.
58 */
59 private static final class RadioGroup : IStateListener {
60
61 /**
62 * The active state. If there is no active state, then this value is
63 * <code>null</code>.
64 */
65 private RadioState active = null;
66
67 /**
68 * The current members in this group. If there are no members, then
69 * this value is <code>nlistenerull</code>.
70 */
71 private Set members = null;
72
73 /**
74 * Activates a memeber. This checks to see if there are any other
75 * active members. If there are, they are deactivated.
76 *
77 * @param state
78 * The state that should become active; must not be
79 * <code>null</code>.
80 */
81 private final void activateMember(RadioState state) {
82 if (active !is null && active !is state) {
83 active.setValue(Boolean.FALSE);
84 }
85 active = state;
86 }
87
88 /**
89 * Adds a member to this radio group. If the state being added is
90 * active, then it replaces the currently active group memeber as
91 * the active state.
92 *
93 * @param state
94 * The state to add; must not be <code>null</code>.
95 */
96 private final void addMember(RadioState state) {
97 if (members is null) {
98 members = new HashSet();
99 }
100
101 members.add(state);
102 state.addListener(this);
103
104 Object value = state.getValue();
105 if ( auto v = cast(Boolean)value ) {
106 if (v.booleanValue()) {
107 activateMember(state);
108 }
109 }
110 }
111
112 public final void handleStateChange(State state,
113 Object oldValue) {
114 Object newValue = state.getValue();
115 if ( auto v = cast(Boolean)newValue) {
116 if (v.booleanValue()) {
117 activateMember(cast(RadioState) state);
118 }
119 }
120 }
121
122 /**
123 * Removes a member from this radio group. If the state was the
124 * active state, then there will be no active state.
125 *
126 * @param state
127 * The state to remove; must not be <code>null</code>.
128 */
129 private final void removeMember(RadioState state) {
130 state.removeListener(this);
131 if (active is state) {
132 active = null;
133 }
134
135 if (members is null) {
136 return;
137 }
138 members.remove(state);
139 }
140 }
141
142 /**
143 * The map of radio states indexed by identifier (<code>String</code>).
144 * The radio states is either a single <code>RadioState</code>
145 * instance or a <code>Collection</code> of <code>RadioState</code>
146 * instances.
147 */
148 private static Map radioStatesById = null;
149
150 /**
151 * Activates a particular state within a given group.
152 *
153 * @param identifier
154 * The identifier of the group to which the state belongs;
155 * must not be <code>null</code>.
156 * @param state
157 * The state to activate; must not be <code>null</code>.
158 */
159 private static final void activateGroup(String identifier,
160 RadioState state) {
161 if (radioStatesById is null) {
162 return;
163 }
164
165 auto currentValue = radioStatesById.get(identifier);
166 if ( auto grp = cast(RadioGroup)currentValue) {
167 RadioGroup radioGroup = grp;
168 radioGroup.activateMember(state);
169 }
170 }
171
172 /**
173 * Registers a piece of state with the radio manager.
174 *
175 * @param identifier
176 * The identifier of the radio group; must not be
177 * <code>null</code>.
178 * @param state
179 * The state to register; must not be <code>null</code>.
180 */
181 private static final void registerState(String identifier,
182 RadioState state) {
183 if (radioStatesById is null) {
184 radioStatesById = new HashMap();
185 }
186
187 auto currentValue = radioStatesById.get(identifier);
188 RadioGroup radioGroup;
189 if ( auto grp = cast(RadioGroup)currentValue) {
190 radioGroup = grp;
191 } else {
192 radioGroup = new RadioGroup();
193 }
194 radioGroup.addMember(state);
195 }
196
197 /**
198 * Unregisters a piece of state from the radio manager.
199 *
200 * @param identifier
201 * The identifier of the radio group; must not be
202 * <code>null</code>.
203 * @param state
204 * The state to unregister; must not be <code>null</code>.
205 */
206 private static final void unregisterState(String identifier,
207 RadioState state) {
208 if (radioStatesById is null) {
209 return;
210 }
211
212 auto currentValue = radioStatesById.get(identifier);
213 if ( auto grp = cast(RadioGroup)currentValue ) {
214 final RadioGroup radioGroup = grp;
215 radioGroup.removeMember(state);
216 }
217 }
218 }
219
220 /**
221 * The identifier of the radio group to which this state belongs. This value
222 * may be <code>null</code> if this state doesn't really belong to a group
223 * (yet).
224 */
225 private String radioGroupIdentifier = null;
226
227 /**
228 * Unregisters this state from the manager, which detaches the listeners.
229 */
230 public override void dispose() {
231 setRadioGroupIdentifier(null);
232 }
233
234 /**
235 * Sets the identifier of the radio group for this piece of state. If the
236 * identifier is cleared, then the state is unregistered.
237 *
238 * @param identifier
239 * The identifier of the radio group for this state; may be
240 * <code>null</code> if the identifier is being cleared.
241 *
242 */
243 public final void setRadioGroupIdentifier(String identifier) {
244 if (identifier is null) {
245 RadioStateManager.unregisterState(radioGroupIdentifier, this);
246 radioGroupIdentifier = null;
247 } else {
248 radioGroupIdentifier = identifier;
249 RadioStateManager.registerState(identifier, this);
250 }
251 }
252
253 /**
254 * Sets the value for this object. This notifies the radio state manager of
255 * the change.
256 *
257 * @param value
258 * The new value; should be a <code>Boolean</code>.
259 */
260 public override void setValue(Object value) {
261 if (!( cast(Boolean)value )) {
262 throw new IllegalArgumentException(
263 "RadioState takes a Boolean as a value"); //$NON-NLS-1$
264 }
265
266 if (( cast(Boolean)value ).booleanValue() && (radioGroupIdentifier !is null)) {
267 RadioStateManager.activateGroup(radioGroupIdentifier, this);
268 }
269
270 super.setValue(value);
271 }
272 }