KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > data > TestDataModelFactory


1 /*
2  * $Id: TestDataModelFactory.java,v 1.2 2004/12/17 14:40:34 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.data;
8
9 import java.util.Locale JavaDoc;
10
11 /**
12  * static factory creating DataModels for test cases.
13  *
14  * @author Jeanette Winzenburg
15  */

16 public class TestDataModelFactory {
17
18     public static String JavaDoc statusTypes[] = {
19         "new", "existing", "cancelled"
20     };
21     public static String JavaDoc states[] = {
22     "Alabama", "Alaska", "Arizona", "Arkansas",
23     "California", "Colorado", "Connecticut",
24     "Delaware", "Florida", "Georgia", "Hawaii",
25     "Idaho", "Illinois", "Indiana", "Iowa",
26     "Kansas", "Kentucky", "Louisiana",
27     "Maine", "Mariland", "Massachusetts", "Michegan",
28     "Minnesota", "Mississippi", "Missouri", "Montana",
29     "Nebraska", "Nevada", "New Hampshire", "New Jersey",
30     "New Mexico", "New York", "North Carolina", "North Dakota",
31     "Ohio", "Oklahoma", "Oregan", "Pennsylvania",
32     "Rhode Island", "South Carolina", "South Dakota",
33     "Tennessee", "Texas", "Utah", "Vermont", "Virgina",
34     "Washington", "West Virginia", "Wisconsin", "Wyoming" };
35
36     /**
37      * returns a one-field dataModel with a Validator.
38      *
39      * @param valid the immutable return value of the Validator.
40      * @return
41      */

42     public static DataModel createDataModelWithValidator(boolean valid) {
43         DefaultDataModel model = new DefaultDataModel();
44         model.addField(new MetaData("simpleton"));
45         model.addValidator(createValidator(valid));
46         return model;
47     }
48
49     /**
50      * returns a Validator with a fixed validation restult.
51      *
52      * @param valid the fixed validation result.
53      * @return
54      */

55     public static Validator createValidator(final boolean valid) {
56         
57         Validator v = new Validator() {
58
59             public boolean validate(Object JavaDoc value, Locale JavaDoc locale, String JavaDoc[] error) {
60                 return valid;
61             }
62             
63         };
64         return v;
65     }
66
67     public static DefaultDataModel createPersonModel() {
68         DefaultDataModel personModel = new DefaultDataModel();
69     
70         StringMetaData stringMetaData = new StringMetaData("firstname", "First Name");
71         stringMetaData.setMinValueCount(1); // required
72
stringMetaData.setDisplayWidth(14);
73         stringMetaData.setMinLength(1);
74         stringMetaData.setMaxLength(24);
75         personModel.addField(stringMetaData);
76     
77         stringMetaData = new StringMetaData("middleinitial", "Middle Initial");
78         stringMetaData.setDisplayWidth(2);
79         stringMetaData.setMinLength(1);
80         stringMetaData.setMaxLength(2);
81         personModel.addField(stringMetaData);
82     
83         stringMetaData = new StringMetaData("lastname", "Last Name");
84         stringMetaData.setMinValueCount(1); // required
85
stringMetaData.setDisplayWidth(14);
86         stringMetaData.setMinLength(1);
87         stringMetaData.setMaxLength(24);
88         personModel.addField(stringMetaData);
89     
90         NumberMetaData numberMetaData = new NumberMetaData("age", Integer JavaDoc.class,
91             "Age");
92         numberMetaData.setMinimum(new Integer JavaDoc(0));
93         numberMetaData.setMaximum(new Integer JavaDoc(110));
94         personModel.addField(numberMetaData);
95     
96         MetaData metaData = new MetaData("married", Boolean JavaDoc.class, "Married");
97         personModel.addField(metaData);
98     
99         numberMetaData = new NumberMetaData("customerid", Integer JavaDoc.class, "Customer ID");
100         numberMetaData.setReadOnly(true);
101         personModel.addField(numberMetaData, new Integer JavaDoc(112));
102     
103         EnumeratedMetaData enumMetaData = new EnumeratedMetaData("status", String JavaDoc.class, "Status");
104         enumMetaData.setEnumeration(statusTypes);
105         personModel.addField(enumMetaData, "new");
106     
107         stringMetaData = new StringMetaData("comments", "Comments");
108         stringMetaData.setMaxLength(100);
109         stringMetaData.setMultiLine(true);
110         personModel.addField(stringMetaData);
111     
112         return personModel;
113     
114     }
115
116     public static DataModel createFilledPersonModel(boolean valid) {
117         DataModel model = createPersonModel();
118         model.setValue("firstname", "Melissa");
119         model.setValue("lastname", "Etheridge");
120         if (valid) {
121             model.setValue("age", new Integer JavaDoc(46));
122         } else {
123             model.setValue("age", new Integer JavaDoc(-2));
124         }
125         model.setValue("married", Boolean.TRUE);
126         return model;
127     }
128
129     public static DefaultDataModel createAddressModel() {
130         DefaultDataModel addressModel = new DefaultDataModel();
131     
132         StringMetaData metaData = new StringMetaData("street", "Street");
133         metaData.setMinValueCount(1);
134         metaData.setDisplayWidth(18);
135         metaData.setMaxLength(32);
136         addressModel.addField(metaData);
137     
138         metaData = new StringMetaData("city", "City");
139         metaData.setMinValueCount(1);
140         metaData.setDisplayWidth(16);
141         metaData.setMaxLength(24);
142         addressModel.addField(metaData);
143     
144         EnumeratedMetaData enumMetaData =
145             new EnumeratedMetaData("state", String JavaDoc.class, "State");
146         enumMetaData.setEnumeration(states);
147         enumMetaData.setMinValueCount(1);
148         addressModel.addField(enumMetaData);
149     
150         metaData =
151             new StringMetaData("zipcode", "Zipcode");
152         metaData.setMinLength(5);
153         metaData.setMaxLength(9);
154         metaData.setMinValueCount(1);
155         addressModel.addField(metaData);
156     
157         return addressModel;
158     }
159
160     public static DefaultDataModel createNestedPersonModel() {
161         DefaultDataModel personModel = createPersonModel();
162         MetaData metaData = new MetaData("address", DataModel.class, "Address");
163         personModel.addField(metaData, createAddressModel());
164         return personModel;
165     }
166
167
168 }
169
Popular Tags