KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > comp > BTable


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: BTable.java,v 1.17 2004/02/01 05:16:27 christianc Exp $
19  */

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.view.*;
33 import org.enhydra.barracuda.core.util.dom.DOMUtil;
34 import org.enhydra.barracuda.plankton.*;
35
36 /**
37  * A BTable is used to put data into a table format within a DOM. In
38  * practice, this has proven to be one of the least used Barracuda
39  * components, since it it usually a lot easier (and more flexible)
40  * simply to use a BTemplate.
41  *
42  * <p>In the case of BTable, you will ALMOST ALWAYS need to manually
43  * bind it to a View, unless you happen to be returning it from a model
44  * (in which case this will be done for you automatically)
45  */

46 public class BTable extends BComponent {
47
48     //public vars
49
protected static final Logger logger = Logger.getLogger(BTable.class.getName());
50     
51     //private vars
52
protected TableModel model = null;
53     protected TableModel headerModel = null;
54     protected TableModel footerModel = null;
55     private LocalModelListener callback = null;
56     private LocalModelListener headerCallback = null;
57     private LocalModelListener footerCallback = null;
58     protected Node templateNode = null;
59     protected BText caption = null; //ndc_101202.1
60

61     //--------------- Constructors -------------------------------
62
/**
63      * Public noargs constructor
64      */

65     public BTable() {
66         this (null);
67     }
68     
69     /**
70      * Public constructor which creates the component and
71      * binds it to a specific model. The component is also
72      * bound to the specified view.
73      *
74      * <p>Null values may be passed in for any parameters,
75      * but if you do so you will need manually provide these
76      * values (via the accessor methods) prior to actually
77      * rendering the component
78      *
79      * @param imodel the specific model to back this component
80      */

81     public BTable(TableModel imodel) {
82         this (null, imodel, null, null);
83     }
84     
85     /**
86      * Public constructor which creates the component and
87      * binds it to a specific model. The component is also
88      * bound to the specified view.
89      *
90      * <p>Null values may be passed in for any parameters,
91      * but if you do so you will need manually provide these
92      * values (via the accessor methods) prior to actually
93      * rendering the component
94      *
95      * @param headerModel the specific header model to back this component
96      * @param bodyModel the main data model that backs this component
97      * @param footerModel the specific footer model to back this component
98      * @param view the View the component should be bound to
99      */

100     BTable(TableModel iheaderModel, TableModel ibodyModel, TableModel ifooterModel, TableView view) {
101         if (iheaderModel!=null) setHeaderModel(iheaderModel);
102         if (ibodyModel!=null) setModel(ibodyModel);
103         if (ifooterModel!=null) setFooterModel(ifooterModel);
104         if (view!=null) this.addView(view);
105     }
106     
107
108
109
110
111     //--------------- Renderer -----------------------------------
112
/**
113      * Default component renderer factory registrations
114      */

115     static {
116         HTMLRendererFactory rfHTML = new HTMLRendererFactory();
117         installRendererFactory(rfHTML, BTable.class, HTMLElement.class);
118         installRendererFactory(rfHTML, BTable.class, HTMLDocument.class);
119     }
120
121     /**
122      * HTML RendererFactory
123      */

124     static class HTMLRendererFactory implements RendererFactory {
125         public Renderer getInstance() {return new HTMLTableRenderer();}
126     }
127
128
129
130
131     //--------------- BTable ------------------------------
132
/**
133      * Set the model that backs the table. This causes the table to register
134      * as a listener on the model, so any changes to it will be reflected in
135      * the table.
136      *
137      * @param imodel the model that backs the table
138      */

139     public void setModel(TableModel imodel) {
140
141         //deregister if possible
142
if (model!=null && callback!=null) {
143 // model.removeTableModelListener(callback);
144
model.removeModelListener(callback);
145         }
146     
147         //set the model
148
model = imodel;
149         invalidate();
150         
151         //reregister if possible
152
if (model!=null) {
153             if (callback==null) callback = new LocalModelListener();
154 // model.addTableModelListener(callback);
155
model.addModelListener(callback);
156         }
157     }
158     
159     /**
160      * Get the model that backs the table
161      *
162      * @return the model that backs the table
163      */

164     public TableModel getModel() {
165         return model;
166     }
167     
168     /**
169      * Set the header model that backs the table. This causes
170      * the table to register as a listener on the model, so
171      * any changes to it will be reflected in the table header.
172      *
173      * @param imodel the headermodel that backs the table
174      */

175     public void setHeaderModel(TableModel imodel) {
176
177         //deregister if possible
178
if (headerModel!=null && headerCallback!=null) {
179 // headerModel.removeTableModelListener(headerCallback);
180
headerModel.removeModelListener(headerCallback);
181         }
182     
183         //set the model
184
headerModel = imodel;
185         invalidate();
186         
187         //reregister if possible
188
if (headerModel!=null) {
189             if (headerCallback==null) headerCallback = new LocalModelListener();
190 // headerModel.addTableModelListener(headerCallback);
191
headerModel.addModelListener(headerCallback);
192         }
193     }
194     
195     /**
196      * Get the header model that backs the table
197      *
198      * @return the header model that backs the table
199      */

200     public TableModel getHeaderModel() {
201         return headerModel;
202     }
203     
204     /**
205      * Set the footer model that backs the table. This causes
206      * the table to register as a listener on the model, so
207      * any changes to it will be reflected in the table footer.
208      *
209      * @param imodel the footer model that backs the table
210      */

211     public void setFooterModel(TableModel imodel) {
212
213         //deregister if possible
214
if (footerModel!=null && footerCallback!=null) {
215 // footerModel.removeTableModelListener(footerCallback);
216
footerModel.removeModelListener(footerCallback);
217         }
218     
219         //set the model
220
footerModel = imodel;
221         invalidate();
222         
223         //reregister if possible
224
if (footerModel!=null) {
225             if (footerCallback==null) footerCallback = new LocalModelListener();
226 // footerModel.addTableModelListener(footerCallback);
227
footerModel.addModelListener(footerCallback);
228         }
229     }
230     
231     /**
232      * Get the footer model that backs the table
233      *
234      * @return the footer model that backs the table
235      */

236     public TableModel getFooterModel() {
237         return footerModel;
238     }
239     
240     //ndc_101202.1 - added
241
/**
242      * Set the caption that backs the table
243      *
244      * @param icaption A BText to represent to Caption Element.
245      */

246     public void setCaption(BText icaption) {
247         this.caption = icaption;
248     }
249
250     //ndc_101202.1 - added
251
/**
252      * Get the caption that backs the table
253      *
254      * @return the caption to be rendered
255      */

256     public BText getCaption() {
257         return this.caption;
258     }
259
260     /**
261      * Render a specific view for the component.
262      *
263      * @param view View to be rendered
264      * @param vc ViewContext for the client view
265      * @throws RenderException if the particular View is not supported
266      * @param list a List of all the views for this component
267      */

268 /*
269 //021102.3_csc - removed, because now its in BComponent
270     protected void renderView (View view, ViewContext vc, int depth) throws RenderException {
271         if (logger.isInfoEnabled()) logger.info("rendering view: "+view);
272
273         //actually render the view according to known interfaces
274         try {
275             Renderer r = getRenderer(view);
276             r.renderComponent(this, view, vc);
277             
278         } catch (DOMException e) {
279             logger.warn("DOM Error:", e);
280             throw new DOMAccessException("Error rendering component in view:"+e, e);
281         }
282     }
283 */

284
285
286
287     //--------------- Lifecycle ----------------------------------
288
/**
289      * Destroy cycle. The component should use this cycle to
290      * perform any special cleanup.
291      */

292     public void destroyCycle() {
293         //default destroy
294
super.destroyCycle();
295     
296         //we set the model to null so that the component can be
297
//garbage collected. If we don't do this, the model retains
298
//a reference back to the component and so the component
299
//will never be freed up...
300
setModel(null);
301         setHeaderModel(null);
302         setFooterModel(null);
303     }
304
305
306
307
308     //--------------- Utility ------------------------------------
309
/*
310     class LocalTableModelListener implements TableModelListener {
311         //for right now, just invalidate everything. In the future,
312         //we could get a lot smarter and keep track of the ranges
313         //changed and then only redraw those. This would greatly
314         //speed rendering, because we'd only need to redraw the parts
315         //of the DOM that changed...
316         public void tableChanged(TableModelEvent e) {
317             invalidate();
318         }
319     }
320 */

321     class LocalModelListener implements ModelListener {
322         //get notified when one of the underlying models changes
323
public void modelChanged(Model m) {
324             invalidate();
325         }
326     }
327 }
Popular Tags