KickJava   Java API By Example, From Geeks To Geeks.

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


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

20 package org.enhydra.barracuda.core.comp;
21
22 import java.util.*;
23 import org.enhydra.barracuda.core.comp.model.*;
24
25 /**
26  * This class provides the abstract implementation
27  * for a Template Model.
28  */

29 public abstract class AbstractTemplateModel implements TemplateModel {
30
31     public static final String JavaDoc UNDEFINED = "Undefined";
32
33     protected ViewContext viewContext = null;
34     protected List listeners = new ArrayList();
35
36     //--------------- AbstractTemplateModel ----------------------
37
/**
38      * Add a listener to the template that's notified each time a change
39      * to the data model occurs.
40      *
41      * @param ml the TemplateModelListener
42      */

43     public void addModelListener(ModelListener ml) {
44         listeners.add(ml);
45     }
46
47     /**
48      * Remove a listener
49      *
50      * @param ml the TemplateModelListener
51      */

52     public void removeModelListener(ModelListener ml) {
53         listeners.remove(ml);
54     }
55     
56     /**
57      * Forwards the given notification event to all
58      * <code>TemplateModelListeners</code> that registered
59      * themselves as listeners for this template model.
60      */

61     public void fireModelChanged() {
62         Iterator it = listeners.iterator();
63         ModelListener ml = null;
64         while (it.hasNext()) {
65             ml = (ModelListener) it.next();
66             ml.modelChanged(this);
67         }
68     }
69
70
71     //--------------- AbstractTemplateModel ----------------------
72
/**
73      * Add a listener to the template that's notified each time a change
74      * to the data model occurs.
75      *
76      * @param l the TemplateModelListener
77      */

78 // public void addTemplateModelListener(TemplateModelListener l) {
79
// listeners.add(l);
80
// }
81

82     /**
83      * Remove a listener from the template.
84      *
85      * @param l the TemplateModelListener
86      */

87 // public void removeTemplateModelListener(TemplateModelListener l) {
88
// listeners.remove(l);
89
// }
90

91     /**
92      * Reset the model to its initial (unprocessed) state. This
93      * is a convenience method that gets invoked prior to the
94      * entire model being rendered. You only need to override this
95      * method if you want to do something (like reset internal counters)
96      * before the model is queried
97      */

98 /*
99 //csc_100401.1 - this doesn't need to be in here
100     public void resetModel() {
101         //nop
102     }
103 */

104     /**
105      * process any directives. return false to indicate a the node
106      * containing this directive should be skipped.
107      */

108     public boolean processDirective(TemplateDirective td) {
109         return true; //by default, allow all directives
110
}
111
112     //csc_030603.2 - added
113
/**
114      * get an item for a given template directive. Implement this
115      * method if you want access to the full directive, not just the
116      * String key. Note that if you implement this method, AND you
117      * want to use the getItem(String key) method, then your implementation
118      * of this method must be sure to call super.getItem() in order for
119      * for the convenience method to get called.
120      */

121     public Object JavaDoc getItem(TemplateDirective td) {
122         return getItem(td.getKeyName());
123     }
124
125     /**
126      * Convenience method to get an item based on the key name (extracted
127      * from the TemplateDirective).
128      */

129     public Object JavaDoc getItem(String JavaDoc key) {
130         return getName()+"."+key+" "+UNDEFINED;
131     }
132
133     /**
134      * Forwards the given notification event to all
135      * <code>TemplateModelListeners</code> that registered
136      * themselves as listeners for this template model.
137      *
138      * @param e the event to be forwarded
139      *
140      * @see #addTemplateModelListener
141      * @see TemplateModelEvent
142      */

143 // public void fireTemplateChanged(TemplateModelEvent e) {
144
// Iterator it = listeners.iterator();
145
// TemplateModelListener tml = null;
146
// while (it.hasNext()) {
147
// tml = (TemplateModelListener) it.next();
148
// tml.modelChanged(e);
149
// }
150
// }
151

152     /**
153      * Return all the listeners for this given list model (returns
154      * a copy of the underlying List)
155      *
156      * @returns all the listeners for this given list model
157      */

158 // public List getListeners() {
159
// return new ArrayList(listeners);
160
// }
161

162     //--------------- Contextual ---------------------------------
163
/**
164      * Specify the ViewContext. This method will generally be called
165      * by the class that is using the model to actually render the data
166      * in a view. The context will be specified prior to a render pass,
167      * and the context will be reset to null after the render pass.
168      *
169      * @param ivc the current ViewContext
170      */

171     public void setViewContext(ViewContext ivc) {
172         viewContext = ivc;
173     }
174
175     /**
176      * Get the current ViewContext
177      *
178      * @return the current ViewContext
179      */

180     public ViewContext getViewContext() {
181         return viewContext;
182     }
183
184 }
Popular Tags