1 20 package org.enhydra.barracuda.core.comp; 21 22 import java.util.*; 23 24 import org.apache.log4j.*; 25 import org.w3c.dom.*; 26 import org.w3c.dom.html.*; 27 28 import org.enhydra.barracuda.core.comp.model.*; 29 import org.enhydra.barracuda.core.comp.renderer.*; 30 import org.enhydra.barracuda.core.comp.renderer.html.*; 31 import org.enhydra.barracuda.core.comp.renderer.xml.*; 32 import org.enhydra.barracuda.core.event.*; 33 import org.enhydra.barracuda.core.view.*; 34 import org.enhydra.barracuda.core.util.dom.DOMUtil; 35 import org.enhydra.barracuda.plankton.*; 36 37 50 public class BSelect extends BList { 51 52 protected static final Logger logger = Logger.getLogger(BSelect.class.getName()); 54 55 protected ListSelectionModel selectionModel = null; 57 protected LocalModelListener scallback = null; 58 protected List listeners = null; 59 protected Integer viewSize = null; 60 protected boolean disableBackButton = false; 61 private ListenerFactory lnkListenerFactory; 62 protected BAction baction = null; 64 68 public BSelect() {} 69 70 76 public BSelect(ListModel imodel) { 77 this(imodel, null, null); 78 } 79 80 95 BSelect(ListModel imodel, View iview, ViewContext idvc) { 96 if (imodel!=null) setModel(imodel); 97 if (iview!=null) this.addView(iview); 98 if (idvc!=null) setDefaultViewContext(idvc); 99 } 100 101 102 103 104 108 static { 109 HTMLRendererFactory rfHTML = new HTMLRendererFactory(); 110 installRendererFactory(rfHTML, BSelect.class, HTMLElement.class); 111 installRendererFactory(rfHTML, BSelect.class, HTMLDocument.class); 112 113 } 114 115 118 static class HTMLRendererFactory implements RendererFactory { 119 public Renderer getInstance() {return new HTMLSelectRenderer();} 120 } 121 122 123 124 125 135 public void setSelectionModel(ListSelectionModel imodel) { 136 137 if (selectionModel!=null && scallback!=null) { 139 selectionModel.removeModelListener(scallback); 140 } 141 142 selectionModel = imodel; 144 145 if (selectionModel!=null) { 147 if (scallback==null) scallback = new LocalModelListener(); 148 selectionModel.addModelListener(scallback); 149 } 150 invalidate(); 151 } 152 153 158 public ListSelectionModel getSelectionModel() { 159 if (selectionModel==null) setSelectionModel(new DefaultListSelectionModel()); 160 return selectionModel; 161 } 162 163 171 public int getSelectedIndex() { 172 if (model==null || model.getSize()<1) return -1; 173 if (selectionModel==null || selectionModel.isSelectionEmpty()) return -1; 174 for (int i=0,max=model.getSize(); i<max; i++) { 175 if (selectionModel.isSelectedIndex(i)) return i; 176 } 177 return -1; 178 } 179 180 187 public void setSelectedIndex(int i) { 188 if (selectionModel==null) getSelectionModel(); 189 selectionModel.setSelectionInterval(i,i); 190 invalidate(); 191 } 192 193 199 public int[] getSelectedIndexes() { 200 if (model==null || model.getSize()<1) return null; 201 if (selectionModel==null || selectionModel.isSelectionEmpty()) return null; 202 int idx[] = new int[model.getSize()]; 203 int cntr = -1; 204 for (int i=0,max=model.getSize(); i<max; i++) { 205 if (selectionModel.isSelectedIndex(i)) idx[++cntr] = i; 206 } 207 int idx2[] = new int[cntr]; 208 System.arraycopy(idx, 0, idx2, 0, idx2.length); 209 return idx2; 210 } 211 212 222 public void setViewSize(Integer iviewSize) { 223 if (iviewSize==null || iviewSize.intValue()<1) return; 224 viewSize = iviewSize; 225 invalidate(); 226 } 227 228 236 public Integer getViewSize() { 237 return viewSize; 238 } 239 240 247 252 258 263 264 270 public void setAction(BAction ibaction) { 271 baction = ibaction; 272 } 273 274 280 public BAction getAction() { 281 return baction; 282 } 283 284 292 public void addEventListener(ListenerFactory lf, boolean idisableBackButton) { 293 if (lf==null) return; 294 disableBackButton = idisableBackButton; 295 if (listeners==null) listeners = new ArrayList(5); 296 listeners.add(lf); 297 invalidate(); 298 } 299 300 305 public void addEventListener(ListenerFactory lf) { 306 if (lf==null) return; 307 if (listeners==null) listeners = new ArrayList(5); 308 listeners.add(lf); 309 invalidate(); 310 } 311 312 317 public void removeEventListener(ListenerFactory lf) { 318 if (lf==null) return; 319 if (listeners==null) return; 320 listeners.remove(lf); 321 invalidate(); 322 } 323 324 329 protected void preRender(ViewContext vc, int depth) { 330 331 if (baction!=null) this.addStepChild(baction, true); 334 if (listeners!=null) { 337 Iterator it = listeners.iterator(); 339 while (it.hasNext()) { 340 ListenerFactory lf = (ListenerFactory) it.next(); 341 342 373 BAction baComp = new BAction(); 374 baComp.setDisableBackButton(disableBackButton); 375 baComp.addEventListener(lf); 376 this.addStepChild(baComp, true); 377 } 379 } 380 } 381 382 390 406 407 408 409 410 415 public void destroyCycle() { 416 super.destroyCycle(); 418 419 setSelectionModel(null); 424 } 425 426 427 428 429 438 class LocalModelListener implements ModelListener { 439 public void modelChanged(Model m) { 441 invalidate(); 442 } 443 } 444 445 } | Popular Tags |