KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > dataset > DataRelation


1 /*
2  * $Id: DataRelation.java,v 1.1 2005/02/23 17:51:29 rbair Exp $
3  *
4  * Copyright 2005 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.dataset;
9 import java.beans.PropertyChangeListener JavaDoc;
10 import java.beans.PropertyChangeSupport JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.Collections JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.logging.Logger JavaDoc;
15 import org.jdesktop.dataset.NameGenerator;
16
17 /**
18  *
19  * @author rbair
20  */

21 public class DataRelation {
22     /**
23      * The Logger
24      */

25     private static final Logger JavaDoc LOG = Logger.getLogger(DataRelation.class.getName());
26     
27     //protected for testing
28
protected static final String JavaDoc DEFAULT_NAME_PREFIX = "DataRelation";
29     private static final NameGenerator NAMEGEN = new NameGenerator(DEFAULT_NAME_PREFIX);
30
31     /**
32      * The DataSet that created this DataRelation. This is a readonly property
33      */

34     private DataSet dataSet;
35     /**
36      * The name of the DataRelation.
37      */

38     private String JavaDoc name;
39     /**
40      * The DataColumn from a "parent" DataTable to use in establishing this
41      * relationship. If you need multiple columns in the parent, create a
42      * single calculated column in the parent and use that instead
43      */

44     private DataColumn parentColumn;
45     /**
46      * The DataColumn from a "child" DataTable to use in establishing this
47      * relationship. childColumn cannot equal DataColumn, but it can come
48      * from the same table.
49      */

50     private DataColumn childColumn;
51     
52     private PropertyChangeSupport JavaDoc pcs = new PropertyChangeSupport JavaDoc(this);
53     
54     /**
55      * Create a new DataRelation
56      */

57     protected DataRelation(DataSet ds) {
58         assert ds != null;
59         this.dataSet = ds;
60         name = NAMEGEN.generateName(this);
61     }
62     
63     /**
64      * @return the DataSet that this DataRelation belongs to
65      */

66     public DataSet getDataSet() {
67         return dataSet;
68     }
69     
70     /**
71      * Set the name of the DataRelation
72      * @param name
73      */

74     public void setName(String JavaDoc name) {
75         if (this.name != name) {
76             assert DataSetUtils.isValidName(name);
77             assert !dataSet.hasElement(name);
78             String JavaDoc oldName = this.name;
79             this.name = name;
80             pcs.firePropertyChange("name", oldName, name);
81         }
82     }
83
84     /**
85      * @returns the name of this DataRelation
86      */

87     public String JavaDoc getName() {
88         return name;
89     }
90
91     /**
92      * @returns the DataColumn that is the parent in this parent/child relation
93      */

94     public DataColumn getParentColumn() {
95         return parentColumn;
96     }
97
98     /**
99      * Sets the DataColumn that is the parent in this parent/child relation. The
100      * value in the parentColumn for a specified range of field indices will be
101      * used to generate the list of child Rows.
102      * @param parentColumn
103      */

104     public void setParentColumn(DataColumn parentColumn) {
105         if (this.parentColumn != parentColumn) {
106             assert parentColumn != this.childColumn;
107             DataColumn oldValue = this.parentColumn;
108             this.parentColumn = parentColumn;
109             pcs.firePropertyChange("parentColumn", oldValue, parentColumn);
110         }
111     }
112
113     /**
114      * @returns the child DataColumn in this parent/child relation
115      */

116     public DataColumn getChildColumn() {
117         return childColumn;
118     }
119
120     /**
121      * sets the child DataColumn in this parent/child relation
122      * @param childColumn
123      */

124     public void setChildColumn(DataColumn childColumn) {
125         if (this.childColumn != childColumn) {
126             assert childColumn != this.parentColumn;
127             DataColumn oldValue = this.childColumn;
128             this.childColumn = childColumn;
129             pcs.firePropertyChange("childColumn", oldValue, childColumn);
130         }
131     }
132     
133     /**
134      * Given a DataRow from the parent DataTable, return a list of
135      * related DataRows from the child DataTable.
136      * @param parentRow
137      */

138     public List JavaDoc<DataRow> getRows(DataRow parentRow) {
139         //return an empty list if I don't have enough info to produce any
140
//child rows. Short circuits this method
141
if (parentColumn == null || childColumn == null || parentRow == null) {
142             return Collections.unmodifiableList(Collections.EMPTY_LIST);
143         }
144         
145         //make sure that this DataRow is from the parent DataTable!
146
assert parentRow.getTable().equals(parentColumn.getTable());
147         
148         DataTable childTable = childColumn.getTable();
149         Object JavaDoc parentKey = parentColumn.getTable().getValue(parentRow, parentColumn);
150         List JavaDoc<DataRow> rows = new ArrayList JavaDoc<DataRow>();
151         for (DataRow childRow : childTable.getRows()) {
152             Object JavaDoc childKey = childTable.getValue(childRow, childColumn);
153             if (parentKey != null && childKey != null && parentKey.equals(childKey)) {
154                 rows.add(childRow);
155             }
156         }
157         return Collections.unmodifiableList(rows);
158     }
159
160     /**
161      * Given the index of a row in the parent DataTable, produce a corrosponding
162      * list of related rows from the child DataTable
163      *
164      * @param parentRowIndex
165      */

166     public List JavaDoc<DataRow> getRows(int parentRowIndex) {
167         if (parentColumn == null || childColumn == null || parentRowIndex < 0) {
168             return Collections.unmodifiableList(Collections.EMPTY_LIST);
169         }
170         //NOTE: No need to check the parentRowIndex for an upper out of bounds
171
//condition because the table will do so in the parentTable.getRow()
172
//method call.
173
DataTable parentTable = parentColumn.getTable();
174         return getRows(parentTable.getRow(parentRowIndex));
175     }
176     
177     /**
178      * Given an array of DataRows, produce the union of the results for each
179      * DataRow from the child DataTable
180      *
181      * @param parentRows
182      */

183     public List JavaDoc<DataRow> getRows(DataRow[] parentRows) {
184         List JavaDoc<DataRow> rows = new ArrayList JavaDoc<DataRow>();
185         for (DataRow parentRow : parentRows) {
186             rows.addAll(getRows(parentRow));
187         }
188         return Collections.unmodifiableList(rows);
189     }
190     
191     /**
192      * Given an array if parent row indices, produce the union of the results
193      * for each index from the child DataTable
194      *
195      * @param parentRowIndices
196      */

197     public List JavaDoc<DataRow> getRows(int[] parentRowIndices) {
198         //short circuit the method if I don't have enough info to produce proper
199
//results
200
if (parentColumn == null || childColumn == null || parentRowIndices == null) {
201             return Collections.unmodifiableList(Collections.EMPTY_LIST);
202         }
203         
204         DataTable parentTable = parentColumn.getTable();
205         DataRow[] parentRows = new DataRow[parentRowIndices.length];
206         for (int i=0; i<parentRows.length; i++) {
207             parentRows[i] = parentTable.getRow(parentRowIndices[i]);
208         }
209         return getRows(parentRows);
210     }
211     
212     /**
213      * Given a List of indices, produce the union of the results for each index
214      * from the child DataTable
215      *
216      * @param parentRowIndices
217      */

218     public List JavaDoc<DataRow> getRows(List JavaDoc<Integer JavaDoc> parentRowIndices) {
219         //short circuit this method if I don't have enough info
220
if (parentColumn == null || childColumn == null || parentRowIndices == null) {
221             return Collections.unmodifiableList(Collections.EMPTY_LIST);
222         }
223         
224         DataTable parentTable = parentColumn.getTable();
225         DataRow[] parentRows = new DataRow[parentRowIndices.size()];
226         for (int i=0; i<parentRows.length; i++) {
227             parentRows[i] = parentTable.getRow(parentRowIndices.get(i));
228         }
229         return getRows(parentRows);
230     }
231
232     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
233         pcs.addPropertyChangeListener(listener);
234     }
235     
236     public void addPropertyChangeListener(String JavaDoc property, PropertyChangeListener JavaDoc listener) {
237         pcs.addPropertyChangeListener(property, listener);
238     }
239     
240     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
241         pcs.removePropertyChangeListener(listener);
242     }
243     
244     public void removePropertyChangeListener(String JavaDoc propertyName, PropertyChangeListener JavaDoc listener) {
245         pcs.removePropertyChangeListener(propertyName, listener);
246     }
247 }
Popular Tags