KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: AbstractDataModel.java,v 1.3 2004/11/22 16:05:15 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 org.jdesktop.swing.data.MetaData;
11 import org.jdesktop.swing.data.Validator;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.List JavaDoc;
16
17 /**
18  * Abstract base class for implementing concrete DataModel implementations.
19  * This class provides support for managing validators and value change listeners.
20  * Subclasses must implement their own mechanism to store field meta-data and
21  * values.
22  *
23  * @author Amy Fowler
24  * @version 1.0
25  */

26 public abstract class AbstractDataModel implements DataModel {
27
28     protected ArrayList JavaDoc validators;
29     private ArrayList JavaDoc valueChangeListeners;
30     private HashMap JavaDoc valueChangeEvents;
31
32     public abstract String JavaDoc[] getFieldNames();
33
34     public MetaData[] getMetaData() {
35         String JavaDoc fieldNames[] = getFieldNames();
36         MetaData metaData[] = new MetaData[fieldNames.length];
37         for(int i = 0; i < fieldNames.length; i++) {
38             metaData[i] = getMetaData(fieldNames[i]);
39         }
40         return metaData;
41     }
42
43     public abstract MetaData getMetaData(String JavaDoc fieldName);
44
45     public abstract Object JavaDoc getValue(String JavaDoc fieldName);
46
47     public void setValue(String JavaDoc fieldName, Object JavaDoc value) {
48         Object JavaDoc oldValue = getValue(fieldName);
49         setValueImpl(fieldName, value);
50         if ((oldValue != null && !oldValue.equals(value)) ||
51             (oldValue == null && value != null)) {
52             fireValueChanged(fieldName);
53         }
54     }
55
56     protected abstract void setValueImpl(String JavaDoc fieldName, Object JavaDoc value);
57
58     public void addValidator(Validator validator) {
59         if (validators == null) {
60             validators = new ArrayList JavaDoc();
61         }
62         validators.add(validator);
63
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
77     public void addValueChangeListener(ValueChangeListener l) {
78         if (valueChangeListeners == null) {
79             valueChangeListeners = new ArrayList JavaDoc();
80         }
81         valueChangeListeners.add(l);
82     }
83
84     public void removeValueChangeListener(ValueChangeListener l) {
85         if (valueChangeListeners != null){
86             valueChangeListeners.remove(l);
87         }
88     }
89
90     public ValueChangeListener[] getValueChangeListeners() {
91         if (valueChangeListeners != null) {
92             return (ValueChangeListener[])valueChangeListeners.toArray(new ValueChangeListener[1]);
93         }
94         return new ValueChangeListener[0];
95     }
96
97     protected void fireValueChanged(String JavaDoc fieldName) {
98         ValueChangeListener[] formListeners = getValueChangeListeners();
99         ValueChangeEvent e = getCachedEvent(fieldName);
100         if (valueChangeListeners != null) {
101             for (int i = 0; i < valueChangeListeners.size(); i++) {
102                 ValueChangeListener vcl = (ValueChangeListener)
103                     valueChangeListeners.get(i);
104                 vcl.valueChanged(e);
105             }
106         }
107     }
108
109     private ValueChangeEvent getCachedEvent(String JavaDoc fieldName) {
110         if (valueChangeEvents == null) {
111             valueChangeEvents = new HashMap JavaDoc();
112         }
113         ValueChangeEvent event = (ValueChangeEvent)valueChangeEvents.get(fieldName);
114         if (event == null) {
115             event = new ValueChangeEvent(this, fieldName);
116             valueChangeEvents.put(fieldName, event);
117         }
118         return event;
119     }
120
121
122 }
123
Popular Tags