KickJava   Java API By Example, From Geeks To Geeks.

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


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: AbstractListModel.java,v 1.7 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 List Model.
28  */

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

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

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

59     public void fireModelChanged() {
60         Iterator it = listeners.iterator();
61         ModelListener ml = null;
62         while (it.hasNext()) {
63             ml = (ModelListener) it.next();
64             ml.modelChanged(this);
65         }
66     }
67 // protected List listeners = new ArrayList();
68

69     //--------------- AbstractListModel --------------------------
70
/**
71      * Add a listener to the list that's notified each time a change
72      * to the data model occurs.
73      *
74      * @param l the ListDataListener
75      */

76 // public void addListDataListener(ListDataListener l) {
77
// listeners.add(l);
78
// }
79

80     /**
81      * Remove a listener from the list that's notified each time a
82      * change to the data model occurs.
83      * @param l the ListDataListener
84      */

85 // public void removeListDataListener(ListDataListener l) {
86
// listeners.remove(l);
87
// }
88

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

96     public void resetModel() {
97         //nop
98
}
99
100     /**
101      * AbstractListModel subclasses must call this method <b>after</b>
102      * one or more elements of the list change. The changed elements
103      * are specified by a closed interval index0, index1, i.e. the
104      * range that includes both index0 and index1. Note that
105      * index0 need not be less than or equal to index1.
106      *
107      * @param source The ListModel that changed, typically "this".
108      * @param index0 One end of the new interval.
109      * @param index1 The other end of the new interval.
110      * @see DefaultListModel
111      */

112 /*
113     protected void fireContentsChanged(Object source, int index0, int index1) {
114         Iterator it = listeners.iterator();
115         ListDataListener ldl = null;
116         ListDataEvent e = null;
117         while (it.hasNext()) {
118             ldl = (ListDataListener) it.next();
119             if (e==null) e = new ListDataEvent(source, ListDataEvent.CONTENTS_CHANGED, index0, index1);
120             ldl.contentsChanged(e);
121         }
122     }
123 */

124     /**
125      * AbstractListModel subclasses must call this method <b>after</b>
126      * one or more elements are added to the model. The new elements
127      * are specified by a closed interval index0, index1, i.e. the
128      * range that includes both index0 and index1. Note that
129      * index0 need not be less than or equal to index1.
130      *
131      * @param source The ListModel that changed, typically "this".
132      * @param index0 One end of the new interval.
133      * @param index1 The other end of the new interval.
134      * @see DefaultListModel
135      */

136 /*
137     protected void fireIntervalAdded(Object source, int index0, int index1) {
138         Iterator it = listeners.iterator();
139         ListDataListener ldl = null;
140         ListDataEvent e = null;
141         while (it.hasNext()) {
142             ldl = (ListDataListener) it.next();
143             if (e==null) e = new ListDataEvent(source, ListDataEvent.INTERVAL_ADDED, index0, index1);
144             ldl.contentsChanged(e);
145         }
146     }
147 */

148     /**
149      * AbstractListModel subclasses must call this method <b>after</b>
150      * one or more elements are removed from the model. The new elements
151      * are specified by a closed interval index0, index1, i.e. the
152      * range that includes both index0 and index1. Note that
153      * index0 need not be less than or equal to index1.
154      *
155      * @param source The ListModel that changed, typically "this".
156      * @param index0 One end of the new interval.
157      * @param index1 The other end of the new interval.
158      * @see DefaultListModel
159      */

160 /*
161     protected void fireIntervalRemoved(Object source, int index0, int index1) {
162         Iterator it = listeners.iterator();
163         ListDataListener ldl = null;
164         ListDataEvent e = null;
165         while (it.hasNext()) {
166             ldl = (ListDataListener) it.next();
167             if (e==null) e = new ListDataEvent(source, ListDataEvent.INTERVAL_REMOVED, index0, index1);
168             ldl.contentsChanged(e);
169         }
170     }
171 */

172     /**
173      * Return all the listeners for this given list model (returns
174      * a copy of the underlying List)
175      *
176      * @returns all the listeners for this given list model
177      */

178 /*
179     public List getListeners() {
180         return new ArrayList(listeners);
181     }
182 */

183
184     //--------------- Contextual ---------------------------------
185
/**
186      * Specify the ViewContext. This method will generally be called
187      * by the class that is using the model to actually render the data
188      * in a view. The context will be specified prior to a render pass,
189      * and the context will be reset to null after the render pass.
190      *
191      * @param ivc the current ViewContext
192      */

193     public void setViewContext(ViewContext ivc) {
194         viewContext = ivc;
195     }
196
197     /**
198      * Get the current ViewContext
199      *
200      * @return the current ViewContext
201      */

202     public ViewContext getViewContext() {
203         return viewContext;
204     }
205
206 }
Popular Tags