KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: DefaultDataModel.java,v 1.2 2005/01/27 14:40:25 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
8 package org.jdesktop.swing.data;
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.List JavaDoc;
12 import java.util.HashMap JavaDoc;
13
14
15 /**
16  * Default data model implementation designed to hold a single record of
17  * field values. This class provides storage of the model's values and
18  * may be used when there is no underlying data model.
19  *
20  * @see TableModelExtAdapter
21  * @see JavaBeanDataModel
22  *
23  * @author Amy Fowler
24  * @version 1.0
25  */

26
27 public class DefaultDataModel extends AbstractDataModel {
28     private ArrayList JavaDoc fieldNames = new ArrayList JavaDoc();
29     private HashMap JavaDoc values = new HashMap JavaDoc();
30     private HashMap JavaDoc metaData = new HashMap JavaDoc();
31     private HashMap JavaDoc fieldAdapters = new HashMap JavaDoc();
32
33     public DefaultDataModel() {
34     }
35
36     public DefaultDataModel(MetaData fieldMetaData[]) {
37         for(int i = 0; i < fieldMetaData.length; i++) {
38             addField(fieldMetaData[i], null);
39         }
40     }
41
42     public void addField(MetaData fieldMetaData,
43                            Object JavaDoc defaultValue) {
44         String JavaDoc name = fieldMetaData.getName();
45         addField(fieldMetaData);
46         values.put(name, defaultValue);
47     }
48
49     public void addField(MetaData fieldMetaData) {
50         String JavaDoc name = fieldMetaData.getName();
51         fieldNames.add(name); // track order fields were added
52
metaData.put(name, fieldMetaData);
53     }
54
55     public void removeField(MetaData fieldMetaData) {
56         String JavaDoc name = fieldMetaData.getName();
57         fieldNames.remove(name);
58         metaData.remove(name);
59     }
60
61     public String JavaDoc[] getFieldNames() {
62         return (String JavaDoc[])fieldNames.toArray(new String JavaDoc[fieldNames.size()]);
63     }
64
65     public MetaData getMetaData(String JavaDoc fieldName) {
66         return (MetaData)metaData.get(fieldName);
67     }
68
69     public int getFieldCount() {
70         return metaData.size();
71     }
72
73     public Object JavaDoc getValue(String JavaDoc fieldName) {
74         return values.get(fieldName);
75     }
76
77     protected void setValueImpl(String JavaDoc fieldName, Object JavaDoc value) {
78         values.put(fieldName, value);
79     }
80
81     public int getRecordCount() {
82         return 1;
83     }
84
85     public int getRecordIndex() {
86         return 0;
87     }
88
89     public void setRecordIndex(int index) {
90         if (index != 0) {
91             throw new IndexOutOfBoundsException JavaDoc("DefaultDataModel contains only 1 record");
92         }
93     }
94
95
96 }
97
Popular Tags