1 package org.bsf.remoteIterator.client.tableModel; 2 3 import org.bsf.remoteIterator.client.RemoteIteratorClient; 4 5 import javax.swing.table.AbstractTableModel ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.Vector ; 9 10 15 public class DefaultRemoteIteratorTableModel extends AbstractTableModel 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 25 private Vector _rows = null; 26 27 31 38 public DefaultRemoteIteratorTableModel() { 39 this( 1000 ); 40 } 41 42 53 public DefaultRemoteIteratorTableModel( RemoteIteratorClient p_remoteIteratorClient ) { 54 this(); 55 56 setRemoteIteratorClient( p_remoteIteratorClient ); 58 59 startLoadingData(); 61 } 62 63 74 public DefaultRemoteIteratorTableModel( int p_estimatedSize ) { 75 super(); 76 77 _rows = new Vector ( p_estimatedSize, p_estimatedSize ); 78 } 79 80 95 public DefaultRemoteIteratorTableModel( RemoteIteratorClient p_remoteIteratorClient, int p_estimatedSize ) { 96 this( p_estimatedSize ); 97 98 setRemoteIteratorClient( p_remoteIteratorClient ); 100 101 startLoadingData(); 103 } 104 105 109 public int getRowCount() { 110 return _rows.size(); 111 } 112 113 public int getColumnCount() { 114 if ( getRowCount() > 0 ) { 115 List aRow = (List ) _rows.get( 0 ); 117 return aRow.size(); 118 } else { 119 return 0; 120 } 121 } 122 123 public Object getValueAt( int p_rowIndex, int p_columnIndex ) { 124 List aRow = (List ) _rows.get( p_rowIndex ); 125 126 return aRow.get( p_columnIndex ); 127 } 128 129 133 public void setRemoteIteratorClient( RemoteIteratorClient p_remoteIteratorClient ) { 134 if ( p_remoteIteratorClient == null ) { 135 throw new IllegalArgumentException ( "Need a non null RemoteIteratorClient..." ); 136 } 137 138 if ( isLoadingData() ) { 140 stopLoadingData(); 141 } 142 143 if ( _rows.size() != 0 ) { 145 _rows.clear(); 146 fireTableDataChanged(); 147 } 148 149 _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 ( "The data have already been loaded..." ); 158 } 159 } 160 161 public void stopLoadingData() { 162 if ( _loadingThread != null ) { 163 _loadingThread.stopLoading(); _loadingThread = null; 166 _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 192 199 public int getDefaultBlockSize() { 200 return _defaultBlockSize; 201 } 202 203 206 public void setDefaultBlockSize( int p_defaultBlockSize ) { 207 _defaultBlockSize = p_defaultBlockSize; 208 } 209 210 216 public int getInitialBlockSize() { 217 return _initialBlockSize; 218 } 219 220 223 public void setInitialBlockSize( int p_initialBlockSize ) { 224 _initialBlockSize = p_initialBlockSize; 225 } 226 227 233 public int getDelayBetweenRetrieval() { 234 return _delayBetweenRetrieval; 235 } 236 237 244 public void setDelayBetweenRetrieval( int p_delayBetweenRetrieval ) { 245 _delayBetweenRetrieval = p_delayBetweenRetrieval; 246 } 247 248 252 257 public synchronized void addRows( Iterator p_rowIterator ) { 258 int firstInsertedRow = getRowCount(); 259 260 while ( p_rowIterator.hasNext() ) { 261 _rows.add( p_rowIterator.next() ); 262 } 263 264 fireTableRowsInserted( firstInsertedRow, getRowCount() ); 266 } 267 268 274 public static void cleanUp() { 275 BackgroundLoadingThread.removeAllRemainingRemoteIterator(); 276 } 277 } 278 | Popular Tags |