KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > sortabletable > TestModel


1 package snow.sortabletable;
2
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6 import javax.swing.event.*;
7 import javax.swing.table.*;
8
9 import java.util.*;
10
11
12 public final class TestModel extends FineGrainTableModel
13 {
14   final Vector<TestModelItem> items = new Vector<TestModelItem>();
15   String JavaDoc[] COLUMN_NAMES = new String JavaDoc[]{"Size", "Name", "Index", "Index2", "Index3", "check"};
16
17
18   public TestModel()
19   {
20     for (int i=0; i<58; i++)
21     {
22       addRandom();
23     }
24   }
25
26   int[] COLUMN_PREFERED_SIZES = new int[]{6,4,4,12,12,12};
27   public int getPreferredColumnWidth(int column)
28   {
29     if(column>=0 && column<COLUMN_PREFERED_SIZES.length) return COLUMN_PREFERED_SIZES[column];
30     return -1;
31   }
32
33
34
35   public void add(TestModelItem titm)
36   {
37       fireTableModelWillChange();
38
39       items.addElement(titm);
40
41       fireTableDataChanged();
42       fireTableModelHasChanged();
43   }
44
45   public void remove(TestModelItem titm)
46   {
47       fireTableModelWillChange();
48       items.removeElement(titm);
49       fireTableDataChanged();
50       fireTableModelHasChanged();
51   }
52
53
54   int n=0;
55
56   public void addRandom()
57   {
58      n++;
59
60      TestModelItem titm = new TestModelItem();
61      titm.col1 = Math.random()*1e6;
62      if(Math.random()<0.1) titm.col1 = Math.random()*1e3;
63      titm.col2 = ""+((char) ('a'+(n%26)));
64      titm.col3 = n;
65      titm.col4 = (int) (Math.random()*1000);
66      titm.col5 = (int) (Math.random()*1000000);
67      add(titm);
68   }
69
70
71   public void removeLast()
72   {
73       if(items.size()==0) return;
74
75       fireTableModelWillChange();
76       items.removeElementAt(items.size()-1);
77       fireTableDataChanged();
78       fireTableModelHasChanged();
79   }
80
81
82   public int getRowCount() {return items.size();}
83   public int getColumnCount() {return 6;}
84
85   public TestModelItem getItemAt(int pos)
86   {
87     return items.elementAt(pos);
88   }
89
90   public Object JavaDoc getValueAt(int row, int column)
91   {
92     TestModelItem item = getItemAt(row);
93     if(column==0) return item.col1;
94     if(column==1) return item.col2;
95     if(column==2) return item.col3;
96     if(column==3) return item.col4;
97     if(column==4) return item.col5;
98     if(column==5) return item.checked;
99     return "??";
100   }
101
102   public boolean hitForTextSearch(int row, String JavaDoc str)
103   {
104     return super.hitForTextSearch(row, str, null);
105   /*
106     TestModelItem item = getItemAt(row);
107
108     String s1 =""+item.col1;
109     if(s1.indexOf(str)>=0) return true;
110     String s2 =""+item.col2;
111     if(s2.indexOf(str)>=0) return true;
112     String s3 =""+item.col3;
113     if(s3.indexOf(str)>=0) return true;
114     String s4 =""+item.col4;
115     if(s4.indexOf(str)>=0) return true;
116     String s5 =""+item.col5;
117     if(s5.indexOf(str)>=0) return true;
118     return false; */

119   }
120
121   public boolean isCellEditable(int row, int col)
122   {
123     return col<=2 || col==5;
124   }
125
126   public void setValueAt(Object JavaDoc val, int row, int col)
127   {
128     if(val==null) return;
129
130     fireTableModelWillChange();
131     TestModelItem item = getItemAt(row);
132     if(col==0)
133     {
134       try
135       {
136         double vd = Double.parseDouble(""+val);
137         item.col1 = vd;
138       }
139       catch(Exception JavaDoc e){}
140     }
141     else if(col==1)
142     {
143       item.col2 = (String JavaDoc) val;
144     }
145     else if(col==2)
146     {
147       item.col3 = (Integer JavaDoc) val;
148     }
149     else if(col==5)
150     {
151       item.checked = (Boolean JavaDoc) val;
152     }
153
154
155     // must notify table that data has changed
156
fireTableDataChanged();
157     fireTableModelHasChanged();
158   }
159
160
161
162   public String JavaDoc getColumnName(int column)
163   {
164     if(column>=0 && column<COLUMN_NAMES.length) return COLUMN_NAMES[column];
165     return "Bad column "+column;
166   }
167
168      /*
169   public int compareForColumnSort(int pos1, int pos2, int col)
170   {
171     if(col==0 || col>=2)
172     {
173       double v1 = Double.parseDouble(""+getValueAt(pos1, col));
174       double v2 = Double.parseDouble(""+getValueAt(pos2, col));
175       if(v1==v2) return 0;
176       if(v1>v2) return 1;
177       return -1;
178     }
179     else
180     {
181       String v1 = ""+getValueAt(pos1, col);
182       String v2 = ""+getValueAt(pos2, col);
183       return v1.compareTo(v2);
184     }
185    // return 0;
186
187   }*/

188
189   // Selection implementation
190
//
191

192   public void setRowSelection(int row, boolean isSelected)
193   {
194     TestModelItem item = getItemAt(row);
195     item.selected = isSelected;
196   }
197
198   public boolean isRowSelected(int row)
199   {
200     TestModelItem item = getItemAt(row);
201     return item.selected;
202   }
203
204   public void clearRowSelection()
205   {
206     synchronized(items)
207     {
208       for(int i=0; i<getRowCount(); i++)
209       {
210          TestModelItem item = getItemAt(i);
211          item.selected = false;
212       }
213     }
214   }
215
216 }
Popular Tags