KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > monitor > client > DisplayTableSorter


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.web.monitor.client;
21
22 import java.util.*;
23 import javax.swing.table.TableModel JavaDoc;
24 import javax.swing.table.AbstractTableModel JavaDoc;
25 import javax.swing.event.TableModelEvent JavaDoc;
26 import javax.swing.event.TableModelListener JavaDoc;
27 import org.openide.util.NbBundle;
28
29 /**
30  * DisplayTableSorter.java
31  *
32  *
33  * Created: Fri Jan 25 13:37:39 2002
34  *
35  * @author Ana von Klopp
36  * @version
37  */

38 public class DisplayTableSorter extends AbstractTableModel JavaDoc implements
39     TableModelListener JavaDoc {
40
41     private int[] index = null;
42     private int sort = DisplayTable.NEUTRAL;
43     protected TableModel JavaDoc model;
44
45     private static final boolean debug = false;
46     
47     public DisplayTableSorter(TableModel JavaDoc model) {
48     setModel(model);
49     resetIndices();
50     }
51
52     // PENDING: we could speed this up considerably given that we can
53
// just reverse the order of the indices if we know that the table
54
// hasn't changed, for example...
55
public void sort(int sort) {
56     
57         this.sort = sort;
58     if(debug) log(" sort: " + String.valueOf(this.sort)); //NOI18N
59
if(sort == DisplayTable.NEUTRAL) {
60         tableChanged(new TableModelEvent JavaDoc(this));
61         return;
62     }
63
64     if(debug) {
65         StringBuffer JavaDoc buf =
66         new StringBuffer JavaDoc("Order of indices before sorting: ");//NOI18N
67
for(int i=0; i<index.length; ++i) {
68         buf.append(String.valueOf(index[i]));
69         buf.append(", "); //NOI18N
70
}
71         log(buf.toString());
72     }
73
74     resetIndices();
75     sort((int[])index.clone(), index, 0, index.length);
76      
77     if(debug) {
78         StringBuffer JavaDoc buf =
79         new StringBuffer JavaDoc("Order of indices after sorting: ");//NOI18N
80
for(int i=0; i<index.length; ++i) {
81         buf.append(String.valueOf(index[i]));
82         buf.append(", "); //NOI18N
83
}
84         log(buf.toString());
85     }
86
87     tableChanged(new TableModelEvent JavaDoc(this));
88     }
89
90     public void sort(int[] from, int[] to, int low, int high) {
91
92     if (high - low < 2) {
93         return;
94     }
95         
96     int middle = (low + high)/2;
97     sort(to, from, low, middle);
98     sort(to, from, middle, high);
99
100     int p = low;
101     int q = middle;
102
103     if (high - low >= 4 && compare(from[middle-1], from[middle]) <= 0) {
104         for (int i = low; i < high; i++) {
105         to[i] = from[i];
106         }
107         return;
108         }
109
110         for (int i = low; i < high; i++) {
111             if (q >= high || (p < middle && compare(from[p], from[q]) <= 0)) {
112                 to[i] = from[p++];
113             }
114             else {
115                 to[i] = from[q++];
116             }
117         }
118     }
119
120     public int compare(int row1, int row2) {
121
122      
123     String JavaDoc str1 = (String JavaDoc)model.getValueAt(row1, 0);
124     String JavaDoc str2 = (String JavaDoc)model.getValueAt(row2, 0);
125
126      
127     if(debug) log(" comparing " + row1 + ":" + str1 + //NOI18N
128
" and " + row2 + ":" + str2); //NOI18N
129

130     int result = str1.compareTo(str2);
131     if(result == 0) return 0;
132     boolean ascending = (sort == DisplayTable.A2Z);
133     return ascending ? result : -result;
134     }
135     
136     
137
138     public void resetIndices() {
139         int rowCount = getRowCount();
140     
141     if(debug) log("rows=" + //NOI18N
142
String.valueOf(rowCount));
143     
144         // Set up a new array of indexes with the right number of elements
145
// for the new data model.
146
index = new int[rowCount];
147
148         // Initialise with the identity mapping.
149
for (int row = 0; row < rowCount; row++) {
150             index[row] = row;
151         }
152     }
153
154
155     public TableModel JavaDoc getModel() {
156         return model;
157     }
158
159     public void setModel(TableModel JavaDoc model) {
160         this.model = model;
161         model.addTableModelListener(this);
162     }
163
164     // By default, implement TableModel by forwarding all messages
165
// to the model.
166

167     public Object JavaDoc getValueAt(int aRow, int aColumn) {
168     if(sort == DisplayTable.NEUTRAL) return model.getValueAt(aRow,
169                                    aColumn);
170     
171     if(debug) {
172         String JavaDoc value = (String JavaDoc)(model.getValueAt(index[aRow], aColumn));
173         value = value + String.valueOf(aRow) + "+" + index[aRow]; //NOI18N
174
return value;
175     }
176         return model.getValueAt(index[aRow], aColumn);
177     }
178         
179     public void setValueAt(Object JavaDoc aValue, int aRow, int aColumn) {
180     if(sort == DisplayTable.NEUTRAL) {
181         model.setValueAt(aValue, aRow, aColumn);
182         return;
183     }
184         model.setValueAt(aValue, index[aRow], aColumn);
185     }
186
187     public int getRowCount() {
188         return (model == null) ? 0 : model.getRowCount();
189     }
190
191     public int getColumnCount() {
192         return (model == null) ? 0 : model.getColumnCount();
193     }
194         
195     public String JavaDoc getColumnName(int aColumn) {
196         return model.getColumnName(aColumn);
197     }
198
199     public Class JavaDoc getColumnClass(int aColumn) {
200         return model.getColumnClass(aColumn);
201     }
202         
203     public boolean isCellEditable(int row, int column) {
204          return model.isCellEditable(row, column);
205     }
206
207     public void tableChanged(TableModelEvent JavaDoc e) {
208         fireTableChanged(e);
209     }
210
211     private void log(String JavaDoc s) {
212     System.out.println("DisplayTableSorter::" + s); //NOI18N
213
}
214     
215 } // DisplayTableSorter
216
Popular Tags