KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > table > SortedTableModel


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.table;
14
15 import java.util.Arrays JavaDoc;
16 import java.util.Comparator JavaDoc;
17
18 /**
19  * a table model decorator that sorts the underlying table model
20  */

21
22 class SortedTableModel extends TableModelDecorator {
23   TableRow[] data = null;
24
25   public SortedTableModel() {
26   }
27
28   public SortedTableModel(TableModel model) {
29     super(model);
30   }
31
32
33   public int getRowCount() {
34     if (data == null)
35       return super.getRowCount();
36     return data.length;
37   }
38
39   public TableRow getRow(int index) {
40     if (data == null)
41       return super.getRow(index);
42     return data[index];
43   }
44
45   public void unSort() {
46     data = null;
47   }
48
49   public void sort(Comparator JavaDoc compare) {
50     int N = super.getRowCount();
51     data = new TableRow[N];
52     for (int i = 0; i < N; i++)
53       data[i] = super.getRow(i);
54     Arrays.sort(data, compare);
55   }
56
57   public void tableModelChanged(TableModelChangeEvent event) {
58     data = null;
59   }
60
61 }
62
Popular Tags