Mercurial > projects > dwt-addons
comparison dwtx/core/commands/AbstractHandlerWithState.d @ 104:04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
These new wrappers now use the tango.util.containers instead of the tango.util.collections.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 07 Aug 2008 15:01:33 +0200 |
parents | 6518c18a01f7 |
children |
comparison
equal
deleted
inserted
replaced
103:2d6540440fe6 | 104:04b47443bb01 |
---|---|
17 import dwtx.core.commands.IObjectWithState; | 17 import dwtx.core.commands.IObjectWithState; |
18 import dwtx.core.commands.IStateListener; | 18 import dwtx.core.commands.IStateListener; |
19 import dwtx.core.commands.State; | 19 import dwtx.core.commands.State; |
20 | 20 |
21 import dwt.dwthelper.utils; | 21 import dwt.dwthelper.utils; |
22 | 22 import dwtx.dwtxhelper.Collection; |
23 import tango.util.collection.HashMap; | |
24 import tango.util.collection.model.Map; | |
25 import tango.util.collection.model.Set; | |
26 | 23 |
27 /** | 24 /** |
28 * <p> | 25 * <p> |
29 * An abstract implementation of {@link IObjectWithState}. This provides basic | 26 * An abstract implementation of {@link IObjectWithState}. This provides basic |
30 * handling for adding and remove state. When state is added, the handler | 27 * handling for adding and remove state. When state is added, the handler |
43 | 40 |
44 /** | 41 /** |
45 * The map of states currently held by this handler. If this handler has no | 42 * The map of states currently held by this handler. If this handler has no |
46 * state (generally, when inactive), then this will be <code>null</code>. | 43 * state (generally, when inactive), then this will be <code>null</code>. |
47 */ | 44 */ |
48 private Map!(String,State) states = null; | 45 private Map states = null; |
49 | 46 |
50 /** | 47 /** |
51 * <p> | 48 * <p> |
52 * Adds a state to this handler. This will add this handler as a listener to | 49 * Adds a state to this handler. This will add this handler as a listener to |
53 * the state, and then fire a handleStateChange so that the handler can | 50 * the state, and then fire a handleStateChange so that the handler can |
68 if (state is null) { | 65 if (state is null) { |
69 throw new NullPointerException("Cannot add a null state"); //$NON-NLS-1$ | 66 throw new NullPointerException("Cannot add a null state"); //$NON-NLS-1$ |
70 } | 67 } |
71 | 68 |
72 if (states is null) { | 69 if (states is null) { |
73 states = new HashMap!(String,State)(/+3+/); | 70 states = new HashMap(3); |
74 } | 71 } |
75 states.add(stateId, state); | 72 states.put(stateId, state); |
76 state.addListener(this); | 73 state.addListener(this); |
77 handleStateChange(state, null); | 74 handleStateChange(state, null); |
78 } | 75 } |
79 | 76 |
80 public final State getState(String stateId) { | 77 public final State getState(String stateId) { |
81 if ((states is null) || (states.drained())) { | 78 if ((states is null) || (states.isEmpty())) { |
82 return null; | 79 return null; |
83 } | 80 } |
84 | 81 |
85 return cast(State) states.get(stateId); | 82 return cast(State) states.get(stateId); |
86 } | 83 } |
87 | 84 |
88 public final String[] getStateIds() { | 85 public final String[] getStateIds() { |
89 if ((states is null) || (states.drained())) { | 86 if ((states is null) || (states.isEmpty())) { |
90 return null; | 87 return null; |
91 } | 88 } |
92 | 89 |
93 String[] res; | 90 Set stateIds = states.keySet(); |
94 foreach( k, v; states.keys() ){ | 91 return stringcast( stateIds.toArray()); |
95 res ~= k; | |
96 } | |
97 return res; | |
98 } | 92 } |
99 | 93 |
100 /** | 94 /** |
101 * <p> | 95 * <p> |
102 * Removes a state from this handler. This will remove this handler as a | 96 * Removes a state from this handler. This will remove this handler as a |
120 State state = cast(State) states.get(stateId); | 114 State state = cast(State) states.get(stateId); |
121 if (state !is null) { | 115 if (state !is null) { |
122 state.removeListener(this); | 116 state.removeListener(this); |
123 if (states !is null) { | 117 if (states !is null) { |
124 states.remove(state); | 118 states.remove(state); |
125 if (states.drained()) { | 119 if (states.isEmpty()) { |
126 states = null; | 120 states = null; |
127 } | 121 } |
128 } | 122 } |
129 } | 123 } |
130 } | 124 } |