1 20 package org.enhydra.barracuda.core.comp.renderer.html; 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.*; 29 import org.enhydra.barracuda.core.comp.renderer.*; 30 import org.enhydra.barracuda.core.util.dom.*; 31 import org.enhydra.barracuda.plankton.*; 32 33 36 public class HTMLSelectRenderer extends HTMLListRenderer { 37 38 protected static final Logger logger = Logger.getLogger(HTMLSelectRenderer.class.getName()); 39 40 protected BSelect bscomp = null; 41 protected ListModel lm = null; 42 protected ListSelectionModel lsm = null; 43 44 55 public Node createDefaultNode(Document doc, BComponent comp, ViewContext vc) throws UnsupportedFormatException { Node templateNode = vc.getTemplateNode(); 59 Node defaultNode = null; 60 if (templateNode instanceof HTMLSelectElement) { 65 defaultNode = templateNode.cloneNode(true); 66 } else { 67 Element selectEl = (HTMLSelectElement) doc.createElement("SELECT"); 68 defaultNode = selectEl; 69 } 70 if (logger.isInfoEnabled()) logger.info("Creating default node:"+defaultNode); 71 return defaultNode; 72 } 73 74 77 public void renderComponent(BComponent comp, View view, ViewContext vc) throws RenderException { 78 if (!(comp instanceof BSelect)) throw new NoSuitableRendererException("This renderer can only render BSelect components; comp is of type:"+comp.getClass().getName()); 80 bscomp = (BSelect) comp; 81 lm = bscomp.getModel(); 82 lsm = bscomp.getSelectionModel(); 83 Node node = view.getNode(); 84 85 if (logger.isDebugEnabled()) showNodeInterfaces(view, logger); 87 88 super.renderComponent(comp, view, vc); 90 91 if (node instanceof HTMLSelectElement) { 95 if (logger.isInfoEnabled()) logger.info("Rendering based on HTMLSelectElement interface..."); 96 HTMLSelectElement el = (HTMLSelectElement) node; 97 98 if (bscomp.getViewSize()!=null) { 100 el.setSize(bscomp.getViewSize().intValue()); 101 } 102 103 el.setMultiple(lsm.getSelectionMode()!=ListSelectionModel.SINGLE_SELECTION); 105 106 HTMLCollection options = el.getOptions(); 110 for (int i=0, max=options.getLength(); i<max; i++) { 111 HTMLOptionElement opt = (HTMLOptionElement) options.item(i); 112 113 115 if (lsm.isSelectedIndex(i)) opt.setAttribute("selected", "selected"); 129 else opt.removeAttribute("selected"); 130 131 Object item = lm.getItemAt(i); 135 if (item==null) continue; 136 if (item instanceof ItemMap) { 137 opt.setValue(((ItemMap) item).getKey().toString()); 138 } else { 139 opt.setValue(item.toString()); 140 } 141 } 142 143 } else { 144 String errmsg = "Node does not implement HTMLSelectElement and cannot be rendered: "+node; 145 logger.warn(errmsg); 146 throw new NoSuitableRendererException(errmsg); 147 } 148 } 149 } | Popular Tags |