1 15 package org.apache.tapestry.form; 16 17 import java.util.ArrayList ; 18 import java.util.Collections ; 19 import java.util.HashMap ; 20 import java.util.HashSet ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.Set ; 24 25 import org.apache.tapestry.Tapestry; 26 27 74 75 public class ListEditMap 76 { 77 private Map _map = new HashMap (); 78 79 private List _keys = new ArrayList (); 80 81 private Set _deletedKeys; 82 83 private Object _currentKey; 84 85 90 91 public void add(Object key, Object value) 92 { 93 _currentKey = key; 94 95 _keys.add(_currentKey); 96 _map.put(_currentKey, value); 97 } 98 99 103 104 public List getKeys() 105 { 106 return _keys; 107 } 108 109 113 114 public void setKey(Object key) 115 { 116 _currentKey = key; 117 } 118 119 122 123 public Object getKey() 124 { 125 return _currentKey; 126 } 127 128 133 134 public Object getValue() 135 { 136 return _map.get(_currentKey); 137 } 138 139 142 143 public boolean isDeleted() 144 { 145 return checkSet(_deletedKeys); 146 } 147 148 152 153 protected boolean checkSet(Set set) 154 { 155 if (set == null) 156 return false; 157 158 return set.contains(_currentKey); 159 } 160 161 164 165 public void setDeleted(boolean value) 166 { 167 _deletedKeys = updateSet(_deletedKeys, value); 168 } 169 170 175 176 protected Set updateSet(Set set, boolean value) 177 { 178 if (value) 179 { 180 if (set == null) 181 set = new HashSet (); 182 183 set.add(_currentKey); 184 } 185 else 186 { 187 if (set != null) 188 set.remove(_currentKey); 189 } 190 191 return set; 192 } 193 194 198 199 public List getDeletedKeys() 200 { 201 return convertSetToList(_deletedKeys); 202 } 203 204 212 213 public void purgeDeletedKeys() 214 { 215 if (_deletedKeys == null) 216 return; 217 218 _map.keySet().removeAll(_deletedKeys); 219 _keys.removeAll(_deletedKeys); 220 221 _deletedKeys = null; 222 } 223 224 231 232 protected List convertSetToList(Set set) 233 { 234 if (Tapestry.isEmpty(set)) 235 return Collections.EMPTY_LIST; 236 237 return new ArrayList (set); 238 } 239 240 244 245 public List getAllValues() 246 { 247 int count = _keys.size(); 248 List result = new ArrayList (count); 249 250 for (int i = 0; i < count; i++) 251 { 252 Object key = _keys.get(i); 253 Object value = _map.get(key); 254 255 result.add(value); 256 } 257 258 return result; 259 } 260 261 265 266 public List getValues() 267 { 268 int deletedCount = Tapestry.size(_deletedKeys); 269 270 if (deletedCount == 0) 271 return getAllValues(); 272 273 int count = _keys.size(); 274 275 List result = new ArrayList (count - deletedCount); 276 277 for (int i = 0; i < count; i++) 278 { 279 Object key = _keys.get(i); 280 281 if (_deletedKeys.contains(key)) 282 continue; 283 284 Object value = _map.get(key); 285 result.add(value); 286 } 287 288 return result; 289 } 290 291 } 292 | Popular Tags |