KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > contrib > dbroggisch > display > IterativeCollectionModel


1 /*
2  * Copyright (C) 2003 Diez B. Roggisch [deets@web.de]
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: IterativeCollectionModel.java,v 1.2 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.contrib.dbroggisch.display;
21
22
23 import java.util.*;
24 import javax.servlet.*;
25 import javax.servlet.http.*;
26
27 import org.enhydra.barracuda.core.comp.*;
28 import org.enhydra.barracuda.core.forms.*;
29 import org.enhydra.barracuda.core.comp.model.*;
30
31 /**
32  * Support class to build IterativeModels
33  *
34  * Use this class to build IterativeModels bassed on arbitrary Collections.
35  * The class will iterate over the Collection and store the current item
36  * in the _current variable.
37  * <p>
38  * To make this class work, you will have to subclass it and provide a
39  * getItem implementation.<p>
40  *
41  * A possible usage as anonymous inner class could look like this:
42  * <code><br>
43  * IterativeCollectionModel ilm = new IterativeCollectionModel() {<br>
44  * public Object getItem(Strink key) {<br>
45  * if(key.equals("MYKEY"))= {
46  * return _current;<br>
47  * }<br>
48  * }}<br>
49  *<p>
50  * If the Collection is of type TemplateModel, it could look like this:
51  * <code><br>
52  * IterativeCollectionModel ilm = new IterativeCollectionModel() {<br>
53  * public Object getItem(Strink key) {<br>
54  * return ((TemplateModel)_current).getItem(key);<br>
55  * }<br>
56  * }}<br>
57  *<p>
58  *
59  * @author <a HREF="mailto:deets@web.de">Diez B. Roggisch</a>
60  * @version 1.0
61  */

62 public class IterativeCollectionModel extends AbstractTemplateModel implements IterativeModel {
63
64     private Collection _models;
65     private String JavaDoc _name;
66     private Iterator _it;
67     /**
68      * The current item of the list.
69      *
70      */

71     protected Object JavaDoc _current;
72
73
74     /**
75      * Creates a new <code>IterativeCollectionModel</code> instance.
76      *
77      * @param name a <code>String</code> value
78      * @param models a <code>Collection</code> value
79      */

80     public IterativeCollectionModel(String JavaDoc name, Collection models) {
81         _name = name;
82         _models = models;
83     }
84
85     /**
86      * Creates a new <code>IterativeCollectionModel</code> instance.
87      *
88      * @param name a <code>String</code> value
89      */

90     public IterativeCollectionModel(String JavaDoc name) {
91         this(name, new ArrayList());
92     }
93
94     /**
95      * Creates a new <code>IterativeCollectionModel</code> instance.
96      *
97      */

98     public IterativeCollectionModel() {
99         this(null, new ArrayList());
100     }
101
102
103     /**
104      * Sets the modelname
105      *
106      * @param name a <code>String</code> value
107      */

108     public void setName(String JavaDoc name) {
109         _name = name;
110     }
111
112
113     /**
114      * getName.
115      *
116      * @return a <code>String</code> value
117      */

118     public String JavaDoc getName() {
119         return _name;
120     }
121
122     /**
123      * Gets the value of _models
124      *
125      * @return the value of _models
126      */

127     public Collection getModels() {
128         return this._models;
129     }
130
131
132     /**
133      * Sets the value of _models
134      *
135      * @param arg_models Value to assign to this._models
136      */

137     public void setModels(Collection arg_models){
138         this._models = arg_models;
139     }
140
141     /**
142      * Describe <code>add</code> method here.
143      *
144      * @param val an <code>Object</code> value
145      */

146     public void add(Object JavaDoc val) {
147         _models.add(val);
148     }
149
150     // implementation of org.enhydra.barracuda.core.comp.IterativeModel interface
151

152
153     public boolean hasNext()
154     {
155         return (_it != null && _it.hasNext());
156     }
157
158     public void preIterate()
159     {
160         _it = _models.iterator();
161     }
162
163     public void postIterate()
164     {
165
166     }
167
168     public void loadNext()
169     {
170         _current = _it.next();
171     }
172
173 }
174
Popular Tags