KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: DataRow.java,v 1.1 2005/02/23 17:51:30 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.Collection JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Map JavaDoc;
14
15
16 /**
17  *
18  * @author rbair
19  */

20 public class DataRow {
21     /**
22      * Flag indicating the status of the DataRow
23      */

24     public enum DataRowStatus {INSERTED, DELETED, UPDATED, UNCHANGED};
25     
26     //used for communicating changes to this JavaBean, especially necessary for
27
//IDE tools, but also handy for any other component listening to this row
28
private PropertyChangeSupport JavaDoc pcs = new PropertyChangeSupport JavaDoc(this);
29
30     /**
31      * The DataTable that created this DataRow. This is an immutable property
32      */

33     private DataTable table;
34     /**
35      * The status of this DataRow. By default, it is set to INSERTED. It is
36      * possible to change the status manually, although during certain
37      * lifecycle events it is automatically changed, such as after the data
38      * is saved to disk, or a change is made.
39      */

40     private DataRowStatus status = DataRowStatus.INSERTED;
41     /**
42      * The data associated with this Row. This structure implies that when a
43      * DataColumn is removed from the DataTable, then each row will have to be
44      * traversed and the DataColumn removed from the Map, along with the cell
45      */

46     private Map JavaDoc<DataColumn,DataCell> cells = new HashMap JavaDoc<DataColumn,DataCell>();
47     
48     /**
49      * Create a new DataRow. The table creating this row must be passed in
50      */

51     protected DataRow(DataTable table) {
52         assert table != null;
53         this.table = table;
54         
55         //construct the cells based on the columns in the table
56
//add a cell for each column
57
for (DataColumn c : this.table.getColumns()) {
58             DataCell cell = new DataCell();
59             cell.value = c.getDefaultValue();
60             cells.put(c, cell);
61         }
62     }
63     
64     /**
65      * Sets the given value to the column with the given name
66      *
67      * @param colName
68      * @param value
69      */

70     public void setValue(String JavaDoc colName, Object JavaDoc value) {
71         DataColumn col = table.getColumn(colName);
72         assert col != null;
73         DataCell cell = cells.get(col);
74         cell.setValue(value);
75         if (status == DataRowStatus.UNCHANGED && cell.changed) {
76             setStatus(DataRowStatus.UPDATED);
77         }
78     }
79     
80     /**
81      * @return the value at the given column name
82      */

83     public Object JavaDoc getValue(String JavaDoc colName) {
84         return getValue(table.getColumn(colName));
85     }
86     
87     public Object JavaDoc getValue(DataColumn col) {
88         return cells.get(col).value;
89     }
90     
91     public DataTable getTable() {
92         return table;
93     }
94
95     public DataRowStatus getStatus() {
96         return status;
97     }
98
99     /**
100      * @param status
101      */

102     public void setStatus(DataRowStatus status) {
103         if (this.status != status) {
104             DataRowStatus oldValue = this.status;
105             this.status = status;
106             pcs.firePropertyChange("status", oldValue, status);
107         }
108     }
109     
110     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
111         pcs.addPropertyChangeListener(listener);
112     }
113     
114     public void addPropertyChangeListener(String JavaDoc property, PropertyChangeListener JavaDoc listener) {
115         pcs.addPropertyChangeListener(property, listener);
116     }
117     
118     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
119         pcs.removePropertyChangeListener(listener);
120     }
121     
122     public void removePropertyChangeListener(String JavaDoc propertyName, PropertyChangeListener JavaDoc listener) {
123         pcs.removePropertyChangeListener(propertyName, listener);
124     }
125
126     public String JavaDoc toString() {
127         StringBuilder JavaDoc buffer = new StringBuilder JavaDoc();
128         buffer.append("Row #");
129         buffer.append(table.indexOfRow(this));
130         buffer.append(" [ ");
131         int i=0;
132         for (DataCell c : cells.values()) {
133             buffer.append(c.value);
134             if (i < cells.size() -1) {
135                 buffer.append(", ");
136             }
137             i++;
138         }
139         return buffer.toString();
140     }
141     
142     private static final class DataCell {
143         Object JavaDoc originalValue;
144         Object JavaDoc value;
145         boolean changed;
146         
147         public void setValue(Object JavaDoc newValue) {
148             if (newValue != value && !changed) {
149                 originalValue = value;
150                 value = newValue;
151                 changed = true;
152             } else if (newValue != value) {
153                 value = newValue;
154             }
155         }
156     }
157 }
Popular Tags