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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: AbstractDataModel.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. import org.jdesktop.binding.metadata.Validator;
  26. /**
  27.  * Abstract base class for implementing concrete DataModel implementations.
  28.  * This class provides support for managing validators and value change listeners.
  29.  * Subclasses must implement their own mechanism to store field meta-data and
  30.  * values.
  31.  *
  32.  * @author Amy Fowler
  33.  * @version 1.0
  34.  */
  35. public abstract class AbstractDataModel implements DataModel {
  36.     protected ArrayList validators;
  37.     private ArrayList valueChangeListeners;
  38.     private HashMap valueChangeEvents;
  39.     public abstract String[] getFieldNames();
  40.     public MetaData[] getMetaData() {
  41.         String fieldNames[] = getFieldNames();
  42.         MetaData metaData[] = new MetaData[fieldNames.length];
  43.         for(int i = 0; i < fieldNames.length; i++) {
  44.             metaData[i] = getMetaData(fieldNames[i]);
  45.         }
  46.         return metaData;
  47.     }
  48.     public abstract MetaData getMetaData(String fieldName);
  49.     public abstract Object getValue(String fieldName);
  50.     public void setValue(String fieldName, Object value) {
  51.         Object oldValue = getValue(fieldName);
  52.         setValueImpl(fieldName, value);
  53.         if ((oldValue != null && !oldValue.equals(value)) ||
  54.             (oldValue == null && value != null)) {
  55.             fireValueChanged(fieldName);
  56.         }
  57.     }
  58.     protected abstract void setValueImpl(String fieldName, Object value);
  59.     public void addValidator(Validator validator) {
  60.         if (validators == null) {
  61.             validators = new ArrayList();
  62.         }
  63.         validators.add(validator);
  64.     }
  65.     public void removeValidator(Validator validator) {
  66.         if (validators != null) {
  67.             validators.remove(validator);
  68.         }
  69.     }
  70.     public Validator[] getValidators() {
  71.         if (validators != null) {
  72.             return (Validator[]) validators.toArray(new Validator[validators.size()]);
  73.         }
  74.         return new Validator[0];
  75.     }
  76.     public void addValueChangeListener(ValueChangeListener l) {
  77.         if (valueChangeListeners == null) {
  78.             valueChangeListeners = new ArrayList();
  79.         }
  80.         valueChangeListeners.add(l);
  81.     }
  82.     public void removeValueChangeListener(ValueChangeListener l) {
  83.         if (valueChangeListeners != null){
  84.             valueChangeListeners.remove(l);
  85.         }
  86.     }
  87.     public ValueChangeListener[] getValueChangeListeners() {
  88.         if (valueChangeListeners != null) {
  89.             return (ValueChangeListener[])valueChangeListeners.toArray(new ValueChangeListener[1]);
  90.         }
  91.         return new ValueChangeListener[0];
  92.     }
  93.     protected void fireValueChanged(String fieldName) {
  94.         ValueChangeListener[] formListeners = getValueChangeListeners();
  95.         ValueChangeEvent e = getCachedEvent(fieldName);
  96.         if (valueChangeListeners != null) {
  97.             for (int i = 0; i < valueChangeListeners.size(); i++) {
  98.                 ValueChangeListener vcl = (ValueChangeListener)
  99.                     valueChangeListeners.get(i);
  100.                 vcl.valueChanged(e);
  101.             }
  102.         }
  103.     }
  104.     private ValueChangeEvent getCachedEvent(String fieldName) {
  105.         if (valueChangeEvents == null) {
  106.             valueChangeEvents = new HashMap();
  107.         }
  108.         ValueChangeEvent event = (ValueChangeEvent)valueChangeEvents.get(fieldName);
  109.         if (event == null) {
  110.             event = new ValueChangeEvent(this, fieldName);
  111.             valueChangeEvents.put(fieldName, event);
  112.         }
  113.         return event;
  114.     }
  115. }