KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: DefaultMetaDataProvider.java,v 1.1 2004/12/16 17:05:49 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 package org.jdesktop.swing.data;
8
9 import java.util.*;
10
11
12 /**
13  * @author Jeanette Winzenburg
14  */

15 public class DefaultMetaDataProvider implements MetaDataProvider {
16
17   private List fieldNames;
18   private Map metaData;
19
20   public DefaultMetaDataProvider() {
21     
22   }
23   public DefaultMetaDataProvider(MetaDataProvider provider) {
24     this(provider.getMetaData());
25   }
26
27   public DefaultMetaDataProvider(MetaData[] metaDatas) {
28     
29     setMetaData(metaDatas);
30 }
31 //------------------------ metaDataProvider
32
public MetaData[] getMetaData() {
33     MetaData[] metaData = new MetaData[getFieldCount()];
34     for (int i = 0; i < metaData.length; i++) {
35       metaData[i] = getMetaData(getFieldName(i));
36     }
37     return metaData;
38   }
39
40   public MetaData getMetaData(String JavaDoc dataID) {
41     return (MetaData) getMetaDataMap().get(dataID);
42   }
43
44 //-------------------------- convenience accessing
45
public String JavaDoc[] getFieldNames() {
46     return (String JavaDoc[]) getFieldNameList().toArray(new String JavaDoc[getFieldCount()]);
47   }
48
49   public int getFieldCount() {
50     return getFieldNameList().size();
51   }
52
53   /**
54    * PRE: 0 <= index < getFieldCount()
55    * @param index
56    * @return
57    */

58   public String JavaDoc getFieldName(int index) {
59     return (String JavaDoc) getFieldNameList().get(index);
60   }
61
62   public int getFieldIndex(String JavaDoc fieldName) {
63     for (int i = 0; i < getFieldCount(); i++) {
64       if (fieldName.equals(getFieldName(i))) {
65         return i;
66       }
67     }
68     return -1;
69   }
70
71   public boolean hasField(String JavaDoc fieldName) {
72     return getFieldNameList().contains(fieldName);
73   }
74
75 //-------------------------- mutating (use on init mostly)
76
public void setMetaData(MetaData[] metaData) {
77     clear();
78     for (int i = 0; i < metaData.length; i++) {
79       addField(metaData[i]);
80     }
81   }
82
83   public void setMetaData(List metaData) {
84     clear();
85     for (Iterator iter = metaData.iterator(); iter.hasNext(); ) {
86       addField((MetaData) iter.next());
87     }
88   }
89
90   public void addField(MetaData data) {
91     getFieldNameList().add(data.getName());
92     getMetaDataMap().put(data.getName(), data);
93   }
94
95   public void clear() {
96     fieldNames = null;
97     metaData = null;
98   }
99
100 //-------------------------- helper
101
private List getFieldNameList() {
102     if (fieldNames == null) {
103       fieldNames = new ArrayList();
104     }
105     return fieldNames;
106   }
107
108   private Map getMetaDataMap() {
109     if (metaData == null) {
110       metaData = new HashMap();
111     }
112     return metaData;
113   }
114 }
115
Popular Tags