KickJava   Java API By Example, From Geeks To Geeks.

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


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: BTemplate.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.util.dom.DOMUtil;
33 import org.enhydra.barracuda.core.view.*;
34 import org.enhydra.barracuda.plankton.*;
35
36 /**
37  * A BTemplate is used to process part of a DOM as a template--the
38  * component will look for directives and then query the models to
39  * return the data associated with a given key. BTemplate essentially
40  * brings "pull-mvc" to XMLC.
41  *
42  * <p>In the case of BTemplate, 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 BTemplate extends BComponent {
47
48     //public vars
49
protected static final Logger logger = Logger.getLogger(BTemplate.class.getName());
50
51     
52     //private vars
53
protected HashMap templateModels = new HashMap();
54 // protected TreeMap templateModels = new TreeMap();
55
private LocalModelListener callback = new LocalModelListener();
56 // protected Node templateNode = null;
57
// protected Stack itStack = null;
58

59     /**
60      * @clientCardinality 1..*
61      */

62     private TemplateModel lnkTemplateModel;
63
64
65     //--------------- Constructors -------------------------------
66
/**
67      * Public noargs constructor
68      */

69     public BTemplate() {
70         this (null, null);
71     }
72     
73     /**
74      * Public constructor which creates the component and
75      * binds it to a specific model. You will need to manually
76      * set the View if you use this constructor.
77      *
78      * @param imodel the specific model to back this component
79      */

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

97     BTemplate(TemplateModel imodel, TemplateView iview) {
98         if (imodel!=null) addModel(imodel);
99         if (iview!=null) this.addView(iview);
100     }
101     
102
103
104
105     //--------------- Renderer -----------------------------------
106
/**
107      * Default component renderer factory registrations
108      */

109     static {
110         HTMLRendererFactory rfHTML = new HTMLRendererFactory();
111         installRendererFactory(rfHTML, BTemplate.class, HTMLElement.class);
112         installRendererFactory(rfHTML, BTemplate.class, HTMLDocument.class);
113
114         XMLRendererFactory rfXML = new XMLRendererFactory();
115         installRendererFactory(rfXML, BTemplate.class, Node.class);
116     }
117
118     /**
119      * HTML RendererFactory
120      */

121     static class HTMLRendererFactory implements RendererFactory {
122         public Renderer getInstance() {return new HTMLTemplateRenderer();}
123     }
124
125     /**
126      * XML RendererFactory
127      */

128     static class XMLRendererFactory implements RendererFactory {
129         public Renderer getInstance() {return new XMLTemplateRenderer();}
130     }
131
132
133
134     //--------------- BTemplate ---------------------------
135
/**
136      * Add a model to the component. Unlike other components, the template
137      * component can have ant number of models...
138      *
139      * @param imodel a model that backs the template
140      */

141     public void addModel(TemplateModel imodel) {
142         //eliminate the obvious
143
if (imodel==null) return;
144     
145         //saw_121102.1 - deregister first if possible; this ensures that if there
146
//is already a model in place with the same name, that one will get properly
147
//removed and have it listeners deregistered
148
imodel.removeModelListener(callback);
149         
150         //add the model and set the default if necess.
151
templateModels.put(imodel.getName(), imodel);
152         invalidate();
153         
154         //reregister if possible
155
imodel.addModelListener(callback);
156     }
157     
158     /**
159      * Add a whole list of models to the component.
160      *
161      * @param ilist a list of TemplateModels to back the component
162      */

163     public void addModels(List ilist) {
164         //eliminate the obvious
165
if (ilist==null) return;
166     
167         //iterate through the list, adding any instances of TemplateModel
168
Iterator it = ilist.iterator();
169         while (it.hasNext()) {
170             Object JavaDoc o = it.next();
171             if (o instanceof TemplateModel) addModel((TemplateModel) o);
172         }
173     }
174     
175     /**
176      * Remove a model from the component.
177      *
178      * @param imodel a model that backs the template
179      */

180     public void removeModel(TemplateModel imodel) {
181         //eliminate the obvious
182
if (imodel==null) return;
183     
184         //add the model and set the default if necess.
185
templateModels.remove(imodel.getName());
186         invalidate();
187         
188         //deregister if possible
189
imodel.removeModelListener(callback);
190     }
191
192     /**
193      * Remove a model from the component by model name
194      *
195      * @param modelName the name of the model to be removed
196      */

197     public void removeModel(String JavaDoc modelName) {
198         removeModel(getModel(modelName));
199     }
200
201     //saw_121002.1 - added
202
/**
203      * Remove all models from the component
204      */

205     public void removeAllModels() {
206         Iterator it = templateModels.entrySet().iterator();
207         while (it.hasNext()) {
208             Map.Entry entry = (Map.Entry) it.next();
209             TemplateModel model = (TemplateModel) entry.getValue();
210             
211             //remove the model
212
it.remove();
213
214             //deregister if possible
215
model.removeModelListener(callback);
216         }
217         
218         invalidate();
219     }
220
221     /**
222      * Get the model that backs the table
223      *
224      * @param modelName the name of the model we're interested in
225      */

226     public TemplateModel getModel(String JavaDoc modelName) {
227         return (TemplateModel) templateModels.get(modelName);
228     }
229     
230     /**
231      * Get a list of models associated with this view
232      *
233      * @return a list of models associated with this view
234      */

235     public List getModels() {
236         return new ArrayList(templateModels.values());
237     }
238     
239     /**
240      * Render a specific view for the component.
241      *
242      * @param view View to be rendered
243      * @param vc ViewContext for the client view
244      * @throws RenderException if the particular View is not supported
245      * @param list a List of all the views for this component
246      */

247 /*
248 //021102.3_csc - removed, because now its in BComponent
249     protected void renderView (View view, ViewContext vc, int depth) throws RenderException {
250         if (logger.isInfoEnabled()) logger.info("rendering view: "+view);
251
252         //actually render the view according to known interfaces
253         try {
254             Renderer r = getRenderer(view);
255             r.renderComponent(this, view, vc);
256         } catch (DOMException e) {
257             logger.warn("DOM Error:", e);
258             throw new DOMAccessException("Error rendering component in view:"+e, e);
259         }
260     }
261 */

262
263
264
265     //--------------- Lifecycle ----------------------------------
266
/**
267      * Destroy cycle. The component should use this cycle to
268      * perform any special cleanup.
269      */

270     public void destroyCycle() {
271         //default destroy
272
super.destroyCycle();
273     
274         //we set the model to null so that the component can be
275
//garbage collected. If we don't do this, the model retains
276
//a reference back to the component and so the component
277
//will never be freed up...
278
removeAllModels(); //saw_121002.1 - we should remove all models first so everything gets deregistered properly, allowing it to be gc'd
279
templateModels = null;
280         callback = null; //csc_041202.1 - seems like this should be getting cleared too...
281
}
282
283
284
285
286     //--------------- Utility ------------------------------------
287
/*
288     class LocalTemplateModelListener implements TemplateModelListener {
289         //get notified when one of the underlying models changes
290         public void modelChanged(TemplateModelEvent e) {
291             invalidate();
292         }
293     }
294 */

295     class LocalModelListener implements ModelListener {
296         //get notified when one of the underlying models changes
297
public void modelChanged(Model m) {
298             invalidate();
299         }
300     }
301 }
Popular Tags