1 11 12 package org.eclipse.jface.commands; 13 14 import java.util.HashMap ; 15 import java.util.HashSet ; 16 import java.util.Map ; 17 import java.util.Set ; 18 19 import org.eclipse.core.commands.IStateListener; 20 import org.eclipse.core.commands.State; 21 import org.eclipse.jface.menus.IMenuStateIds; 22 23 40 public class RadioState extends ToggleState { 41 42 47 private static final class RadioStateManager { 48 49 52 private static final class RadioGroup implements IStateListener { 53 54 58 private RadioState active = null; 59 60 64 private Set members = null; 65 66 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 89 private final void addMember(final RadioState state) { 90 if (members == null) { 91 members = new HashSet (5); 92 } 93 94 members.add(state); 95 state.addListener(this); 96 97 final Object value = state.getValue(); 98 if (value instanceof Boolean ) { 99 if (((Boolean ) value).booleanValue()) { 100 activateMember(state); 101 } 102 } 103 } 104 105 public final void handleStateChange(final State state, 106 final Object oldValue) { 107 final Object newValue = state.getValue(); 108 if (newValue instanceof Boolean ) { 109 if (((Boolean ) newValue).booleanValue()) { 110 activateMember((RadioState) state); 111 } 112 } 113 } 114 115 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 141 private static Map radioStatesById = null; 142 143 152 private static final void activateGroup(final String identifier, 153 final RadioState state) { 154 if (radioStatesById == null) { 155 return; 156 } 157 158 final Object currentValue = radioStatesById.get(identifier); 159 if (currentValue instanceof RadioGroup) { 160 final RadioGroup radioGroup = (RadioGroup) currentValue; 161 radioGroup.activateMember(state); 162 } 163 } 164 165 174 private static final void registerState(final String identifier, 175 final RadioState state) { 176 if (radioStatesById == null) { 177 radioStatesById = new HashMap (); 178 } 179 180 final Object 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 199 private static final void unregisterState(final String identifier, 200 final RadioState state) { 201 if (radioStatesById == null) { 202 return; 203 } 204 205 final Object currentValue = radioStatesById.get(identifier); 206 if (currentValue instanceof RadioGroup) { 207 final RadioGroup radioGroup = (RadioGroup) currentValue; 208 radioGroup.removeMember(state); 209 } 210 } 211 } 212 213 218 private String radioGroupIdentifier = null; 219 220 223 public void dispose() { 224 setRadioGroupIdentifier(null); 225 } 226 227 236 public final void setRadioGroupIdentifier(final String 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 253 public void setValue(final Object value) { 254 if (!(value instanceof Boolean )) { 255 throw new IllegalArgumentException ( 256 "RadioState takes a Boolean as a value"); } 258 259 if (((Boolean ) value).booleanValue() && (radioGroupIdentifier != null)) { 260 RadioStateManager.activateGroup(radioGroupIdentifier, this); 261 } 262 263 super.setValue(value); 264 } 265 } 266 | Popular Tags |