1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui.components;
21
22 import com.cosylab.gui.components.table.DefaultObjectTableEngine;
23 import com.cosylab.gui.components.table.MutableTableModelRows;
24 import com.cosylab.gui.components.table.ObjectTableEngine;
25 import com.cosylab.gui.components.table.TableRow;
26
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.Map;
32 import java.util.Set;
33
34
35
36
37
38
39
40
41
42
43 public class ObjectTableModel extends MutableTableModelRows implements Set
44 {
45 private static final long serialVersionUID = 1L;
46 private ObjectTableEngine objectTableEngine;
47 private Map<Object, TableRow> objects2rows = new HashMap<Object, TableRow>();
48
49
50
51
52 public ObjectTableModel()
53 {
54 super();
55 }
56
57
58
59
60
61
62 public ObjectTableModel(ObjectTableEngine engine)
63 {
64 this();
65 setObjectTableEngine(engine);
66 }
67
68
69
70
71 public int size()
72 {
73 return objects2rows.size();
74 }
75
76
77
78
79 public synchronized void clear()
80 {
81 Object[] o = objects2rows.keySet().toArray();
82
83 for (int i = 0; i < o.length; i++) {
84 remove(o[i]);
85 }
86 }
87
88
89
90
91 public boolean isEmpty()
92 {
93 return objects2rows.isEmpty();
94 }
95
96
97
98
99 public synchronized Object[] toArray()
100 {
101 return objects2rows.keySet().toArray();
102 }
103
104
105
106
107 public synchronized boolean add(Object o)
108 {
109 if (objects2rows.containsKey(o)) {
110 return false;
111 }
112
113 TableRow row = getObjectTableEngine().getTableRow(o);
114
115 if (row == null) {
116 return false;
117 }
118
119 addRow(row);
120 objects2rows.put(o, row);
121
122 return true;
123 }
124
125
126
127
128 public synchronized boolean contains(Object o)
129 {
130 return objects2rows.containsKey(o);
131 }
132
133
134
135
136 public synchronized boolean remove(Object o)
137 {
138 TableRow r = objects2rows.get(o);
139
140 if (r == null) {
141 return false;
142 }
143
144 removeRow(r);
145 objects2rows.remove(o);
146 getObjectTableEngine().releaseTableRow(o, r);
147
148 return true;
149 }
150
151
152
153
154 public synchronized boolean addAll(Collection c)
155 {
156 boolean b = false;
157 Iterator it = c.iterator();
158
159 while (it.hasNext()) {
160 Object o = it.next();
161
162 if (!objects2rows.containsKey(o)) {
163 b = true;
164 add(o);
165 }
166 }
167
168 return b;
169 }
170
171
172
173
174 public synchronized boolean containsAll(Collection c)
175 {
176 return objects2rows.keySet().containsAll(c);
177 }
178
179
180
181
182 public synchronized boolean removeAll(Collection c)
183 {
184 if (c == null || c.isEmpty()) {
185 return false;
186 }
187
188 Iterator it = c.iterator();
189
190 while (it.hasNext()) {
191 remove(it.next());
192 }
193
194 return true;
195 }
196
197
198
199
200 public synchronized boolean retainAll(Collection c)
201 {
202 ArrayList<Object> rem = new ArrayList<Object>(objects2rows.size());
203 Iterator<Object> it = objects2rows.keySet().iterator();
204
205 while (it.hasNext()) {
206 Object key = it.next();
207
208 if (!c.contains(key)) {
209 rem.add(key);
210 }
211 }
212
213 return removeAll(rem);
214 }
215
216
217
218
219 public Iterator iterator()
220 {
221 return objects2rows.keySet().iterator();
222 }
223
224
225
226
227 @SuppressWarnings("unchecked")
228 public synchronized Object[] toArray(Object[] a)
229 {
230 return objects2rows.keySet().toArray(a);
231 }
232
233
234
235
236
237
238
239
240 public ObjectTableEngine getObjectTableEngine()
241 {
242 if (objectTableEngine == null) {
243 objectTableEngine = new DefaultObjectTableEngine();
244 }
245
246 return objectTableEngine;
247 }
248
249
250
251
252
253
254
255
256
257 public void setObjectTableEngine(ObjectTableEngine objectTableEngine)
258 {
259 if (size() > 0) {
260 throw new IllegalStateException(
261 "objectTableEngine can be changed only on empty model.");
262 }
263
264 this.objectTableEngine = objectTableEngine;
265 }
266
267
268
269
270 public synchronized void setValueAt(Object aValue, int rowIndex,
271 int columnIndex)
272 {
273 if (rowIndex < 0 || rowIndex >= getRowCount() || columnIndex < 0
274 || columnIndex >= getColumnCount()) {
275 return;
276 }
277
278 TableRow tr = getRow(rowIndex);
279
280 Iterator it = objects2rows.keySet().iterator();
281
282 while (it.hasNext()) {
283 Object o = it.next();
284
285 if (tr == objects2rows.get(o)) {
286 getObjectTableEngine().setValue(aValue, o, tr, columnIndex);
287
288 return;
289 }
290 }
291 }
292
293
294
295 public String getColumnName(int column) {
296 return getObjectTableEngine().getColumnName(column);
297 }
298 }
299
300