KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > util > DataSetTestSupport


1 /*
2  * $Id: DataSetTestSupport.java,v 1.3 2005/03/01 13:25:00 kleopatra Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

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 /**
18  * @author Jeanette Winzenburg
19  */

20 public class DataSetTestSupport {
21
22     public static final String JavaDoc ADAPTEE_SELECTOR_NAME = "adapterSelector";
23     public static final String JavaDoc ADAPTEE_TABLE_NAME = "filledTable";
24  
25     /**
26      * creates DataSet with table "ADAPTEE_TABLE_NAME" and
27      * selector ADAPTEE_SELECTOR_NAME. The table has
28      * columns according initialized from the default metaData.
29      * The table has rowCount empty unchanged rows added.
30      *
31      * @return
32      */

33     public static DataSet createDefaultDataSet(int rowCount) {
34         return createDataSet(createDefaultMetaData(), rowCount);
35     }
36     
37     /**
38      * creates DataSet with table "ADAPTEE_TABLE_NAME" and
39      * selector ADAPTEE_SELECTOR_NAME. The table has
40      * columns according initialized from the given metaData.
41      * The table has rowCount empty unchanged rows added.
42      *
43      * @return
44      */

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             // fake a unchanged row
51
row.setStatus(DataRow.DataRowStatus.UNCHANGED);
52         }
53         
54         return dataSet;
55     }
56     
57     /**
58      * creates DataSet with blank table "ADAPTEE_TABLE_NAME" and
59      * selector ADAPTEE_SELECTOR_NAME.
60      *
61      * @return
62      */

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     /**
72      * creates DataSet with table "ADAPTEE_TABLE_NAME" and
73      * selector ADAPTEE_SELECTOR_NAME. The table has
74      * columns according initialized from the given metaData.
75      *
76      * @return
77      */

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     /**
92      *
93      * @return
94      */

95     public static MetaData[] createDefaultMetaData() {
96         MetaData[] metas = new MetaData[3];
97         metas[0] = new MetaData("firstName", String JavaDoc.class, "First Name");
98         metas[1] = new MetaData("second", Boolean JavaDoc.class, "Has Money");
99         metas[2] = new MetaData("amount", Number JavaDoc.class, "Total Funds");
100         return metas;
101     }
102     
103     /**
104      * convenience to fill a default DataSet (as returned by
105      * createDefaultDataSet) with some values.
106      *
107      * @param dataSet
108      */

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 JavaDoc(i %2 == 0));
115             table.setValue(i, "amount", i * 100);
116             table.getRow(i).setStatus(DataRow.DataRowStatus.UNCHANGED);
117         }
118     }
119
120     /**
121      * creates DataSet with table "ADAPTEE_TABLE_NAME" and
122      * selector ADAPTEE_SELECTOR_NAME. The table has
123      * columns according initialized from the default metaData.
124      * The table has rowCount filled unchanged rows added.
125      * Wraps the table into a TabularDataModel with metaData
126      * label set as in defaultMetaData.
127      *
128      * @return
129      */

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