1 56 package org.objectstyle.cayenne.modeler.util; 57 58 import java.util.Collections ; 59 60 import javax.swing.table.AbstractTableModel ; 61 62 import org.objectstyle.cayenne.modeler.ProjectController; 63 import org.objectstyle.cayenne.util.PropertyComparator; 64 import org.objectstyle.cayenne.util.Util; 65 66 71 public abstract class CayenneTableModel extends AbstractTableModel { 72 protected ProjectController mediator; 73 protected Object eventSource; 74 protected java.util.List objectList; 75 76 79 public CayenneTableModel( 80 ProjectController mediator, 81 Object eventSource, 82 java.util.List objectList) { 83 super(); 84 this.eventSource = eventSource; 85 this.mediator = mediator; 86 this.objectList = objectList; 87 88 orderList(); 89 } 90 91 public void setValueAt(Object newVal, int row, int col) { 92 if (!Util.nullSafeEquals(newVal, getValueAt(row, col))) { 93 setUpdatedValueAt(newVal, row, col); 94 } 95 } 96 97 101 public abstract void setUpdatedValueAt(Object newVal, int row, int col); 102 103 107 protected void orderList() { 108 String key = getOrderingKey(); 109 if (key != null) { 110 Collections.sort( 111 objectList, 112 new PropertyComparator(getOrderingKey(), getElementsClass())); 113 } 114 } 115 116 119 public abstract Class getElementsClass(); 120 121 125 public String getOrderingKey() { 126 return "name"; 127 } 128 129 132 public int getRowCount() { 133 return objectList.size(); 134 } 135 136 139 public Object getEventSource() { 140 return eventSource; 141 } 142 143 146 public ProjectController getMediator() { 147 return mediator; 148 } 149 150 153 public java.util.List getObjectList() { 154 return objectList; 155 } 156 157 public void addRow(Object row) { 158 objectList.add(row); 159 fireTableDataChanged(); 160 } 161 162 public void removeRow(Object row) { 163 objectList.remove(row); 164 fireTableDataChanged(); 165 } 166 167 170 public int moveRowUp(Object row) { 171 int len = objectList.size(); 172 if (len < 2) { 173 return -1; 174 } 175 176 int ind = objectList.indexOf(row); 177 if (ind <= 0) { 178 return -1; 179 } 180 181 int neighborIndex = ind - 1; 182 if (neighborIndex < 0) { 183 neighborIndex = len - 1; 184 } 185 186 swapRows(ind, neighborIndex); 187 return neighborIndex; 188 } 189 190 193 public int moveRowDown(Object row) { 194 int len = objectList.size(); 195 if (len < 2) { 196 return -1; 197 } 198 199 int ind = objectList.indexOf(row); 200 if (ind < 0 || (ind + 1) >= len) { 202 return -1; 203 } 204 205 int neighborIndex = ind + 1; 206 207 swapRows(ind, neighborIndex); 208 return neighborIndex; 209 } 210 211 protected void swapRows(int i, int j) { 212 Collections.swap(objectList, i, j); 213 fireTableDataChanged(); 214 } 215 } 216 | Popular Tags |