KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.bsf.remoteIterator.client.tableModel;
2
3 import org.bsf.remoteIterator.client.RemoteIteratorClient;
4
5 import java.rmi.RemoteException JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.Vector JavaDoc;
8
9 /**
10  * A thread to load the data in the background, using a RemoteIteratorClient.
11  *
12  * @see DefaultRemoteIteratorTableModel
13  */

14 public class BackgroundLoadingThread extends Thread JavaDoc {
15     private static Vector JavaDoc _instantiatedThreads = new Vector JavaDoc( 20, 10 );
16
17     private RemoteIteratorClient _remoteIteratorClient = null;
18     private DefaultRemoteIteratorTableModel _defaultRemoteIteratorTableModel = null;
19
20     private boolean _shouldStop = false;
21     private boolean _isFirstCall = true;
22
23     // *************************************************************************
24
// ****************************** Constructors *****************************
25
// *************************************************************************
26

27     /**
28      * Instantiates a Thread that will, using the provided RemoteIteratorClient,
29      * retrieve the data and insert it in the given DefaultRemoteIteratorTableModel.
30      *
31      * @param p_remoteIteratorClient The RIClient to use to retrieve the data.
32      * @param p_tableModel The DefaultRemoteIteratorTableModel in which the data
33      * will be inserted as they arrived.
34      */

35     public BackgroundLoadingThread( RemoteIteratorClient p_remoteIteratorClient,
36                                     DefaultRemoteIteratorTableModel p_tableModel ) {
37         super();
38
39         _remoteIteratorClient = p_remoteIteratorClient;
40         _defaultRemoteIteratorTableModel = p_tableModel;
41
42         // We add it to the map of instantiated threads... For later cleanup...
43
_instantiatedThreads.add( this );
44     }
45
46     // *************************************************************************
47
// ********************** Overriden Thread Methods *************************
48
// *************************************************************************
49

50     public void run() {
51         while ( !_shouldStop ) {
52             // We retrieve the block size for this call (a different one for the
53
// first call to have a quick display of data...)
54
int blockSize = _defaultRemoteIteratorTableModel.getDefaultBlockSize();
55
56             if ( _isFirstCall ) {
57                 blockSize = _defaultRemoteIteratorTableModel.getInitialBlockSize();
58
59                 // Not the first call anymore
60
_isFirstCall = false;
61             }
62
63             try {
64                 // Retrieving the rows
65
Iterator JavaDoc iterator = _remoteIteratorClient.next( blockSize );
66
67                 // Inserting them in the TableModel
68
if ( _defaultRemoteIteratorTableModel != null ) {
69                     _defaultRemoteIteratorTableModel.addRows( iterator );
70                 }
71
72                 // Do we have to go on ? (ie. Have we reached the last row ?)
73
if ( _remoteIteratorClient.isLast() ) {
74                     _defaultRemoteIteratorTableModel.stopLoadingData();
75                 } else {
76                     try {
77                         // We sleep the given time (if > 0)
78
int timeToSleepInMs = _defaultRemoteIteratorTableModel.getDelayBetweenRetrieval();
79
80                         if ( timeToSleepInMs > 0 ) {
81                             Thread.sleep( timeToSleepInMs );
82                         }
83                     } catch( InterruptedException JavaDoc e ) {
84                         // Too bad... Should not happen though... Nothing to do...
85
}
86                 }
87             } catch( RemoteException JavaDoc e ) {
88                 // Nothing can be done... Better luck next time ?
89
}
90         }
91
92         // We remove the RI since all the data have been loaded or the loading was stopped
93
removeRemoteIterator();
94     }
95
96     // *************************************************************************
97
// **************************** Action Methods *****************************
98
// *************************************************************************
99

100     public void stopLoading() {
101         // We tell the Thread that this is the end (my friend...)
102
_shouldStop = true;
103
104         // And we don't want to go on notify the model....
105
_defaultRemoteIteratorTableModel = null;
106     }
107
108     public synchronized void removeRemoteIterator() {
109         // Should have been called before (except when quitting the application
110
// and that this thread is sleeping...
111
stopLoading();
112
113         try {
114             if ( _remoteIteratorClient != null ) {
115                 // We remove the RI and we clear the ref to the client
116
_remoteIteratorClient.remove();
117                 _remoteIteratorClient = null;
118
119                 // This thread has no more active RI
120
_instantiatedThreads.remove( this );
121             }
122         } catch( RemoteException JavaDoc e ) {
123             // We did what we could...
124
}
125     }
126
127     /**
128      * This method is called by the model when the static cleanUp method is called
129      * to prevent phantom RIs (application exited with sleeping threads...).
130      */

131     public static void removeAllRemainingRemoteIterator() {
132         Iterator JavaDoc iterator = _instantiatedThreads.iterator();
133
134         while ( iterator.hasNext() ) {
135             try {
136                 ( (BackgroundLoadingThread) iterator.next() ).removeRemoteIterator();
137             } catch( Exception JavaDoc e ) {
138                 // We don't care !!! Clean up time...
139
}
140         }
141     }
142 }
143
Popular Tags