1 /***
2 * FilterListTableModel.java
3 *
4 * Project: Dependency Tool
5 *
6 * WHEN WHO WHAT
7 * 06.06.2003 pko initial public release
8 * 25.02.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 org.apache.regexp.RESyntaxException;
33 import java.util.ArrayList;
34 import javax.swing.table.AbstractTableModel;
35
36 import ch.elca.dependency.core.Filter;
37
38 /***
39 * This class is the data model for the filter list table in the
40 * <code>FilterDialog</code>. It is responsible for managing the table
41 * entries.
42 *
43 * @author Christoph Trutmann
44 * @version 1.0-beta
45 */
46 public class FilterListTableModel extends AbstractTableModel {
47
48 /***
49 * This flag specifies whether the model contains exclude or include
50 * filters.
51 * If the flag is true the model is an include one.
52 */
53 private final boolean m_isInclude;
54
55 /***
56 * Column names appearing in the table header.
57 */
58 private static final String[] COLUMN_NAMES = {"", "Filter", "Value"};
59
60 /***
61 * Determines the type of the column.
62 */
63 private static final Class[] COLUMN_CLASSES
64 = {Boolean.class, String.class, String.class};
65
66 /***
67 * Determines which cells are editable.
68 */
69 private static final boolean[] CELL_IS_EDITABLE
70 = {true, false, false};
71
72 /***
73 * Collection with all the rows of the table stored in an object array.
74 */
75 private ArrayList m_rows;
76
77 /***
78 * The index of the last column which was set as marked by the user.
79 */
80 //private int m_lastRowIndex;
81
82
83 /***
84 * Constructor
85 */
86 public FilterListTableModel(boolean isInclude){
87 m_rows = new ArrayList();
88 m_isInclude = isInclude;
89 }
90
91
92 /***
93 * Tells the requester whether the model is an include or an exclude one.
94 */
95 public boolean isInclude(){
96 return m_isInclude;
97 }
98
99
100 /***
101 * Returns the number of columns in this data table.
102 *
103 * @return the number of columns in the model
104 */
105 public int getColumnCount(){
106 return COLUMN_NAMES.length;
107 }
108
109
110 /***
111 * Returns the number of rows in this data table.
112 *
113 * @return the number of rows in the model
114 */
115 public int getRowCount(){
116 return m_rows.size();
117 }
118
119
120 /***
121 * Returns an attribute value for the cell at <code>row</code>
122 * and <code>column</code>.
123 *
124 * @param row the row whose value is to be queried
125 * @param column the column whose value is to be queried
126 * @return the value Object at the specified cell
127 * @exception ArrayIndexOutOfBoundsException if an invalid row or
128 * column was given
129 */
130 public Object getValueAt(int row, int col){
131 Object[] filter = (Object[])m_rows.get(row);
132 return filter[col];
133 }
134
135
136 /***
137 * Returns the column name.
138 *
139 * @return A name for this column using the string value of the
140 * appropriate member.
141 * If the value is <code>null</code> or does not have an entry for
142 * this index, returns the default name provided by the superclass.
143 */
144 public String getColumnName(int col) {
145
146 if ( COLUMN_NAMES.length <= col ) {
147 return super.getColumnName(col);
148 }
149
150 if ( COLUMN_NAMES[col] == null || COLUMN_NAMES[col].equals("") ) {
151 if ( col == 0 ){
152 return ""; // because here has to be no label
153 }
154 else {
155 return super.getColumnName(col);
156 }
157 }
158 else {
159 return COLUMN_NAMES[col];
160 }
161 }
162
163
164 /***
165 * Returns the most specific superclass for all the cell values
166 * in the column. This is used by the <code>JTable</code> to set up a
167 * default renderer and editor for the column.
168 *
169 * @param columnIndex the index of the column
170 * @return the common ancestor class of the object values in the model.
171 */
172 public Class getColumnClass(int columnIndex){
173 return COLUMN_CLASSES[columnIndex];
174 }
175
176
177 /***
178 * Sets the value in the cell at <code>columnIndex</code> and
179 * <code>rowIndex</code> to <code>aValue</code>.
180 *
181 * @param aValue the new value
182 * @param rowIndex the row whose value is to be changed
183 * @param columnIndex the column whose value is to be changed
184 * @see #getValueAt
185 * @see #isCellEditable
186 */
187 public void setValueAt(Object aValue, int rowIndex, int columnIndex){
188 // allows only one selection
189 /*if ( columnIndex == 0 && ((Boolean)aValue).booleanValue() ){
190 Object[] filterTmp = (Object[])m_rows.get(m_lastRowIndex);
191 filterTmp[columnIndex] = new Boolean(false);
192 m_rows.set(m_lastRowIndex, filterTmp);
193 }*/
194
195 Object[] filter = (Object[])m_rows.get(rowIndex);
196 filter[columnIndex] = aValue;
197 m_rows.set(rowIndex, filter);
198 //m_lastRowIndex = rowIndex;
199 fireTableRowsUpdated(rowIndex, rowIndex);
200 }
201
202
203 /***
204 * Adds the Filter to the list.
205 * The new entry is not marked as selected.
206 *
207 * @param name Name of the added filter.
208 * @param value Value of the added filter. It is a string representation of
209 * a regular expression.
210 */
211 public void addFilterEntry(String name, String value){
212 Object[] newEntry = new Object[3];
213 newEntry[0] = new Boolean(false);
214 newEntry[1] = name;
215 newEntry[2] = value;
216 m_rows.add(newEntry);
217 int lastElem = m_rows.size()-1;
218 fireTableRowsInserted(lastElem, lastElem);
219 }
220
221
222 /***
223 * Adds the <code>Filter</code> object to the list.
224 * The new filter entry is not marked as selected.
225 *
226 * @param filter <code>Filter</code> object.
227 */
228 public void addFilter(Filter filter){
229 Object[] newEntry = new Object[3];
230 newEntry[0] = new Boolean(filter.isMarked());
231 newEntry[1] = filter.getName();
232 newEntry[2] = filter.getValue();
233 m_rows.add(newEntry);
234 int lastElem = m_rows.size()-1;
235 fireTableRowsInserted(lastElem, lastElem);
236 }
237
238
239 /***
240 * Removes the Filter from the list.
241 *
242 * @param index Index of the row to remove.
243 * @return Object array with the filter which is now removed.
244 */
245 public Object[] removeFilterEntry(int index){
246 Object[] tmp = null;
247 try {
248 tmp = (Object[])m_rows.remove(index);
249 // if no row exists it's not possible to delete something
250 } catch ( IndexOutOfBoundsException e ) { }
251 fireTableRowsDeleted(index, index);
252 return tmp;
253 }
254
255
256 /***
257 * Get the filter in the specified row.
258 *
259 * @param index Index of the row in which the filter is.
260 * @return <code>Filter</code> object representing the filter in this row.
261 */
262 public Filter getFilterAt(int index) throws RESyntaxException {
263 String name = (String)getValueAt(index, 1);
264 String value = (String)getValueAt(index, 2);
265 boolean isMarked = ((Boolean)getValueAt(index, 0)).booleanValue();
266
267 return new Filter(name, value, m_isInclude, isMarked);
268 }
269
270
271 /***
272 * Returns false for all cells except the first which is the check box for
273 * marking a filter entry.
274 *
275 * @param rowIndex the row being queried
276 * @param columnIndex the column being queried
277 * @return false for all the cells except the first one.
278 */
279 public boolean isCellEditable(int rowIndex, int columnIndex) {
280 return CELL_IS_EDITABLE[columnIndex];
281 }
282
283
284 /***
285 * Clears the whole table model that means all the rows are deleted.
286 */
287 public void clear(){
288 m_rows.clear();
289 //m_lastRowIndex = 0;
290 }
291 }
This page was automatically generated by Maven