1 20 package org.enhydra.barracuda.contrib.dbroggisch.repopulation; 21 22 import java.util.List ; 23 import java.util.ArrayList ; 24 import org.enhydra.barracuda.core.forms.DefaultFormElement; 25 import org.enhydra.barracuda.core.comp.ListModel; 26 import org.enhydra.barracuda.core.comp.DefaultListModel; 27 import org.apache.log4j.Logger; 28 import org.enhydra.barracuda.core.comp.ListSelectionModel; 29 import org.enhydra.barracuda.core.forms.FormType; 30 import org.enhydra.barracuda.core.forms.FormValidator; 31 import org.enhydra.barracuda.core.comp.ItemMap; 32 import org.enhydra.barracuda.core.comp.ViewContext; 33 import org.enhydra.barracuda.core.comp.BSelect; 34 35 42 public class SelectFormElement extends DefaultFormElement implements RepopulationElement { 43 private static final Logger logger = Logger.getLogger(SelectFormElement.class.getName()); 44 ListModel lm; 45 ListSelectionModel lsm; 46 int selectionType = -1; 47 48 53 public void setListModel(ListModel lm) { 54 this.lm = lm; 55 } 56 57 62 public ListModel getListModel() { 63 return lm; 64 } 65 66 72 public void setListSelectionModel(ListSelectionModel lsm) { 73 this.lsm = lsm; 74 } 75 76 77 82 public ListSelectionModel getListSelectionModel() { 83 if(logger.isDebugEnabled()) { 84 logger.debug("Name: " + getName()); 85 logger.debug("allowMultiples: " + ( 86 allowMultiples() ? "yes" : "no")); 87 if(getVals() != null) { 88 logger.debug("type(Vals): " + getVals().getClass().getName()); 89 logger.debug("Vals: " + getVals()); 90 } 91 } 92 if(lsm == null) { 93 int selType = selectionType; 94 if(selType == -1) { 95 if(allowMultiples()) { 96 selType = ListSelectionModel.MULTIPLE_INTERVAL_SELECTION; 97 } else { 98 selType = ListSelectionModel.SINGLE_SELECTION; 99 } 100 } 101 List selected; 102 if(getOrigVal() != null) { 103 if(getOrigVal() instanceof List ) { 104 selected = (List )getOrigVal(); 105 } else { 106 selected = new ArrayList (); 107 selected.add(getOrigVal()); 108 } 109 } else { 110 selected = new ArrayList (); 112 } 113 if(lm instanceof DefaultListModel) { 114 return calcListSelectionModel(((DefaultListModel)lm).subList(0, lm.getSize()), selected, selType); 115 } 116 else { 117 List values = new ArrayList (lm.getSize()); 118 for(int i = 0; i < lm.getSize(); i++) { 119 values.add(lm.getItemAt(i)); 120 } 121 return calcListSelectionModel(values, selected, selType); 122 } 123 } 124 return lsm; 125 } 126 127 128 138 public static int calcSelectionMode(List vals, List sel) { 139 ListSelectionModel lsm = calcListSelectionModel(vals, sel, ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); 140 return calcSelectionMode(lsm); 141 } 142 143 151 public static int calcSelectionMode(ListSelectionModel lsm) { 152 int selMode = -1; 153 int min = lsm.getMinSelectionIndex(); 154 if(min != -1) { 155 int max = lsm.getMaxSelectionIndex(); 156 if(min != max) { 157 boolean singleInterval = true; 158 for(int i = min; i <= max; i++) { 159 singleInterval = singleInterval && lsm.isSelectedIndex(i); 160 } 161 if(singleInterval) { 162 selMode = ListSelectionModel.SINGLE_INTERVAL_SELECTION; 163 } else { 164 selMode = ListSelectionModel.MULTIPLE_INTERVAL_SELECTION; 165 } 166 } else { 167 selMode = ListSelectionModel.SINGLE_SELECTION; 168 } 169 } 170 return selMode; 171 } 172 173 183 public static ListSelectionModel calcListSelectionModel(List vals, List sel, int selType) { 184 if(logger.isDebugEnabled()) { 185 logger.debug("Calculating"); 186 } 187 188 ListSelectionModel lm = new org.enhydra.barracuda.core.comp.DefaultListSelectionModel(); 189 lm.setSelectionMode(selType); 190 if(vals == null || sel == null || sel.size() == 0) { 191 logger.debug("No selection"); 192 return lm; 193 } 194 for(int i = 0;i < vals.size(); i++) { 195 Object key; 196 Object rawKey, item = vals.get(i); 197 if (logger.isDebugEnabled() && item != null) { 198 logger.debug("Itemtype:" + item.getClass()); 199 } 201 if(item instanceof ItemMap) { 202 key = ((ItemMap)item).getKey(); 203 } else { 204 key = item; 205 } 206 if (logger.isDebugEnabled() && key != null) { 207 logger.debug("ItemKey:" + key); 208 logger.debug("ItemKeyType:" + key.getClass()); 209 } 211 if(sel.contains(key) || sel.contains(key.toString())) { 212 logger.debug("Added interval " + i + " to " + i); 213 lm.addSelectionInterval(i, i); 214 } 215 } 216 if(logger.isDebugEnabled()) { 217 for(int i = 0;i < vals.size(); i++) { 218 if(lm.isSelectedIndex(i)) 219 logger.debug("Index " + i + " is selected"); 220 } 221 } 222 return lm; 223 } 224 225 235 public SelectFormElement(java.lang.String ikey, 236 java.lang.String iname, 237 FormType itype, 238 java.lang.Object idefaultVal, 239 FormValidator ivalidator, 240 boolean iallowMultiples) { 241 super(ikey, iname, itype, idefaultVal, ivalidator, iallowMultiples); 242 243 } 244 245 256 public SelectFormElement(ListModel lm, java.lang.String ikey, 257 java.lang.String iname, 258 FormType itype, 259 java.lang.Object idefaultVal, 260 FormValidator ivalidator, 261 boolean iallowMultiples) { 262 super(ikey, iname, itype, idefaultVal, ivalidator, iallowMultiples); 263 this.lm = lm; 264 } 265 266 276 public SelectFormElement(ListModel lm, 277 java.lang.String ikey, 278 FormType itype, 279 java.lang.Object idefaultVal, 280 FormValidator ivalidator, 281 boolean iallowMultiples) { 282 super(ikey, itype, idefaultVal, ivalidator, iallowMultiples); 283 this.lm = lm; 284 } 285 286 298 public SelectFormElement(ListModel lm, ListSelectionModel lsm, java.lang.String ikey, 299 java.lang.String iname, 300 FormType itype, 301 java.lang.Object idefaultVal, 302 FormValidator ivalidator, 303 boolean iallowMultiples) { 304 super(ikey, iname, itype, idefaultVal, ivalidator, iallowMultiples); 305 this.lm = lm; 306 this.lsm = lsm; 307 } 308 316 public SelectFormElement(java.lang.String ikey, 317 FormType itype, 318 java.lang.Object idefaultVal, 319 FormValidator ivalidator) { 320 super(ikey, itype, idefaultVal, ivalidator); 321 } 322 323 333 public SelectFormElement(ListModel lm, ListSelectionModel lsm, java.lang.String ikey, 334 FormType itype, 335 java.lang.Object idefaultVal, 336 FormValidator ivalidator) { 337 super(ikey, itype, idefaultVal, ivalidator); 338 this.lm = lm; 339 this.lsm = lsm; 340 } 341 342 348 public Object render(ViewContext context) { 349 BSelect bs = new BSelect(getListModel()); 350 bs.setName(getName()); 351 bs.setSelectionModel(getListSelectionModel()); 352 return bs; 353 } 354 } 355 | Popular Tags |