1 13 package com.tonbeller.wcf.list; 14 15 import java.util.ArrayList ; 16 import java.util.Collections ; 17 import java.util.Comparator ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 21 import org.apache.log4j.Logger; 22 import org.w3c.dom.Element ; 23 24 import com.tonbeller.wcf.component.FormListener; 25 import com.tonbeller.wcf.controller.RequestContext; 26 import com.tonbeller.wcf.convert.ConvertException; 27 import com.tonbeller.wcf.ui.ListItem; 28 import com.tonbeller.wcf.utils.DomUtils; 29 import com.tonbeller.wcf.utils.SoftException; 30 31 77 public class MappedListBox implements FormListener { 78 List entries = new ArrayList (); 79 Element listBox; 80 private static Logger logger = Logger.getLogger(MappedListBox.class); 81 82 public MappedListBox(Element listBox) { 83 this.listBox = listBox; 84 } 85 86 public void revert(RequestContext context) { 87 DomUtils.removeChildElements(listBox); 88 for (Iterator iter = entries.iterator(); iter.hasNext();) { 89 Item item = (Item) iter.next(); 90 Element elem = ListItem.addListItem(listBox); 91 item.setElement(elem); 92 ListItem.setId(elem, item.getId()); 93 ListItem.setLabel(elem, item.getLabel()); 94 if (item.isSelected()) 95 ListItem.setSelected(elem, true); 96 } 97 } 98 99 public boolean validate(RequestContext context) { 100 try { 101 context.getConverter().validate(context.getParameters(), context.getFileParameters(), listBox, null); 102 for (Iterator it = entries.iterator(); it.hasNext();) { 103 Item item = (Item) it.next(); 104 item.setSelected(ListItem.isSelected(item.getElement())); 105 } 106 } catch (ConvertException e) { 107 logger.error("exception caught", e); 108 throw new SoftException(e); 109 } 110 return true; 111 } 112 113 public Item getSingleSelection() { 114 for (Iterator it = entries.iterator(); it.hasNext();) { 115 Item item = (Item) it.next(); 116 if (item.isSelected()) 117 return item; 118 } 119 return null; 120 } 121 122 public void sortByLabel() { 123 Collections.sort(entries, new Comparator (){ 124 public int compare(Object o1, Object o2) { 125 String l1 = ((Item)o1).getLabel(); 126 String l2 = ((Item)o2).getLabel(); 127 return l1.compareTo(l2); 128 } 129 }); 130 } 131 132 135 public static class Item { 136 String label; 137 String id; 138 Object value; 139 boolean selected; 140 Element element; 141 142 public Item(String label, Object value) { 143 this.label = label; 144 this.value = value; 145 this.id = DomUtils.randomId(); 146 } 147 148 152 public String getLabel() { 153 return label; 154 } 155 156 160 public Object getValue() { 161 return value; 162 } 163 164 168 public void setLabel(String label) { 169 this.label = label; 170 } 171 172 176 public void setValue(Object value) { 177 this.value = value; 178 } 179 183 public String getId() { 184 return id; 185 } 186 187 191 public void setId(String id) { 192 this.id = id; 193 } 194 195 199 public boolean isSelected() { 200 return selected; 201 } 202 203 207 public void setSelected(boolean selected) { 208 this.selected = selected; 209 } 210 211 215 public Element getElement() { 216 return element; 217 } 218 219 223 public void setElement(Element element) { 224 this.element = element; 225 } 226 227 } 228 229 233 public List getEntries() { 234 return entries; 235 } 236 237 241 public void setEntries(List entries) { 242 this.entries = entries; 243 } 244 245 } 246 | Popular Tags |