KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > components > table > SortFilterModel


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.components.table;
6
7 import com.opensymphony.webwork.components.table.SortableTableModel;
8
9 import javax.swing.*;
10 import javax.swing.event.TableModelEvent JavaDoc;
11 import javax.swing.event.TableModelListener JavaDoc;
12 import javax.swing.table.TableModel JavaDoc;
13 import java.awt.event.MouseAdapter JavaDoc;
14 import java.awt.event.MouseEvent JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Collections JavaDoc;
17
18
19 /**
20  * @author Onyeje Bose
21  * @version 1.1
22  */

23 public class SortFilterModel extends AbstractFilterModel implements TableModelListener JavaDoc, SortableTableModel {
24     //~ Instance fields ////////////////////////////////////////////////////////
25

26     private ArrayList JavaDoc rows = new ArrayList JavaDoc();
27
28     /**
29      * These are just here to implement the interface
30      */

31     private String JavaDoc _sortDirection = NONE;
32     private boolean dirty = true;
33     private int sortColumn = -1;
34
35     //~ Constructors ///////////////////////////////////////////////////////////
36

37     public SortFilterModel(TableModel tm) {
38         super(tm);
39         setModel(tm);
40     }
41
42     //~ Methods ////////////////////////////////////////////////////////////////
43

44     public boolean isCellEditable(int r, int c) {
45         if ((rows.size() > 0) && (r < rows.size())) {
46             return model.isCellEditable(((Row) rows.get(r)).index, c);
47         }
48
49         return false;
50     }
51
52     public void setModel(TableModel tm) {
53         super.setModel(tm);
54         rows.ensureCapacity(model.getRowCount());
55         model.addTableModelListener(this);
56         sortColumn = -1;
57         dirty = true;
58         refresh();
59     }
60
61     public int getSortedColumnNumber() {
62         return sortColumn;
63     }
64
65     public String JavaDoc getSortedDirection(int columnNumber) {
66         if (getSortedColumnNumber() < 0) {
67             return NONE;
68         }
69
70         return _sortDirection;
71     }
72
73     public void setValueAt(Object JavaDoc aValue, int r, int c) {
74         if ((rows.size() > 0) && (r < rows.size())) {
75             model.setValueAt(aValue, ((Row) rows.get(r)).index, c);
76         }
77     }
78
79     /* compute the moved row for the three methods that access
80        model elements
81     */

82     public Object JavaDoc getValueAt(int r, int c) {
83         if ((rows.size() > 0) && (r < rows.size())) {
84             return model.getValueAt(((Row) rows.get(r)).index, c);
85         }
86
87         return null;
88     }
89
90     public void addMouseListener(final JTable table) {
91         table.getTableHeader().addMouseListener(new MouseAdapter JavaDoc() {
92             public void mouseClicked(MouseEvent JavaDoc event) {
93                 // check for double click
94
if (event.getClickCount() < 2) {
95                     return;
96                 }
97
98                 // find column of click and
99
int tableColumn = table.columnAtPoint(event.getPoint());
100
101                 // translate to table model index and sort
102
int modelColumn = table.convertColumnIndexToModel(tableColumn);
103                 sort(modelColumn);
104             }
105         });
106     }
107
108     public void removeRow(int rowNum) throws ArrayIndexOutOfBoundsException JavaDoc, IllegalStateException JavaDoc {
109         int mappedRow = ((Row) rows.get(rowNum)).index;
110         super.removeRow(mappedRow);
111     }
112
113     public void sort(int columnNumber, String JavaDoc direction) {
114         _sortDirection = ASC;
115         dirty = true;
116         sort(columnNumber);
117
118         if (DESC.equals(direction)) {
119             sort(columnNumber);
120             _sortDirection = DESC;
121         }
122     }
123
124     /**
125      * Implements the TableModelListener interface to receive
126      * notifications of * changes to the table model. SortFilterModel needs
127      * to receive events for adding and removing rows.
128      */

129     public void tableChanged(TableModelEvent JavaDoc e) {
130         dirty = true;
131         refresh();
132         fireTableChanged(e);
133     }
134
135     protected void refresh() {
136         rows.clear();
137
138         for (int i = 0; i < model.getRowCount(); i++) {
139             rows.add(new Row(i));
140         }
141
142         if (dirty && (sortColumn > -1)) {
143             sort(sortColumn);
144         }
145     }
146
147     protected void sort(int c) {
148         boolean sorted = (sortColumn == c);
149         sortColumn = c;
150
151         if (dirty || !sorted) {
152             Collections.sort(rows);
153             dirty = false;
154         } else {
155             Collections.reverse(rows);
156         }
157
158         fireTableDataChanged();
159     }
160
161     //~ Inner Classes //////////////////////////////////////////////////////////
162

163     /* this inner class holds the index of the model row
164      * Rows are compared by looking at the model row entries
165      * in the sort column
166      */

167     private class Row implements Comparable JavaDoc {
168         public int index;
169
170         public Row(int index) {
171             this.index = index;
172         }
173
174         public int compareTo(Object JavaDoc other) {
175             Row otherRow = (Row) other;
176             Object JavaDoc a = model.getValueAt(index, sortColumn);
177             Object JavaDoc b = model.getValueAt(otherRow.index, sortColumn);
178
179             boolean areTheyCompareable = (a instanceof Comparable JavaDoc && b instanceof Comparable JavaDoc && (a.getClass() == b.getClass()));
180
181             if (areTheyCompareable) {
182                 return ((Comparable JavaDoc) a).compareTo((Comparable JavaDoc) b);
183             } else {
184                 return a.toString().compareTo(b.toString());
185             }
186         }
187     }
188 }
189
Popular Tags