1 7 package org.jdesktop.swing.util; 8 9 import org.jdesktop.dataset.DataColumn; 10 import org.jdesktop.dataset.DataRow; 11 import org.jdesktop.dataset.DataSet; 12 import org.jdesktop.dataset.DataTable; 13 import org.jdesktop.dataset.adapter.TabularDataModelAdapter; 14 import org.jdesktop.swing.data.MetaData; 15 import org.jdesktop.swing.data.TabularDataModel; 16 17 20 public class DataSetTestSupport { 21 22 public static final String ADAPTEE_SELECTOR_NAME = "adapterSelector"; 23 public static final String ADAPTEE_TABLE_NAME = "filledTable"; 24 25 33 public static DataSet createDefaultDataSet(int rowCount) { 34 return createDataSet(createDefaultMetaData(), rowCount); 35 } 36 37 45 public static DataSet createDataSet(MetaData[] metaData, int rowCount) { 46 DataSet dataSet = createDataSet(metaData); 47 DataTable table = dataSet.getTable(DataSetTestSupport.ADAPTEE_TABLE_NAME); 48 for (int i = 0; i < rowCount; i++) { 49 DataRow row = table.appendRow(); 50 row.setStatus(DataRow.DataRowStatus.UNCHANGED); 52 } 53 54 return dataSet; 55 } 56 57 63 public static DataSet createDefaultDataSet() { 64 DataSet dataSet = new DataSet(); 65 DataTable table = dataSet.createTable(); 66 table.setName(DataSetTestSupport.ADAPTEE_TABLE_NAME); 67 table.createSelector(DataSetTestSupport.ADAPTEE_SELECTOR_NAME); 68 return dataSet; 69 } 70 71 78 79 public static DataSet createDataSet(MetaData[] defaultMetaData) { 80 DataSet dataSet = createDefaultDataSet(); 81 DataTable table = dataSet.getTable(DataSetTestSupport.ADAPTEE_TABLE_NAME); 82 for (int i = 0; i < defaultMetaData.length; i++) { 83 DataColumn column = table.createColumn(defaultMetaData[i].getName()); 84 column.setType(defaultMetaData[i].getElementClass()); 85 86 87 } 88 return dataSet; 89 } 90 91 95 public static MetaData[] createDefaultMetaData() { 96 MetaData[] metas = new MetaData[3]; 97 metas[0] = new MetaData("firstName", String .class, "First Name"); 98 metas[1] = new MetaData("second", Boolean .class, "Has Money"); 99 metas[2] = new MetaData("amount", Number .class, "Total Funds"); 100 return metas; 101 } 102 103 109 public static void fillDefaultTable(DataSet dataSet) { 110 DataTable table = dataSet.getTable(ADAPTEE_TABLE_NAME); 111 int rowCount = table.getRows().size(); 112 for (int i = 0; i < rowCount; i++) { 113 table.setValue(i, "firstName", "text" + i); 114 table.setValue(i, "second", new Boolean (i %2 == 0)); 115 table.setValue(i, "amount", i * 100); 116 table.getRow(i).setStatus(DataRow.DataRowStatus.UNCHANGED); 117 } 118 } 119 120 130 131 public static TabularDataModel createDefaultFilledTabularDataModel(int rows) { 132 MetaData[] metaData = createDefaultMetaData(); 133 DataSet dataSet = DataSetTestSupport.createDataSet(metaData, rows); 134 DataSetTestSupport.fillDefaultTable(dataSet); 135 DataTable table = dataSet.getTable(DataSetTestSupport.ADAPTEE_TABLE_NAME); 136 TabularDataModel model = new TabularDataModelAdapter(table); 137 for (int column = 0; column < metaData.length; column++) { 138 model.getMetaData(metaData[column].getName()).setLabel(metaData[column].getLabel()); 139 } 140 return model; 141 } 142 143 } 144 | Popular Tags |