1 11 12 package org.eclipse.core.commands; 13 14 import java.util.HashMap ; 15 import java.util.Map ; 16 import java.util.Set ; 17 18 32 public abstract class AbstractHandlerWithState extends AbstractHandler 33 implements IObjectWithState, IStateListener { 34 35 39 private Map states = null; 40 41 58 public void addState(final String stateId, final State state) { 59 if (state == null) { 60 throw new NullPointerException ("Cannot add a null state"); } 62 63 if (states == null) { 64 states = new HashMap (3); 65 } 66 states.put(stateId, state); 67 state.addListener(this); 68 handleStateChange(state, null); 69 } 70 71 public final State getState(final String stateId) { 72 if ((states == null) || (states.isEmpty())) { 73 return null; 74 } 75 76 return (State) states.get(stateId); 77 } 78 79 public final String [] getStateIds() { 80 if ((states == null) || (states.isEmpty())) { 81 return null; 82 } 83 84 final Set stateIds = states.keySet(); 85 return (String []) stateIds.toArray(new String [stateIds.size()]); 86 } 87 88 103 public void removeState(final String stateId) { 104 if (stateId == null) { 105 throw new NullPointerException ("Cannot remove a null state"); } 107 108 final State state = (State) states.get(stateId); 109 if (state != null) { 110 state.removeListener(this); 111 if (states != null) { 112 states.remove(state); 113 if (states.isEmpty()) { 114 states = null; 115 } 116 } 117 } 118 } 119 } 120 | Popular Tags |