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 HTMLTableRenderer extends HTMLComponentRenderer { 37 38 protected static final Logger logger = Logger.getLogger(HTMLTableRenderer.class.getName()); 39 40 protected EnabledHelper eh = new EnabledHelper(); 41 42 protected BTable btable = null; 43 44 57 public Node createDefaultNode(Document doc, BComponent comp, ViewContext vc) throws UnsupportedFormatException { Node templateNode = vc.getTemplateNode(); 63 Node defaultNode = templateNode.cloneNode(true); 64 if (logger.isInfoEnabled()) logger.info("Creating default node:"+defaultNode); 65 comp.setView(new DefaultTableView(defaultNode)); 66 return defaultNode; 67 } 69 70 73 public void renderComponent(BComponent comp, View view, ViewContext vc) throws RenderException { 74 if (!(comp instanceof BTable)) throw new NoSuitableRendererException("This renderer can only render BTable components; comp is of type:"+comp.getClass().getName()); 76 btable = (BTable) comp; 77 TableModel headerModel = btable.getHeaderModel(); 78 TableModel model = btable.getModel(); 79 TableModel footerModel = btable.getFooterModel(); 80 BText captionComponent = btable.getCaption(); Node node = view.getNode(); 82 83 if (!(view instanceof TableView)) throw new InvalidViewException ("Component is bound to an unsupported View:"+view); 85 TableView tview = (TableView) view; 86 87 if (logger.isDebugEnabled()) showNodeInterfaces(view, logger); 89 90 super.renderComponent(comp, view, vc); 92 93 94 if (node instanceof HTMLElement) { 102 103 if (node instanceof HTMLTableElement) { 104 if (logger.isInfoEnabled()) logger.info("Rendering table component..."); 105 106 Element bodyNode = tview.getBodyElement(); 108 Element headerNode = tview.getHeaderElement(); 109 Element footerNode = tview.getFooterElement(); 110 111 Element captionNode = tview.getCaptionElement(); 113 if (captionNode!=null && captionComponent!=null){ 115 if (logger.isDebugEnabled()) logger.debug("Creating caption for table..."); 116 captionComponent.setView(new DefaultView(captionNode)); 117 btable.addChild(captionComponent); 118 } 119 121 if (logger.isDebugEnabled()) logger.debug("Creating table component for body..."); 127 DefaultListModel rowBodyModel = new DefaultListModel(); 128 BList rowBodyList = new BList(rowBodyModel); 130 rowBodyList.setView(new DefaultView(bodyNode)); 131 btable.addStepChild(rowBodyList); 135 136 DefaultListModel rowHeaderModel = rowBodyModel; 139 BList rowHeaderList = rowBodyList; 140 if (headerNode!=null && headerModel!=null) { 141 if (logger.isDebugEnabled()) logger.debug("Creating table component for header..."); 142 rowHeaderModel = new DefaultListModel(); 143 rowHeaderList = new BList(rowHeaderModel); 145 rowHeaderList.setView(new DefaultView(headerNode)); 146 btable.addStepChild(rowHeaderList); 149 } 150 151 DefaultListModel rowFooterModel = rowBodyModel; 153 BList rowFooterList = rowBodyList; 154 if (footerNode!=null && footerModel!=null) { 155 if (logger.isDebugEnabled()) logger.debug("Creating table component for footer..."); 156 rowFooterModel = new DefaultListModel(); 157 rowFooterList = new BList(rowFooterModel); 159 rowFooterList.setView(new DefaultView(footerNode)); 160 btable.addStepChild(rowFooterList); 163 } 164 165 if (headerModel!=null) { 167 if (logger.isDebugEnabled()) logger.debug("Rendering header..."); 168 populateTableFromModel(node, tview, vc, headerModel, rowHeaderModel, rowHeaderList); 169 } 170 if (model!=null) { 171 if (logger.isDebugEnabled()) logger.debug("Rendering body..."); 172 populateTableFromModel(node, tview, vc, model, rowBodyModel, rowBodyList); 173 } 174 if (footerModel!=null) { 175 if (logger.isDebugEnabled()) logger.debug("Rendering footer..."); 176 populateTableFromModel(node, tview, vc, footerModel, rowFooterModel, rowFooterList); 177 } 178 } else { 179 if (logger.isDebugEnabled()) logger.debug("Interface "+node.getClass().getName()+" not a supported View for a BTable component..."); 180 throw new InvalidNodeException ("View is bound to an unsupported Node:"+node); 181 } 182 183 } else { 184 String errmsg = "Node does not implement HTMLElement and cannot be rendered: "+node; 185 logger.warn(errmsg); 186 throw new NoSuitableRendererException(errmsg); 187 } 188 189 eh.setEnabled(node, comp.isEnabled()); 191 } 192 193 protected void populateTableFromModel(Node node, TableView view, ViewContext vc, TableModel curModel, DefaultListModel rowModel, BList rowList) throws RenderException { 194 195 curModel.setViewContext(vc); 197 curModel.resetModel(); 198 Node origTemplateNode = vc.getTemplateNode(); vc.putState(ViewContext.TEMPLATE_NODE, node); 201 202 try { 204 ElementFactory ef = view.getElementFactory(); 205 for (int i=0,max=curModel.getRowCount(); i<max; i++) { 206 207 Element rowNode = node.getOwnerDocument().createElement("TR"); 209 rowModel.add(rowNode); 210 211 DefaultListModel colModel = new DefaultListModel(); 214 DefaultView colView = new DefaultView(rowNode); 215 BList colList = new BList(colModel); 217 colList.setView(colView); 218 rowList.addChild(colList); 219 220 for (int j=0,jmax=curModel.getColumnCount(); j<jmax; j++) { 222 Object item = curModel.getItemAt(i, j); 223 if (item!=null) colModel.add(item); 224 } 225 } 226 } finally { 227 vc.putState(ViewContext.TEMPLATE_NODE, origTemplateNode); curModel.setViewContext(null); 230 } 231 } 232 } | Popular Tags |