1 7 8 package org.jdesktop.dataset; 9 10 import java.beans.PropertyChangeEvent ; 11 import java.beans.PropertyChangeListener ; 12 import java.util.ArrayList ; 13 import java.util.Collections ; 14 import java.util.HashMap ; 15 import java.util.List ; 16 import java.util.Map ; 17 import java.util.logging.Logger ; 18 19 import org.jdesktop.dataset.event.TableChangeEvent; 20 21 22 26 public class DataRelationTable extends DataTable { 27 30 private static final Logger LOG = Logger.getLogger(DataRelationTable.class.getName()); 31 34 private DataRelation relation; 35 39 private DataSelector parentSelector; 40 45 private SelectionListener listener = new SelectionListener(); 46 47 50 public DataRelationTable(DataSet ds) { 51 super(ds); 52 } 53 54 58 public void setRelation(DataRelation relation) { 59 this.relation = relation; 62 } 63 64 67 public DataRelation getRelation() { 68 return relation; 69 } 70 71 80 public void setParentSelector(DataSelector selector) { 81 if (this.parentSelector != null) { 82 this.parentSelector.removePropertyChangeListener("rowIndices", listener); 83 } 84 this.parentSelector = selector; 85 if (this.parentSelector != null) { 86 this.parentSelector.addPropertyChangeListener("rowIndices", listener); 87 } 88 } 89 90 93 public DataSelector getParentSelector() { 94 return parentSelector; 95 } 96 97 103 public void setDataProvider(DataProvider dataProvider) { 104 LOG.warning("An attempt was made to set the DataProvider for a " + 105 "DataRelationTable. This is not honored because a " + 106 "DataRelationTable, by definition, gets its records " + 107 "by querying a DataSelector from the Parent table, and " + 108 "the DataRelation leading to the Child table."); 109 } 110 111 117 public void save() { 118 LOG.warning("An attempt was made to save a DataRelationTable. " + 119 "This is not honored because a DataRelationTable, " + 120 "by definition, gets its records by querying a DataSelector " + 121 "from the Parent table, and the DataRelation leading to the " + 122 "Child table. Therefore, to save, the child table should be " + 123 "asked to save, not the DataRelationTable."); 124 } 125 126 133 public DataRow appendRow() { 134 if (relation != null && relation.getChildColumn() != null) { 140 DataTable t = relation.getChildColumn().getTable(); 141 return t.appendRow(); 142 } 143 return null; 144 } 145 146 161 public void refresh() { 162 Map <DataSelector,Object [][]> selectorState = new HashMap <DataSelector,Object [][]>(); 164 List <DataColumn> keyColumns = new ArrayList <DataColumn>(); 165 for (DataColumn c : columns.values()) { 166 if (c.isKeyColumn()) { 167 keyColumns.add(c); 168 } 169 } 170 171 for (DataSelector sel : selectors.values()) { 172 List <Integer > indices = sel.getRowIndices(); 173 Object [][] values = new Object [indices.size()][keyColumns.size()]; 174 for (int i=0; i<indices.size(); i++) { 175 for (int j=0; j<keyColumns.size(); j++) { 176 DataRow row = rows.get(indices.get(i)); 177 values[i][j] = getValue(row, keyColumns.get(j)); 178 } 179 } 180 sel.setRowIndices(new int[0]); 181 selectorState.put(sel, values); 182 } 183 184 clear(); 186 if (relation != null && relation.getChildColumn() != null) { 187 if (parentSelector != null) { 188 super.rows.addAll(relation.getRows(parentSelector.getRowIndices())); 191 } else { 192 super.rows.addAll(relation.getChildColumn().getTable().getRows()); 194 } 195 if (super.rows.size() > 0) { 197 for (DataSelector s : selectors.values()) { 199 s.setRowIndices(new int[]{0}); 200 } 201 } 202 fireDataTableChanged(new TableChangeEvent(this)); 203 } 204 205 for (DataSelector sel : selectors.values()) { 207 Object [][] values = selectorState.get(sel); 208 List <Integer > indices = new ArrayList <Integer >(values.length); 213 for (int i=0; i<values.length; i++) { 214 boolean found = true; 215 for (int rowIndex=0; rowIndex<rows.size(); rowIndex++) { 216 found = true; for (int j=0; j<keyColumns.size(); j++) { 218 DataRow row = rows.get(rowIndex); 219 if (found && values[i][j].equals(row.getValue(keyColumns.get(j)))) { 220 } else { 222 found = false; 223 } 224 } 225 if (found) { 226 indices.add(rowIndex); 227 } 228 } 229 } 230 231 int[] rows = new int[indices.size()]; 233 for (int i=0; i<rows.length; i++) { 234 rows[i] = indices.get(i); 235 } 236 sel.setRowIndices(rows); 237 } 238 } 239 240 250 public DataColumn getColumn(String colName) { 251 DataColumn col = null; 252 if (relation != null && relation.getChildColumn() != null && relation.getChildColumn().getTable() != null) { 253 col = relation.getChildColumn().getTable().getColumn(colName); 254 } 255 256 if (col == null) { 257 return super.getColumn(colName); 258 } else { 259 return col; 260 } 261 } 262 263 268 public List <DataColumn> getColumns() { 269 List <DataColumn> cols = new ArrayList <DataColumn>(); 270 if (relation != null && relation.getChildColumn() != null && relation.getChildColumn().getTable() != null) { 271 cols.addAll(relation.getChildColumn().getTable().getColumns()); 272 } 273 274 cols.addAll(super.getColumns()); 275 return Collections.unmodifiableList(cols); 276 } 277 278 282 private final class SelectionListener implements PropertyChangeListener { 283 public void propertyChange(PropertyChangeEvent evt) { 284 refresh(); 285 } 286 } 287 } | Popular Tags |