1 26 27 package net.groboclown.util.states.v1; 28 29 30 31 37 public class Stateful 38 { 39 42 43 46 47 50 51 54 55 58 59 62 private IStatefulListener listener; 63 private StateCategory category; 64 private Object [] actions = null; 65 private int currentState = -1; 66 private Object currentAction = null; 67 68 69 72 73 76 public Stateful( IStatefulListener listener ) 77 { 78 if (listener == null) 79 { 80 throw new IllegalArgumentException ("no null args" ); 81 } 82 this.listener = listener; 83 } 84 85 86 87 88 89 92 93 96 public void addStateAction( State s, Object action ) 97 { 98 if (this.category == null) 99 { 100 throw new IllegalStateException ( 101 "Stateful never intialized - you must add it to a set "+ 102 "before adding State Actions" ); 103 } 104 if (!this.category.isOfCategory( s )) 105 { 106 throw new IllegalStateException ( 107 "State object not of state category"); 108 } 109 this.actions[ s.getIndex() ] = action; 110 111 if (s.getIndex() == this.currentState && 112 action != this.currentAction) 113 { 114 this.listener.setAction( action ); 116 this.currentAction = action; 117 } 118 } 119 120 123 public void removeStateAction( State s ) 124 { 125 addStateAction( s, null ); 126 } 127 128 133 public void setState( State s ) 134 { 135 if (this.category == null) 136 { 137 throw new IllegalStateException ( 138 "Stateful never intialized - you must add it to a set "+ 139 "before adding State Actions" ); 140 } 141 if (!this.category.isOfCategory( s )) 142 { 143 throw new IllegalStateException ( 144 "State object not of state category"); 145 } 146 147 152 int si = s.getIndex(); 154 if (si == this.currentState) 155 { 156 return; 158 } 159 this.currentState = si; 160 161 Object a = actions[ si ]; 164 if (this.currentAction == a) 165 { 166 return; 168 } 169 this.currentAction = a; 170 171 this.listener.setAction( actions[ s.getIndex() ] ); 173 } 174 175 178 179 protected void initialize( StateCategory s ) 180 { 181 if (this.actions != null) 182 { 183 throw new IllegalStateException ("Stateful already initialized"); 184 } 185 this.actions = new Object [ s.getMaximumStateCount() ]; 186 this.category = s; 187 } 188 189 192 193 } 194 195 | Popular Tags |