1 7 8 package org.jdesktop.dataset; 9 import java.beans.PropertyChangeListener ; 10 import java.beans.PropertyChangeSupport ; 11 import java.util.ArrayList ; 12 import java.util.Collections ; 13 import java.util.List ; 14 import java.util.logging.Logger ; 15 import org.jdesktop.dataset.NameGenerator; 16 17 21 public class DataRelation { 22 25 private static final Logger LOG = Logger.getLogger(DataRelation.class.getName()); 26 27 protected static final String DEFAULT_NAME_PREFIX = "DataRelation"; 29 private static final NameGenerator NAMEGEN = new NameGenerator(DEFAULT_NAME_PREFIX); 30 31 34 private DataSet dataSet; 35 38 private String name; 39 44 private DataColumn parentColumn; 45 50 private DataColumn childColumn; 51 52 private PropertyChangeSupport pcs = new PropertyChangeSupport (this); 53 54 57 protected DataRelation(DataSet ds) { 58 assert ds != null; 59 this.dataSet = ds; 60 name = NAMEGEN.generateName(this); 61 } 62 63 66 public DataSet getDataSet() { 67 return dataSet; 68 } 69 70 74 public void setName(String name) { 75 if (this.name != name) { 76 assert DataSetUtils.isValidName(name); 77 assert !dataSet.hasElement(name); 78 String oldName = this.name; 79 this.name = name; 80 pcs.firePropertyChange("name", oldName, name); 81 } 82 } 83 84 87 public String getName() { 88 return name; 89 } 90 91 94 public DataColumn getParentColumn() { 95 return parentColumn; 96 } 97 98 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 116 public DataColumn getChildColumn() { 117 return childColumn; 118 } 119 120 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 138 public List <DataRow> getRows(DataRow parentRow) { 139 if (parentColumn == null || childColumn == null || parentRow == null) { 142 return Collections.unmodifiableList(Collections.EMPTY_LIST); 143 } 144 145 assert parentRow.getTable().equals(parentColumn.getTable()); 147 148 DataTable childTable = childColumn.getTable(); 149 Object parentKey = parentColumn.getTable().getValue(parentRow, parentColumn); 150 List <DataRow> rows = new ArrayList <DataRow>(); 151 for (DataRow childRow : childTable.getRows()) { 152 Object 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 166 public List <DataRow> getRows(int parentRowIndex) { 167 if (parentColumn == null || childColumn == null || parentRowIndex < 0) { 168 return Collections.unmodifiableList(Collections.EMPTY_LIST); 169 } 170 DataTable parentTable = parentColumn.getTable(); 174 return getRows(parentTable.getRow(parentRowIndex)); 175 } 176 177 183 public List <DataRow> getRows(DataRow[] parentRows) { 184 List <DataRow> rows = new ArrayList <DataRow>(); 185 for (DataRow parentRow : parentRows) { 186 rows.addAll(getRows(parentRow)); 187 } 188 return Collections.unmodifiableList(rows); 189 } 190 191 197 public List <DataRow> getRows(int[] parentRowIndices) { 198 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 218 public List <DataRow> getRows(List <Integer > parentRowIndices) { 219 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 listener) { 233 pcs.addPropertyChangeListener(listener); 234 } 235 236 public void addPropertyChangeListener(String property, PropertyChangeListener listener) { 237 pcs.addPropertyChangeListener(property, listener); 238 } 239 240 public void removePropertyChangeListener(PropertyChangeListener listener) { 241 pcs.removePropertyChangeListener(listener); 242 } 243 244 public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) { 245 pcs.removePropertyChangeListener(propertyName, listener); 246 } 247 } | Popular Tags |