DefaultDataModel.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:3k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: DefaultDataModel.java,v 1.2 2005/10/10 17:01:03 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.binding;
  22. import java.util.ArrayList;
  23. import java.util.HashMap;
  24. import org.jdesktop.binding.metadata.MetaData;
  25. /**
  26.  * Default data model implementation designed to hold a single record of
  27.  * field values.  This class provides storage of the model's values and
  28.  * may be used when there is no underlying data model.
  29.  *
  30.  * @see TableModelExtAdapter
  31.  * @see JavaBeanDataModel
  32.  *
  33.  * @author Amy Fowler
  34.  * @version 1.0
  35.  */
  36. public class DefaultDataModel extends AbstractDataModel {
  37.     private ArrayList fieldNames = new ArrayList();
  38.     private HashMap values = new HashMap();
  39.     private HashMap metaData = new HashMap();
  40.     private HashMap fieldAdapters = new HashMap();
  41.     public DefaultDataModel() {
  42.     }
  43.     public DefaultDataModel(MetaData fieldMetaData[]) {
  44.         for(int i = 0; i < fieldMetaData.length; i++) {
  45.             addField(fieldMetaData[i], null);
  46.         }
  47.     }
  48.     public void addField(MetaData fieldMetaData,
  49.                            Object defaultValue) {
  50.         String name = fieldMetaData.getName();
  51.         addField(fieldMetaData);
  52.         values.put(name, defaultValue);
  53.     }
  54.     public void addField(MetaData fieldMetaData) {
  55.         String name = fieldMetaData.getName();
  56.         fieldNames.add(name); // track order fields were added
  57.         metaData.put(name, fieldMetaData);
  58.     }
  59.     public void removeField(MetaData fieldMetaData) {
  60.         String name = fieldMetaData.getName();
  61.         fieldNames.remove(name);
  62.         metaData.remove(name);
  63.     }
  64.     public String[] getFieldNames() {
  65.         return (String[])fieldNames.toArray(new String[fieldNames.size()]);
  66.     }
  67.     public MetaData getMetaData(String fieldName) {
  68.         return (MetaData)metaData.get(fieldName);
  69.     }
  70.     public int getFieldCount() {
  71.         return metaData.size();
  72.     }
  73.     public Object getValue(String fieldName) {
  74.         return values.get(fieldName);
  75.     }
  76.     protected void setValueImpl(String fieldName, Object value) {
  77.         values.put(fieldName, value);
  78.     }
  79.     public int getRecordCount() {
  80.         return 1;
  81.     }
  82.     public int getRecordIndex() {
  83.         return 0;
  84.     }
  85.     public void setRecordIndex(int index) {
  86.         if (index != 0) {
  87.             throw new IndexOutOfBoundsException("DefaultDataModel contains only 1 record");
  88.         }
  89.     }
  90. }