KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > sortabletable > FineGrainTableModel


1 package snow.sortabletable;
2
3 import javax.swing.table.*;
4 import java.util.*;
5 import javax.swing.event.*;
6 import javax.swing.*;
7 import java.awt.event.*;
8 import java.awt.*;
9 import java.util.regex.*;
10
11
12
13 /** this table model define a new TableModelChangeListener
14    this model should be used with the SortableTableModel to allow
15    keeping the selection when table model changes.
16
17    HOWTO use:
18     + by each modification of this model (i.e. in SetValue())
19       First call before change : fireModelWillChange()
20       After change : fireTableDataChanged() and fireModelHasChanged()
21 */

22 public abstract class FineGrainTableModel extends AbstractTableModel
23 {
24   final Vector<TableModelChangeListener> modelListeners = new Vector<TableModelChangeListener>();
25
26
27   public FineGrainTableModel()
28   {
29
30   } // Constructor
31

32
33  /** important, this allow the table to determine which renderer to use
34      (checkbox for Boolean, ...)
35   */

36   public Class JavaDoc getColumnClass ( int column )
37   {
38      if(this.getColumnCount()==0 || this.getRowCount()==0)
39      {
40         return Object JavaDoc.class.getClass();
41      }
42
43      Object JavaDoc val = this.getValueAt(0,column);
44      if(val==null) return String JavaDoc.class;
45      return val.getClass();
46   }
47
48
49   /** To sort columns... owerwrite if you need a better (or faster) sorting
50   */

51 @SuppressWarnings JavaDoc("unchecked")
52   public int compareForColumnSort(int pos1, int pos2, int col)
53   {
54     Object JavaDoc val1 = this.getValueAt(pos1,col);
55     Object JavaDoc val2 = this.getValueAt(pos2,col);
56
57     if(val1 instanceof Comparable JavaDoc)
58     {
59        return ((Comparable JavaDoc) val1).compareTo(val2);
60     }
61     else if(val1 instanceof Boolean JavaDoc)
62     {
63        return compareBooleans((Boolean JavaDoc) val1, (Boolean JavaDoc) val2);
64     }
65     else
66     {
67        // compare as a String
68
return (""+val1.toString()).compareTo(this.getValueAt(pos2,col).toString());
69     }
70   }
71
72
73   /** @return true if the txt appears in the given row
74       overwrite it if you want a special search (Approximate, numerical, ...)
75       @param p if not null, regex search p should be used.
76   */

77   public boolean hitForTextSearch(int row, String JavaDoc txt, Pattern p)
78   {
79      if(txt==null || txt.equals("")) return true;
80      String JavaDoc search = txt.toUpperCase();
81
82      for(int col=0; col<this.getColumnCount(); col++)
83      {
84         String JavaDoc val = this.getValueAt(row,col).toString();
85         if(p!=null)
86         {
87           Matcher m = p.matcher(val);
88           return m.matches();
89         }
90         else
91         {
92           if(val.toUpperCase().indexOf(search)>=0) return true;
93         }
94      }
95      return false;
96   }
97
98   /** @return true if the txt appears in the given row and column
99       overwrite it if you want a special search (Approximate, numerical, ...)
100
101       This is used in the advanced search
102   */

103   public boolean hitForTextSearch(int row, int column, String JavaDoc txt, Pattern p)
104   {
105      //System.out.println("Search("+row+", "+column+")");
106
if(column==-1) return hitForTextSearch(row, txt, p);
107
108      if(txt==null || txt.equals("")) return true;
109      String JavaDoc search = txt.toUpperCase();
110      String JavaDoc val = this.getValueAt(row,column).toString();
111      if(val.toUpperCase().indexOf(search)>=0) return true;
112
113      return false;
114   }
115
116   /** overwrite this is you want to specify a default width... (measured in Label.font size).
117       -1 do nothing. Any positive value will be used as preferred width.
118
119       To be effective, the table must be set in
120         table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
121       before call installGUI()
122   */

123   public int getPreferredColumnWidth(int column)
124   {
125     return -1;
126   }
127
128   public int getColumnAlignment(int column)
129   {
130     return JLabel.LEFT;
131   }
132
133 /* public final boolean hitForTextSearch(int row, String txt)
134   {
135    return true;
136   }*/

137
138   // Selection
139
//
140
// These four methods are not abstract,
141
// public void setRowSelection(int row, boolean isSelected)
142
// public boolean isRowSelected(int row)
143
// public void clearRowSelection()
144
// public int[] getSelectedRows()
145
//
146
// Here implemented is a non persistant selection mechanism.
147
// overwrite it to provide a selection that is stored in your model
148
//
149
//
150

151   private HashSet<Integer JavaDoc> selectedRows = new HashSet<Integer JavaDoc>();
152
153
154   /** used to select/deselect a given row.
155       this method doesn't fire any events
156       overwrite this AND THE TWO OTHERS selection method to provide your
157       selection mechanism. (maybe storing it in the model items)
158
159  public void setRowSelection(int row, boolean isSelected)
160  public boolean isRowSelected(int row)
161  public void clearRowSelection()
162
163
164       EACH model row deletion will destroy sense of this selection.
165       => MUST NORMALLY BY OVERWRITTEN
166   */

167   public void setRowSelection(int row, boolean isSelected)
168   {
169     if(isSelected)
170     {
171       selectedRows.add( row );
172     }
173     else
174     {
175       selectedRows.remove( row );
176     }
177   }
178
179   /** tells if the row is selected
180   */

181   public boolean isRowSelected(int row)
182   {
183     return selectedRows.contains( row );
184   }
185
186   public void clearRowSelection()
187   {
188     for(int i=0; i<this.getRowCount(); i++)
189     {
190       this.setRowSelection(i, false);
191     }
192   }
193
194   /** overwrite only if you want to boost.
195      This method use isRowSelected(row) to detect which rows are selected.
196   */

197   public int[] getSelectedRows()
198   {
199     // slow but robust
200
//
201
Vector<Integer JavaDoc> sel = new Vector<Integer JavaDoc>();
202     synchronized(this)
203     {
204       for(int i=0; i<getRowCount(); i++)
205       {
206         if(isRowSelected(i)) sel.add(i);
207       }
208       int[] rep = new int[sel.size()];
209
210       for(int i=0; i<rep.length; i++)
211       {
212         rep[i] = sel.elementAt(i);
213       }
214       return rep;
215     }
216
217   }
218
219
220
221
222   // Model Listener
223
//
224
public void addModelChangeListener(TableModelChangeListener listener)
225   {
226      modelListeners.addElement(listener);
227   }
228
229   public void removeModelChangeListener(TableModelChangeListener listener)
230   {
231      modelListeners.removeElement(listener);
232   }
233
234   public void fireTableModelWillChange()
235   {
236     TableModelChangeListener[] tml = null;
237     synchronized(modelListeners)
238     {
239        // do a copy of the listeners to decouple them from the vector
240
// because they may perform operation on the vector in the notify loop (remove listener)
241
// and caus concurrent modification exceptions
242
tml = modelListeners.toArray(new TableModelChangeListener[modelListeners.size()]);
243     }
244     for(int i=tml.length-1; i>=0; i--) // last added first
245
{
246        tml[i].tableModelWillChange(new ChangeEvent(this));
247     }
248   }
249
250   public void fireTableModelHasChanged()
251   {
252     TableModelChangeListener[] tml = null;
253     synchronized(modelListeners)
254     {
255        // do a copy of the listeners to decouple them from the vector
256
// because they may perform operation on the vector in the notify loop (remove listener)
257
// and caus concurrent modification exceptions
258
tml = modelListeners.toArray(new TableModelChangeListener[modelListeners.size()]);
259     }
260     for(int i=tml.length-1; i>=0; i--) // last added first
261
{
262        tml[i].tableModelHasChanged(new ChangeEvent(this));
263     }
264   }
265
266
267   // Some utilities for comparaison
268
//
269

270   public static int compareInts(int i1, int i2)
271   {
272     if(i1==i2) return 0;
273     if(i1>i2) return 1;
274     return -1;
275   }
276
277
278   public static int compareDoubles(double i1, double i2)
279   {
280     if(i1==i2) return 0;
281     if(i1>i2) return 1;
282     return -1;
283   }
284
285   public static int compareBooleans(boolean b1, boolean b2)
286   {
287     if(b1==b2) return 0;
288     if(b1) return 1;
289     return -1;
290   }
291
292
293 } // FineGrainTableModel
Popular Tags