1 18 19 package org.apache.struts.action; 20 21 import java.io.Serializable ; 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.Comparator ; 25 import java.util.Iterator ; 26 import java.util.HashMap ; 27 import java.util.List ; 28 29 46 public class ActionMessages implements Serializable { 47 48 49 52 private static final Comparator actionItemComparator = new Comparator () { 53 public int compare(Object o1, Object o2) { 54 return ((ActionMessageItem) o1).getOrder() 55 - ((ActionMessageItem) o2).getOrder(); 56 } 57 }; 58 59 60 62 63 67 public static final String GLOBAL_MESSAGE = 68 "org.apache.struts.action.GLOBAL_MESSAGE"; 69 70 71 73 74 82 protected boolean accessed = false; 83 84 85 89 protected HashMap messages = new HashMap (); 90 91 92 96 protected int iCount = 0; 97 98 99 101 102 105 public ActionMessages() { 106 107 super(); 108 109 } 110 111 112 120 public ActionMessages(ActionMessages messages) { 121 super(); 122 this.add(messages); 123 } 124 125 126 134 public void add(String property, ActionMessage message) { 135 136 ActionMessageItem item = (ActionMessageItem) messages.get(property); 137 List list = null; 138 139 if (item == null) { 140 list = new ArrayList (); 141 item = new ActionMessageItem(list, iCount++, property); 142 143 messages.put(property, item); 144 } else { 145 list = item.getList(); 146 } 147 148 list.add(message); 149 150 } 151 152 153 165 public void add(ActionMessages messages) { 166 167 if (messages == null) { 168 return; 169 } 170 171 Iterator props = messages.properties(); 173 while (props.hasNext()) { 174 String property = (String ) props.next(); 175 176 Iterator msgs = messages.get(property); 178 while (msgs.hasNext()) { 179 ActionMessage msg = (ActionMessage) msgs.next(); 180 this.add(property, msg); 181 } 182 } 183 } 184 185 186 189 public void clear() { 190 191 messages.clear(); 192 193 } 194 195 196 202 public boolean isEmpty(){ 203 204 return (messages.isEmpty()); 205 206 207 } 208 209 210 215 public Iterator get() { 216 217 this.accessed = true; 218 219 if (messages.isEmpty()) { 220 return Collections.EMPTY_LIST.iterator(); 221 } 222 223 ArrayList results = new ArrayList (); 224 ArrayList actionItems = new ArrayList (); 225 226 for (Iterator i = messages.values().iterator(); i.hasNext();) { 227 actionItems.add(i.next()); 228 } 229 230 Collections.sort(actionItems, actionItemComparator); 233 234 for (Iterator i = actionItems.iterator(); i.hasNext();) { 235 ActionMessageItem ami = (ActionMessageItem) i.next(); 236 237 for (Iterator messages = ami.getList().iterator(); messages.hasNext();) { 238 results.add(messages.next()); 239 } 240 } 241 242 return results.iterator(); 243 } 244 245 246 252 public Iterator get(String property) { 253 254 this.accessed = true; 255 256 ActionMessageItem item = (ActionMessageItem) messages.get(property); 257 258 if (item == null) { 259 return (Collections.EMPTY_LIST.iterator()); 260 } else { 261 return (item.getList().iterator()); 262 } 263 264 } 265 266 267 275 public boolean isAccessed() { 276 277 return this.accessed; 278 279 } 280 281 282 289 public Iterator properties() { 290 291 if (messages.isEmpty()) { 292 return Collections.EMPTY_LIST.iterator(); 293 } 294 295 ArrayList results = new ArrayList (); 296 ArrayList actionItems = new ArrayList (); 297 298 for (Iterator i = messages.values().iterator(); i.hasNext();) { 299 actionItems.add(i.next()); 300 } 301 302 Collections.sort(actionItems, actionItemComparator); 305 306 for (Iterator i = actionItems.iterator(); i.hasNext();) { 307 ActionMessageItem ami = (ActionMessageItem) i.next(); 308 results.add(ami.getProperty()); 309 } 310 311 return results.iterator(); 312 313 } 314 315 316 322 public int size() { 323 324 int total = 0; 325 326 for (Iterator i = messages.values().iterator(); i.hasNext();) { 327 ActionMessageItem ami = (ActionMessageItem) i.next(); 328 total += ami.getList().size(); 329 } 330 331 return (total); 332 333 } 334 335 336 341 public int size(String property) { 342 343 ActionMessageItem item = (ActionMessageItem) messages.get(property); 344 345 return (item == null) ? 0 : item.getList().size(); 346 } 347 348 349 354 public String toString() { 355 return this.messages.toString(); 356 } 357 358 359 363 protected class ActionMessageItem implements Serializable { 364 365 366 369 protected List list = null; 370 371 372 375 protected int iOrder = 0; 376 377 378 381 protected String property = null; 382 383 384 public ActionMessageItem(List list, int iOrder, String property) { 385 this.list = list; 386 this.iOrder = iOrder; 387 this.property = property; 388 } 389 390 391 public List getList() { 392 return list; 393 } 394 395 396 public void setList(List list) { 397 this.list = list; 398 } 399 400 401 public int getOrder() { 402 return iOrder; 403 } 404 405 406 public void setOrder(int iOrder) { 407 this.iOrder = iOrder; 408 } 409 410 411 public String getProperty() { 412 return property; 413 } 414 415 416 public void setProperty(String property) { 417 this.property = property; 418 } 419 420 421 public String toString() { 422 return this.list.toString(); 423 } 424 425 } 426 427 } 428 | Popular Tags |