View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
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   * <code>TableModel</code>, which accepts new items as objects and converts
37   * them to new rows. This model acts as <code>Set</code> for Objects, it does
38   * not hold them in constant order and does not permit multiple entries with
39   * same object.
40   *
41   * @author <a href="mailto:igor.kriznar@cosylab.com">Igor Kriznar</a>
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  	 * Creates new empty model.
51  	 */
52  	public ObjectTableModel()
53  	{
54  		super();
55  	}
56  
57  	/**
58  	 * Creates new model with engine.
59  	 *
60  	 * @param engine the table engine
61  	 */
62  	public ObjectTableModel(ObjectTableEngine engine)
63  	{
64  		this();
65  		setObjectTableEngine(engine);
66  	}
67  
68  	/* (non-Javadoc)
69  	 * @see java.util.Set#size()
70  	 */
71  	public int size()
72  	{
73  		return objects2rows.size();
74  	}
75  
76  	/* (non-Javadoc)
77  	 * @see java.util.Set#clear()
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  	/* (non-Javadoc)
89  	 * @see java.util.Set#isEmpty()
90  	 */
91  	public boolean isEmpty()
92  	{
93  		return objects2rows.isEmpty();
94  	}
95  
96  	/* (non-Javadoc)
97  	 * @see java.util.Set#toArray()
98  	 */
99  	public synchronized Object[] toArray()
100 	{
101 		return objects2rows.keySet().toArray();
102 	}
103 
104 	/* (non-Javadoc)
105 	 * @see java.util.Set#add(java.lang.Object)
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 	/* (non-Javadoc)
126 	 * @see java.util.Set#contains(java.lang.Object)
127 	 */
128 	public synchronized boolean contains(Object o)
129 	{
130 		return objects2rows.containsKey(o);
131 	}
132 
133 	/* (non-Javadoc)
134 	 * @see java.util.Set#remove(java.lang.Object)
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 	/* (non-Javadoc)
152 	 * @see java.util.Set#addAll(java.util.Collection)
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 	/* (non-Javadoc)
172 	 * @see java.util.Set#containsAll(java.util.Collection)
173 	 */
174 	public synchronized boolean containsAll(Collection c)
175 	{
176 		return objects2rows.keySet().containsAll(c);
177 	}
178 
179 	/* (non-Javadoc)
180 	 * @see java.util.Set#removeAll(java.util.Collection)
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 	/* (non-Javadoc)
198 	 * @see java.util.Set#retainAll(java.util.Collection)
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 	/* (non-Javadoc)
217 	 * @see java.util.Set#iterator()
218 	 */
219 	public Iterator iterator()
220 	{
221 		return objects2rows.keySet().iterator();
222 	}
223 
224 	/* (non-Javadoc)
225 	 * @see java.util.Set#toArray(java.lang.Object[])
226 	 */
227 	@SuppressWarnings("unchecked")
228 	public synchronized Object[] toArray(Object[] a)
229 	{
230 		return objects2rows.keySet().toArray(a);
231 	}
232 
233 	/**
234 	 * Return engine, which converts objects to table rows. If engine was not
235 	 * set before, call to this method initializes this model to
236 	 * <code>DefaultObjectTableEngine</code>.
237 	 *
238 	 * @return object to row converter
239 	 */
240 	public ObjectTableEngine getObjectTableEngine()
241 	{
242 		if (objectTableEngine == null) {
243 			objectTableEngine = new DefaultObjectTableEngine();
244 		}
245 
246 		return objectTableEngine;
247 	}
248 
249 	/**
250 	 * Sets new object to for converter. Setting new factory is only alllowed
251 	 * with empty model.
252 	 *
253 	 * @param objectTableEngine new object to row converter
254 	 *
255 	 * @throws IllegalStateException if model is not empty
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 	/* (non-Javadoc)
268 	 * @see javax.swing.table.AbstractTableModel#setValueAt(java.lang.Object, int, int)
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 	/* (non-Javadoc)
293 	 * @see com.cosylab.gui.components.table.TableModelRows#getColumnName(int)
294 	 */
295 	public String getColumnName(int column) {
296 		return getObjectTableEngine().getColumnName(column);
297 	}
298 }
299 
300 /* __oOo__ */