KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > bsf > remoteIterator > client > tableModel > DefaultRemoteIteratorTableModel


1 package org.bsf.remoteIterator.client.tableModel;
2
3 import org.bsf.remoteIterator.client.RemoteIteratorClient;
4
5 import javax.swing.table.AbstractTableModel JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Vector JavaDoc;
9
10 /**
11  * A default implementation of a RemoteIteratorTableModel. You can look at it
12  * (or extend it) to build your own TableModel. A smart move is to add a static
13  * method that, when called, removes all the RI that are still active when called.
14  */

15 public class DefaultRemoteIteratorTableModel extends AbstractTableModel JavaDoc implements RemoteIteratorTableModel {
16     private int _initialBlockSize = 25;
17     private int _defaultBlockSize = 250;
18     private int _delayBetweenRetrieval = 0;
19
20     private BackgroundLoadingThread _loadingThread = null;
21
22     /**
23      * Holds the rows returned by the RI.
24      */

25     private Vector JavaDoc _rows = null;
26
27     // *************************************************************************
28
// ****************************** Constructors *****************************
29
// *************************************************************************
30

31     /**
32      * Creates a DefaultRemoteIteratorTableModel with a default estimated size of
33      * 1000 rows. Anyway, the underlying container will be trimmed to size when
34      * all the rows will have been loaded.
35      *
36      * @see #DefaultRemoteIteratorTableModel( int p_estimatedSize )
37      */

38     public DefaultRemoteIteratorTableModel() {
39         this( 1000 );
40     }
41
42     /**
43      * Creates a DefaultRemoteIteratorTableModel with a default estimated size of
44      * 1000 rows. Anyway, the underlying container will be trimmed to size when
45      * all the rows will have been loaded. The provided RemoteIteratorClient is
46      * used and the loading is started.
47      *
48      * @param p_remoteIteratorClient The RemoteIteratorClient to use for data loading.
49      *
50      * @see #DefaultRemoteIteratorTableModel( int p_estimatedSize )
51      * @see RemoteIteratorClient
52      */

53     public DefaultRemoteIteratorTableModel( RemoteIteratorClient p_remoteIteratorClient ) {
54         this();
55
56         // We set theRemoteIteratorClient
57
setRemoteIteratorClient( p_remoteIteratorClient );
58
59         // We start to load the data
60
startLoadingData();
61     }
62
63     /**
64      * Creates a DefaultRemoteIteratorTableModel with the given int as the size
65      * of the underlying List storing the rows. The increase value will use the
66      * given int as well. Anyway, the underlying container will be trimmed to
67      * size when all the rows will have been loaded.
68      *
69      * @param p_estimatedSize The size and the increase number to use for the
70      * underlying List storing the* rows. Should be used to prevent on-going
71      * resize of the List. No check is done on this value which, obviously,
72      * should be greater than 0.
73      */

74     public DefaultRemoteIteratorTableModel( int p_estimatedSize ) {
75         super();
76
77         _rows = new Vector JavaDoc( p_estimatedSize, p_estimatedSize );
78     }
79
80     /**
81      * Creates a DefaultRemoteIteratorTableModel with the given estimated size of
82      * rows. Anyway, the underlying container will be trimmed to size when all
83      * the rows will have been loaded. The provided RemoteIteratorClient is used
84      * and the loading is started.
85      *
86      * @param p_remoteIteratorClient The RemoteIteratorClient to use for data loading.
87      * @param p_estimatedSize The size and the increase number to use for the
88      * underlying List storing the* rows. Should be used to prevent on-going
89      * resize of the List. No check is done on this value which, obviously,
90      * should be greater than 0.
91      *
92      * @see #DefaultRemoteIteratorTableModel( int p_estimatedSize )
93      * @see RemoteIteratorClient
94      */

95     public DefaultRemoteIteratorTableModel( RemoteIteratorClient p_remoteIteratorClient, int p_estimatedSize ) {
96         this( p_estimatedSize );
97
98         // We set theRemoteIteratorClient
99
setRemoteIteratorClient( p_remoteIteratorClient );
100
101         // We start to load the data
102
startLoadingData();
103     }
104
105     // *************************************************************************
106
// ********************** AbstractTableModel Methods ***********************
107
// *************************************************************************
108

109     public int getRowCount() {
110         return _rows.size();
111     }
112
113     public int getColumnCount() {
114         if ( getRowCount() > 0 ) {
115             // We return the number of column in the first Row
116
List JavaDoc aRow = (List JavaDoc) _rows.get( 0 );
117             return aRow.size();
118         } else {
119             return 0;
120         }
121     }
122
123     public Object JavaDoc getValueAt( int p_rowIndex, int p_columnIndex ) {
124         List JavaDoc aRow = (List JavaDoc) _rows.get( p_rowIndex );
125
126         return aRow.get( p_columnIndex );
127     }
128
129     // *************************************************************************
130
// ****************** RemoteIteratorTableModel Methods *********************
131
// *************************************************************************
132

133     public void setRemoteIteratorClient( RemoteIteratorClient p_remoteIteratorClient ) {
134         if ( p_remoteIteratorClient == null ) {
135             throw new IllegalArgumentException JavaDoc( "Need a non null RemoteIteratorClient..." );
136         }
137
138         // If needed, we remove the old rows and notify the listeners
139
if ( isLoadingData() ) {
140             stopLoadingData();
141         }
142
143         // We clear the data already present
144
if ( _rows.size() != 0 ) {
145             _rows.clear();
146             fireTableDataChanged();
147         }
148
149         // We instantiate the loading Thread
150
_loadingThread = new BackgroundLoadingThread( p_remoteIteratorClient, this );
151     }
152
153     public void startLoadingData() {
154         if ( _loadingThread != null ) {
155             _loadingThread.start();
156         } else {
157             throw new IllegalStateException JavaDoc( "The data have already been loaded..." );
158         }
159     }
160
161     public void stopLoadingData() {
162         if ( _loadingThread != null ) {
163             _loadingThread.stopLoading(); // We request the stop loading...
164
_loadingThread = null; // We discard the ref...
165

166             // No more data will come...
167
_rows.trimToSize();
168         }
169     }
170
171     public boolean isLoadingData() {
172         boolean isLoadingData = false;
173
174         if ( _loadingThread != null && _loadingThread.isAlive() ) {
175             isLoadingData = true;
176         }
177
178         return isLoadingData;
179     }
180
181     public void releaseResources() {
182         if ( _loadingThread != null ) {
183             _loadingThread.stopLoading();
184             _loadingThread = null;
185         }
186     }
187
188     // *************************************************************************
189
// ****************************** Accessors ********************************
190
// *************************************************************************
191

192     /**
193      * @return the default block size (the size of the returned block, but the first
194      * one).
195      *
196      * @see #setDefaultBlockSize
197      * @see #setInitialBlockSize
198      */

199     public int getDefaultBlockSize() {
200         return _defaultBlockSize;
201     }
202
203     /**
204      * @see #getDefaultBlockSize
205      */

206     public void setDefaultBlockSize( int p_defaultBlockSize ) {
207         _defaultBlockSize = p_defaultBlockSize;
208     }
209
210     /**
211      * @return the initialBlockSize (the size of the first returned block, should
212      * be small to accelerate data display to the user...).
213      *
214      * @see #setInitialBlockSize
215      */

216     public int getInitialBlockSize() {
217         return _initialBlockSize;
218     }
219
220     /***
221      * @see #getInitialBlockSize
222      */

223     public void setInitialBlockSize( int p_initialBlockSize ) {
224         _initialBlockSize = p_initialBlockSize;
225     }
226
227     /**
228      * @return The delay, in ms, between two calls to next() on the RemoteIterator.
229      * Default value is 0.
230      *
231      * @see #setDelayBetweenRetrieval
232      */

233     public int getDelayBetweenRetrieval() {
234         return _delayBetweenRetrieval;
235     }
236
237     /**
238      * Sets the delay between each call to the RI (could be used when multiple
239      * models are accessing various RI to prevent locks because some model are
240      * calling next() all the time...).
241      *
242      * @param p_delayBetweenRetrieval in ms.
243      */

244     public void setDelayBetweenRetrieval( int p_delayBetweenRetrieval ) {
245         _delayBetweenRetrieval = p_delayBetweenRetrieval;
246     }
247
248     // *************************************************************************
249
// ***************************** Action Methods ****************************
250
// *************************************************************************
251

252     /**
253      *
254      * @param p_rowIterator should not be null... Used by the background loading
255      * thread to add rows to the model.
256      */

257     public synchronized void addRows( Iterator JavaDoc p_rowIterator ) {
258         int firstInsertedRow = getRowCount();
259
260         while ( p_rowIterator.hasNext() ) {
261             _rows.add( p_rowIterator.next() );
262         }
263
264         // We notify the listeners...
265
fireTableRowsInserted( firstInsertedRow, getRowCount() );
266     }
267
268     /**
269      * Removes all the remaining RI. Usually called when the application is exiting
270      * because, even though a stop is requested, some threads might be sleeping and
271      * the application will quit BEFORE the RI are removed (it might give some
272      * troubles to the server).
273      */

274     public static void cleanUp() {
275         BackgroundLoadingThread.removeAllRemainingRemoteIterator();
276     }
277 }
278
Popular Tags