1 7 8 package org.jdesktop.swing.data; 9 10 import org.jdesktop.swing.event.ProgressEvent; 11 12 import java.io.BufferedReader ; 13 import java.io.InputStream ; 14 import java.io.InputStreamReader ; 15 import java.io.IOException ; 16 17 import java.text.ParseException ; 18 19 import java.util.ArrayList ; 20 import java.util.List ; 21 import java.util.Collections ; 22 23 import javax.swing.SwingUtilities ; 24 25 26 63 64 public class TableModelExtTextLoader extends DataLoader { 65 66 private String columnDelimiter; 67 private boolean firstRowHeader; 68 private int blockSize; 69 70 private int columnCount = 0; 71 private MetaData columnMetaData[]; 72 private List rows; 73 private boolean complete = false; 74 75 80 public TableModelExtTextLoader() { 81 this("\t", false, 50); 82 } 83 84 94 public TableModelExtTextLoader(String columnDelimiter, 95 boolean isFirstRowHeader, 96 int blockIncrementSize) { 97 super(); 98 this.columnDelimiter = columnDelimiter; 99 this.firstRowHeader = isFirstRowHeader; 100 this.blockSize = blockIncrementSize; 101 } 102 103 108 public String getColumnDelimiter() { 109 return columnDelimiter; 110 } 111 112 117 public void setColumnDelimiter(String regex) { 118 this.columnDelimiter = regex; 119 } 120 121 126 public boolean isFirstRowHeader() { 127 return firstRowHeader; 128 } 129 130 135 public void setFirstRowHeader(boolean isFirstRowHeader) { 136 this.firstRowHeader = isFirstRowHeader; 137 } 138 139 144 public int getBlockIncrementSize() { 145 return blockSize; 146 } 147 148 153 public void setBlockIncrementSize(int blockIncrementSize) { 154 this.blockSize = blockIncrementSize; 155 } 156 157 170 public void loadMetaData(Object model, InputStream is) throws IOException { 171 DefaultTableModelExt dataModel = (DefaultTableModelExt)model; 172 BufferedReader reader = new BufferedReader (new InputStreamReader (is)); 173 String firstLine = reader.readLine(); 174 String columns[] = firstLine.split(columnDelimiter); 175 dataModel.setColumnCount(columns.length); 176 if (firstRowHeader) { 177 for(int i = 0; i < columns.length; i++) { 178 MetaData metaData = dataModel.getColumnMetaData(i); 179 metaData.setName(columns[i]); 180 } 181 } 182 reader.close(); 183 } 184 185 188 public void startLoading(final Object model, final InputStream is) { 189 DefaultTableModelExt dataModel = (DefaultTableModelExt)model; 190 columnCount = dataModel.getColumnCount(); 191 columnMetaData = dataModel.getMetaData(); 192 super.startLoading(model, is); 193 } 194 195 207 protected void readData(InputStream is) throws IOException , 208 ConversionException { 209 BufferedReader reader = new BufferedReader (new InputStreamReader (is)); 210 StringBuffer strbuf = new StringBuffer (); 211 if (columnCount == 0) { 212 throw new IOException ("cannot read data when column count is 0"); 213 } 214 215 int colIndex = 0; 216 int rowCount = 0; 217 Object row[] = null; 218 rows = Collections.synchronizedList(new ArrayList ()); 219 220 String line = reader.readLine(); 221 if (firstRowHeader) { 222 line = reader.readLine(); 224 } 225 while (line != null) { 226 String columns[] = line.split(columnDelimiter); 227 row = new Object [columnCount]; 228 for (int i = 0; i < columnCount; i++) { 229 if (i < columns.length) { 230 row[i] = convertToValue(i, columns[i]); 231 } 232 } 233 synchronized (rows) { 234 rows.add(row); 235 rowCount++; 236 if (rowCount % blockSize == 0) { 237 scheduleLoad(); 238 } 239 } 240 line = reader.readLine(); 241 } 242 complete = true; 243 scheduleLoad(); 244 reader.close(); 245 } 246 247 258 protected void loadData(Object model) { 259 DefaultTableModelExt dataModel = (DefaultTableModelExt)model; 260 boolean done = false; 261 synchronized (rows) { 262 dataModel.loadRows(rows); 263 rows.clear(); 264 done = complete; 265 } 266 if (done) { 267 fireProgressEnded(); 269 } 270 } 271 272 private Object convertToValue(int colIndex, String stringValue) throws ConversionException { 273 Object value = null; 274 if (stringValue.length() == 0) { 275 return null; 276 } 277 Converter converter = columnMetaData[colIndex].getConverter(); 278 if (converter == null) { 279 value = stringValue; 281 } else { 282 value = converter.decode(stringValue, columnMetaData[colIndex].getDecodeFormat()); 283 } 284 return value; 285 } 286 } 287 | Popular Tags |