KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > comp > renderer > html > HTMLTableRenderer


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: HTMLTableRenderer.java,v 1.18 2004/02/01 05:16:27 christianc Exp $
19  */

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 /**
34  * This class handles the default rendering of a table in an HTML view.
35  */

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     /**
45      * The purpose of this method is for a renderer to provide
46      * a default node (if none exists). This component currently
47      * does not provide a default, so it throws an UnsupportedFormatException
48      * instead.
49      *
50      * @param doc the master Document which can be used to create elements
51      * from scratch
52      * @param comp the component that we're dealing with for the current request
53      * @param vc the view context for the current request
54      * @return a default node (created from scratch)
55      * @throws UnsupportedFormatException if the renderer has no default node
56      */

57 //csc_110501.1 public Node createDefaultNode(Document doc, ViewContext vc) throws UnsupportedFormatException;
58
public Node createDefaultNode(Document doc, BComponent comp, ViewContext vc) throws UnsupportedFormatException { //csc_110501.1
59
//csc_031901.1_start
60
// throw new UnsupportedFormatException("Cannot create default node");
61
//ask the renderer to create the default Node
62
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 //csc_031901.1_end
68
}
69     
70     /**
71      *
72      */

73     public void renderComponent(BComponent comp, View view, ViewContext vc) throws RenderException {
74         //make sure the component is a text component
75
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(); //ndc_101202.1
81
Node node = view.getNode();
82
83         //make sure the View implements TableView
84
if (!(view instanceof TableView)) throw new InvalidViewException ("Component is bound to an unsupported View:"+view);
85         TableView tview = (TableView) view;
86
87         //show what we're doing
88
if (logger.isDebugEnabled()) showNodeInterfaces(view, logger);
89
90         //first, allow the parent class to do anything it needs to
91
super.renderComponent(comp, view, vc);
92
93
94         //HTMLElement Interface
95
//---------------------
96
//Supported Elements:
97
//..HTMLTableElement
98
//
99
//Unsupported Elements:
100
//..everything else
101
if (node instanceof HTMLElement) {
102
103             if (node instanceof HTMLTableElement) {
104                 if (logger.isInfoEnabled()) logger.info("Rendering table component...");
105
106                 //get the nodes for the header, body, and footer sections
107
Element bodyNode = tview.getBodyElement();
108                 Element headerNode = tview.getHeaderElement();
109                 Element footerNode = tview.getFooterElement();
110
111                 //ndc_101202.1_start
112
Element captionNode = tview.getCaptionElement();
113                 //now we will create the caption, and add it to the comp
114
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                 //ndc_101202.1_end
120

121                 //now create a list component and bind it to the data section node.
122
//(be sure to add the comp as a temporary child--by doing this, we
123
//ensure that the child will get rendered as well. After rendering,
124
//we'll remove the child from the hierarchy, so as to return to our
125
//original state)
126
if (logger.isDebugEnabled()) logger.debug("Creating table component for body...");
127                 DefaultListModel rowBodyModel = new DefaultListModel();
128 // BList rowBodyList = new BList(rowBodyModel, new DefaultView(bodyNode));
129
BList rowBodyList = new BList(rowBodyModel);
130                 rowBodyList.setView(new DefaultView(bodyNode));
131 // if (tempChildren==null) tempChildren = new ArrayList();
132
// tempChildren.add(rowBodyList);
133
// this.add(rowBodyList);
134
btable.addStepChild(rowBodyList);
135
136                 //create a model, view, and list for the header (default it
137
//to be the same objects as for the body)
138
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, new DefaultView(headerNode));
144
rowHeaderList = new BList(rowHeaderModel);
145                     rowHeaderList.setView(new DefaultView(headerNode));
146 // tempChildren.add(rowHeaderList);
147
// this.add(rowHeaderList);
148
btable.addStepChild(rowHeaderList);
149                 }
150
151                 //do the same thing for the footer model
152
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, new DefaultView(footerNode));
158
rowFooterList = new BList(rowFooterModel);
159                     rowFooterList.setView(new DefaultView(footerNode));
160 // tempChildren.add(rowFooterList);
161
// this.add(rowFooterList);
162
btable.addStepChild(rowFooterList);
163                 }
164
165                 //now populate the rowModel
166
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 JavaDoc errmsg = "Node does not implement HTMLElement and cannot be rendered: "+node;
185             logger.warn(errmsg);
186             throw new NoSuitableRendererException(errmsg);
187         }
188         
189         //finally, make sure we reflect the components enabled/disabled status
190
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         //give the model a chance to initialize
196
curModel.setViewContext(vc);
197         curModel.resetModel();
198 // vc.setTemplateNode(node);
199
Node origTemplateNode = vc.getTemplateNode(); //csc_100901.4
200
vc.putState(ViewContext.TEMPLATE_NODE, node);
201                 
202         //now process items in the model based on model type
203
try {
204             ElementFactory ef = view.getElementFactory();
205             for (int i=0,max=curModel.getRowCount(); i<max; i++) {
206
207                 //create a generic row element and add it to the rowModel
208
Element rowNode = node.getOwnerDocument().createElement("TR");
209                 rowModel.add(rowNode);
210
211                 //create a list component for the cols in the row and bind it
212
//to the rowNode
213
DefaultListModel colModel = new DefaultListModel();
214                 DefaultView colView = new DefaultView(rowNode);
215 // BList colList = new BList(colModel, colView);
216
BList colList = new BList(colModel);
217                 colList.setView(colView);
218                 rowList.addChild(colList);
219             
220                 //now populate the colModel
221
for (int j=0,jmax=curModel.getColumnCount(); j<jmax; j++) {
222                     Object JavaDoc item = curModel.getItemAt(i, j);
223                     if (item!=null) colModel.add(item);
224                 }
225             }
226         } finally {
227 //csc_100901.4 vc.putState(ViewContext.TEMPLATE_NODE, null);
228
vc.putState(ViewContext.TEMPLATE_NODE, origTemplateNode); //csc_100901.4
229
curModel.setViewContext(null);
230         }
231     }
232 }
Popular Tags