1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
| public class ParamVolTableModel extends AbstractTableModel {
private final Map fields = new HashMap();
private final List points = new ArrayList();
private List cache = new ArrayList();
private final List listeners = new ArrayList();
private boolean modifyModel = false;
private final JFileChooser fileChooser = new JFileChooser();
private boolean enable = true;
public ParamVolTableModel() {
this.fields.put(new Integer(0), "days");
this.fields.put(new Integer(1), "values");
}
public int getColumnCount() {
return this.fields.size();
}
public int getRowCount() {
return this.cache.size();
}
public boolean isCellEditable(int row, int col) {
return this.enable;
}
public Class getColumnClass(int col) {
return String.class;
}
public Object getValueAt(int row, int col) {
VolParam point = (VolParam) this.cache.get(row);
switch (col) {
case 0:
return new Double(point.getDay());
case 1:
new Double(point.getValue());
default:
return null;
}
}
public void setValueAt(Object obj, int row, int col) {
this.modifyModel = true;
VolParam point = (VolParam) this.cache.get(row);
try {
switch (col) {
case 0:
point.setDay(Double.parseDouble(obj.toString()));
break;
case 1:
point.setValue(Double.parseDouble(obj.toString()));
break;
default:
}
point.setModified(true);
//nothing todo
} catch (NumberFormatException e) {
//nothing todo
}
}
public String getColumnName(int index) {
return this.fields.get(new Integer(index)).toString();
}
public void clearData() {
this.cache.clear();
this.fireTableChanged(new TableModelEvent(this));
}
public void removeRows(int[] index) {
List purge = new ArrayList();
for (int i = 0; i < index.length; ++i) {
this.modifyModel = true;
if (this.cache.size() >= index[i] && index[i] >= 0) {
purge.add(this.cache.get(index[i]));
} else {
}
}
this.cache.removeAll(purge);
this.fireTableChanged(new TableModelEvent(this));
}
public void addEmptyRow() {
this.cache.add(new VolParam());
this.modifyModel = true;
this.fireTableChanged(new TableModelEvent(this));
}
public void addTableModelListener(TableModelListener listener) {
if (!this.listeners.contains(listener))
this.listeners.add(listener);
}
public void removeTableModelListener(TableModelListener listener) {
this.listeners.remove(listener);
}
public void fireTableChanged(TableModelEvent evt) {
Iterator it = this.listeners.iterator();
while (it.hasNext())
((TableModelListener) it.next()).tableChanged(evt);
}
public void commit() {
if (this.modifyModel) {
this.copy(this.cache, this.points);
this.fireTableChanged(new TableModelEvent(this));
this.modifyModel = false;
return;
}
}
public void rollback() {
if (this.modifyModel) {
this.copy(this.points, this.cache);
this.modifyModel = false;
this.fireTableChanged(new TableModelEvent(this));
return;
}
}
public boolean isCommited() {
return !this.modifyModel;
}
private void copy(List source, List destination) {
Iterator it = source.iterator();
destination.clear();
while (it.hasNext()) {
VolParam point = (VolParam) it.next();
point.setModified(false);
destination.add(point.clone());
}
}
public void save(File file) throws Exception {
String fileName = file.getAbsolutePath();
file = new File(fileName);
XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(file)));
encoder.writeObject(this.points);
encoder.close();
}
public void load(File file) throws Exception {
XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(new FileInputStream(file)));
this.cache = (List) decoder.readObject();
this.fireTableChanged(new TableModelEvent(this));
this.modifyModel = true;
}
public JFileChooser getFileChooser() {
return this.fileChooser;
}
public boolean isEnable() {
return this.enable;
}
public void setEnable(boolean enable) {
this.enable = enable;
}
public boolean isModified(int row, int col) {
VolParam point = (VolParam) this.cache.get(row);
return point.isModified();
}
} |
Partager