1 7 8 package org.jdesktop.swing.data; 9 10 import java.util.ArrayList ; 11 import java.util.List ; 12 import java.util.HashMap ; 13 14 15 26 27 public class DefaultDataModel extends AbstractDataModel { 28 private ArrayList fieldNames = new ArrayList (); 29 private HashMap values = new HashMap (); 30 private HashMap metaData = new HashMap (); 31 private HashMap fieldAdapters = new HashMap (); 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 defaultValue) { 44 String name = fieldMetaData.getName(); 45 addField(fieldMetaData); 46 values.put(name, defaultValue); 47 } 48 49 public void addField(MetaData fieldMetaData) { 50 String name = fieldMetaData.getName(); 51 fieldNames.add(name); metaData.put(name, fieldMetaData); 53 } 54 55 public void removeField(MetaData fieldMetaData) { 56 String name = fieldMetaData.getName(); 57 fieldNames.remove(name); 58 metaData.remove(name); 59 } 60 61 public String [] getFieldNames() { 62 return (String [])fieldNames.toArray(new String [fieldNames.size()]); 63 } 64 65 public MetaData getMetaData(String fieldName) { 66 return (MetaData)metaData.get(fieldName); 67 } 68 69 public int getFieldCount() { 70 return metaData.size(); 71 } 72 73 public Object getValue(String fieldName) { 74 return values.get(fieldName); 75 } 76 77 protected void setValueImpl(String fieldName, Object 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 ("DefaultDataModel contains only 1 record"); 92 } 93 } 94 95 96 } 97 | Popular Tags |