1 19 20 package org.apache.cayenne.dataview; 21 22 import javax.swing.AbstractListModel ; 23 24 import org.apache.cayenne.DataObject; 25 26 31 public class DOListModel extends AbstractListModel implements DataObjectChangeListener, 32 FieldValueChangeListener { 33 34 protected ObjEntityViewField viewField; 35 protected DataObjectList dataObjects = new DataObjectList(1); 36 37 public DOListModel() { 38 } 39 40 public void setViewField(ObjEntityViewField field) { 41 if (this.viewField != null) { 42 this.viewField.getOwner().getOwner().removeFieldValueChangeListener(this); 43 } 44 this.viewField = field; 45 viewField.getOwner().getOwner().addFieldValueChangeListener(this); 46 fireContentsChanged(this, 0, getSize()); 47 } 48 49 public void setDataObjects(DataObjectList dataObjects) { 50 this.dataObjects.removeDataObjectChangeListener(this); 51 this.dataObjects = dataObjects; 52 this.dataObjects.addDataObjectChangeListener(this); 53 fireContentsChanged(this, 0, getSize()); 54 } 55 56 public int getSize() { 57 return dataObjects.size(); 58 } 59 60 public DataObject getDataObject(int index) { 61 return (DataObject) dataObjects.get(index); 62 } 63 64 public Object getElementAt(int index) { 65 if (viewField == null) 66 return getDataObject(index); 67 return viewField.getValue(getDataObject(index)); 68 } 69 70 public void dataChanged(DataObjectChangeEvent event) { 71 if (event.isMultiObjectChange()) { 72 fireContentsChanged(this, 0, getSize()); 73 return; 74 } 75 int affectedRow = event.getAffectedDataObjectIndex(); 76 switch (event.getId()) { 77 case DataObjectChangeEvent.DATAOBJECT_ADDED: 78 fireIntervalAdded(this, affectedRow, affectedRow); 79 break; 80 case DataObjectChangeEvent.DATAOBJECT_REMOVED: 81 fireIntervalRemoved(this, affectedRow, affectedRow); 82 break; 83 case DataObjectChangeEvent.DATAOBJECT_CHANGED: 84 fireContentsChanged(this, affectedRow, affectedRow); 85 break; 86 default: 87 fireContentsChanged(this, 0, getSize()); 88 } 89 } 90 91 public ObjEntityViewField getViewField() { 92 return viewField; 93 } 94 95 public DataObjectList getDataObjects() { 96 return dataObjects; 97 } 98 99 public void fieldValueChanged(FieldValueChangeEvent event) { 100 if (viewField != null && viewField.isSameObjAttribute(event.getField())) { 101 int index = dataObjects.indexOf(event.getModifiedObject()); 102 if (index >= 0) 103 fireContentsChanged(this, index, index); 104 } 105 } 106 } 107 | Popular Tags |