1 /***
2 * DependencyTableModel.java
3 *
4 * Project: Dependency Tool
5 *
6 * WHEN WHO WHAT
7 * 06.06.2003 pko initial public release
8 * 17.07.2002 ctr creation
9 *
10 * Copyright 2003 ELCA Informatique SA
11 * Av. de la Harpe 22-24, 1000 Lausanne 13, Switzerland
12 * www.elca.ch
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public License
16 * as published by the Free Software Foundation; either version 2.1 of
17 * the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 * USA
28 */
29
30 package ch.elca.dependency.gui;
31
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.Comparator;
35 import javax.swing.table.AbstractTableModel;
36
37 /***
38 * This class is the data model for the dependency list table in the
39 * <code>DependencyDialog</code>. It is responsible for managing the
40 * table entries.
41 *
42 * @author Christoph Trutmann
43 * @version 1.0-beta
44 */
45 public class DependencyTableModel extends AbstractTableModel {
46
47 /***
48 * Column names appearing in the table header.
49 */
50 private static final String[] COLUMN_NAMES = {"From class", "Type", "To class"};
51
52 /***
53 * Determines the type of the column.
54 */
55 private static final Class[] COLUMN_CLASSES = {String.class, String.class, String.class};
56
57 /***
58 * Determines which cells are editable.
59 */
60 private static final boolean[] CELL_IS_EDITABLE = {false, false, false};
61
62 /***
63 * Collection with all the rows of the table stored in an object array.
64 */
65 private ArrayList m_rows;
66
67 /***
68 * Constructor
69 */
70 public DependencyTableModel(){
71 m_rows = new ArrayList();
72 }
73
74 /***
75 * Returns the number of columns in this data table.
76 *
77 * @return the number of columns in the model
78 */
79 public int getColumnCount(){
80 return COLUMN_NAMES.length;
81 }
82
83 /***
84 * Returns the number of rows in this data table.
85 *
86 * @return the number of rows in the model
87 */
88 public int getRowCount(){
89 return m_rows.size();
90 }
91
92 /***
93 * Returns an attribute value for the cell at <code>row</code>
94 * and <code>column</code>.
95 *
96 * @param row the row whose value is to be queried
97 * @param column the column whose value is to be queried
98 * @return the value Object at the specified cell
99 * @exception ArrayIndexOutOfBoundsException if an invalid row or
100 * column was given
101 */
102 public Object getValueAt(int row, int col){
103 Object[] entry = (Object[])m_rows.get(row);
104 return entry[col];
105 }
106
107 /***
108 * Returns the column name.
109 *
110 * @return A name for this column using the string value of the
111 * appropriate member.
112 * If the value is <code>null</code> or does not have an entry for
113 * this index, returns the default name provided by the superclass.
114 */
115 public String getColumnName(int col) {
116
117 if ( COLUMN_NAMES.length <= col ) {
118 return super.getColumnName(col);
119 }
120
121 if ( COLUMN_NAMES[col] == null || COLUMN_NAMES[col].equals("") ) {
122 return super.getColumnName(col);
123 }
124 else {
125 return COLUMN_NAMES[col];
126 }
127 }
128
129
130 /***
131 * Returns the most specific superclass for all the cell values
132 * in the column. This is used by the <code>JTable</code> to set up a
133 * default renderer and editor for the column.
134 *
135 * @param columnIndex the index of the column
136 * @return the common ancestor class of the object values in the model.
137 */
138 public Class getColumnClass(int columnIndex){
139 return COLUMN_CLASSES[columnIndex];
140 }
141
142
143 /***
144 * Sets the value in the cell at <code>columnIndex</code> and
145 * <code>rowIndex</code> to <code>aValue</code>.
146 *
147 * @param aValue the new value
148 * @param rowIndex the row whose value is to be changed
149 * @param columnIndex the column whose value is to be changed
150 * @see #getValueAt
151 * @see #isCellEditable
152 */
153 public void setValueAt(Object aValue, int rowIndex, int columnIndex){
154 Object[] row = (Object[])m_rows.get(rowIndex);
155 row[columnIndex] = aValue;
156 m_rows.set(rowIndex, row);
157 fireTableRowsUpdated(rowIndex, rowIndex);
158 }
159
160
161 /***
162 * Adds the dependency to the list.
163 *
164 * @param name Name of the added class.
165 * @param value Included types of that dependency.
166 * @param to Name of the class where the dependency goes to.
167 */
168 public void addDependency(String name, String type, String to){
169 Object[] newDependency = new Object[3];
170 newDependency[0] = name;
171 newDependency[1] = type;
172 newDependency[2] = to;
173 m_rows.add(newDependency);
174 int lastElem = m_rows.size()-1;
175 fireTableRowsInserted(lastElem, lastElem);
176 }
177
178
179 /***
180 * Returns false for all cells except the first which is the check box for
181 * marking a filter entry.
182 *
183 * @param rowIndex the row being queried
184 * @param columnIndex the column being queried
185 * @return false for all the cells except the first one.
186 */
187 public boolean isCellEditable(int rowIndex, int columnIndex) {
188 return CELL_IS_EDITABLE[columnIndex];
189 }
190
191
192 /***
193 * Clears the whole table model that means all the rows are deleted.
194 */
195 public void clear(){
196 m_rows.clear();
197 }
198
199
200 /***
201 * Sorts all the rows of the <code>JTable</code> in the specified order.
202 * The sort is done in order to the specified column. Null values alvays
203 * appear last.
204 *
205 * @param colIndex Column index which counts for the sort of the rows.
206 * @param ascending Flag specifying the order of the sort.
207 */
208 public void sortAllRowsBy(int colIndex, boolean ascending) {
209 Collections.sort(m_rows, new ColumnSorter(colIndex, ascending));
210
211 fireTableStructureChanged(); // update the table view
212 }
213
214
215 /***
216 * Comparator implementation for the table model.
217 */
218 class ColumnSorter implements Comparator {
219
220 /***
221 * Column which is responsible for the sort.
222 */
223 int m_colIndex;
224
225 /***
226 * Flag specifying the order of the sort.
227 */
228 boolean m_ascending;
229
230 /***
231 * Constructor - Initializes the column to sort and the order to
232 * perform the algorithm.
233 *
234 * @param colIndex Index of the column to sort.
235 * @param ascending Order of the sort.
236 */
237 ColumnSorter(int colIndex, boolean ascending) {
238 m_colIndex = colIndex;
239 m_ascending = ascending;
240 }
241
242
243 /***
244 * Compare method for the rows of this table. The objects are Object
245 * arrays which contain the data of a row.
246 *
247 * @param a The first object to compare.
248 * @param b The second object to compare.
249 * @return A negative integer, zero, or a positive integer as this object
250 * is less than, equal to, or greater than the specified object.
251 */
252 public int compare(Object a, Object b) {
253 Object[] objArr1 = (Object[])a;
254 Object[] objArr2 = (Object[])b;
255 Object o1 = objArr1[m_colIndex];
256 Object o2 = objArr2[m_colIndex];
257
258 // Treat empty strains like nulls
259 if (o1 instanceof String && ((String)o1).length() == 0) {
260 o1 = null;
261 }
262 if (o2 instanceof String && ((String)o2).length() == 0) {
263 o2 = null;
264 }
265
266 // Sort nulls so they appear last, regardless
267 // of sort order
268 if (o1 == null && o2 == null) {
269 return 0;
270 } else if (o1 == null) {
271 return 1;
272 } else if (o2 == null) {
273 return -1;
274 } else if (o1 instanceof Comparable) {
275 if (m_ascending) {
276 return ((Comparable)o1).compareTo(o2);
277 } else {
278 return ((Comparable)o2).compareTo(o1);
279 }
280 } else {
281 if (m_ascending) {
282 return o1.toString().compareTo(o2.toString());
283 } else {
284 return o2.toString().compareTo(o1.toString());
285 }
286 }
287 }
288 }
289
290
291 }
This page was automatically generated by Maven