001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.web.monitor.client;
043:
044: import java.util.*;
045: import javax.swing.table.TableModel;
046: import javax.swing.table.AbstractTableModel;
047: import javax.swing.event.TableModelEvent;
048: import javax.swing.event.TableModelListener;
049: import org.openide.util.NbBundle;
050:
051: /**
052: * DisplayTableSorter.java
053: *
054: *
055: * Created: Fri Jan 25 13:37:39 2002
056: *
057: * @author Ana von Klopp
058: * @version
059: */
060: public class DisplayTableSorter extends AbstractTableModel implements
061: TableModelListener {
062:
063: private int[] index = null;
064: private int sort = DisplayTable.NEUTRAL;
065: protected TableModel model;
066:
067: private static final boolean debug = false;
068:
069: public DisplayTableSorter(TableModel model) {
070: setModel(model);
071: resetIndices();
072: }
073:
074: // PENDING: we could speed this up considerably given that we can
075: // just reverse the order of the indices if we know that the table
076: // hasn't changed, for example...
077: public void sort(int sort) {
078:
079: this .sort = sort;
080: if (debug)
081: log(" sort: " + String.valueOf(this .sort)); //NOI18N
082: if (sort == DisplayTable.NEUTRAL) {
083: tableChanged(new TableModelEvent(this ));
084: return;
085: }
086:
087: if (debug) {
088: StringBuffer buf = new StringBuffer(
089: "Order of indices before sorting: ");//NOI18N
090: for (int i = 0; i < index.length; ++i) {
091: buf.append(String.valueOf(index[i]));
092: buf.append(", "); //NOI18N
093: }
094: log(buf.toString());
095: }
096:
097: resetIndices();
098: sort((int[]) index.clone(), index, 0, index.length);
099:
100: if (debug) {
101: StringBuffer buf = new StringBuffer(
102: "Order of indices after sorting: ");//NOI18N
103: for (int i = 0; i < index.length; ++i) {
104: buf.append(String.valueOf(index[i]));
105: buf.append(", "); //NOI18N
106: }
107: log(buf.toString());
108: }
109:
110: tableChanged(new TableModelEvent(this ));
111: }
112:
113: public void sort(int[] from, int[] to, int low, int high) {
114:
115: if (high - low < 2) {
116: return;
117: }
118:
119: int middle = (low + high) / 2;
120: sort(to, from, low, middle);
121: sort(to, from, middle, high);
122:
123: int p = low;
124: int q = middle;
125:
126: if (high - low >= 4
127: && compare(from[middle - 1], from[middle]) <= 0) {
128: for (int i = low; i < high; i++) {
129: to[i] = from[i];
130: }
131: return;
132: }
133:
134: for (int i = low; i < high; i++) {
135: if (q >= high
136: || (p < middle && compare(from[p], from[q]) <= 0)) {
137: to[i] = from[p++];
138: } else {
139: to[i] = from[q++];
140: }
141: }
142: }
143:
144: public int compare(int row1, int row2) {
145:
146: String str1 = (String) model.getValueAt(row1, 0);
147: String str2 = (String) model.getValueAt(row2, 0);
148:
149: if (debug)
150: log(" comparing " + row1 + ":" + str1 + //NOI18N
151: " and " + row2 + ":" + str2); //NOI18N
152:
153: int result = str1.compareTo(str2);
154: if (result == 0)
155: return 0;
156: boolean ascending = (sort == DisplayTable.A2Z);
157: return ascending ? result : -result;
158: }
159:
160: public void resetIndices() {
161: int rowCount = getRowCount();
162:
163: if (debug)
164: log("rows=" + //NOI18N
165: String.valueOf(rowCount));
166:
167: // Set up a new array of indexes with the right number of elements
168: // for the new data model.
169: index = new int[rowCount];
170:
171: // Initialise with the identity mapping.
172: for (int row = 0; row < rowCount; row++) {
173: index[row] = row;
174: }
175: }
176:
177: public TableModel getModel() {
178: return model;
179: }
180:
181: public void setModel(TableModel model) {
182: this .model = model;
183: model.addTableModelListener(this );
184: }
185:
186: // By default, implement TableModel by forwarding all messages
187: // to the model.
188:
189: public Object getValueAt(int aRow, int aColumn) {
190: if (sort == DisplayTable.NEUTRAL)
191: return model.getValueAt(aRow, aColumn);
192:
193: if (debug) {
194: String value = (String) (model.getValueAt(index[aRow],
195: aColumn));
196: value = value + String.valueOf(aRow) + "+" + index[aRow]; //NOI18N
197: return value;
198: }
199: return model.getValueAt(index[aRow], aColumn);
200: }
201:
202: public void setValueAt(Object aValue, int aRow, int aColumn) {
203: if (sort == DisplayTable.NEUTRAL) {
204: model.setValueAt(aValue, aRow, aColumn);
205: return;
206: }
207: model.setValueAt(aValue, index[aRow], aColumn);
208: }
209:
210: public int getRowCount() {
211: return (model == null) ? 0 : model.getRowCount();
212: }
213:
214: public int getColumnCount() {
215: return (model == null) ? 0 : model.getColumnCount();
216: }
217:
218: public String getColumnName(int aColumn) {
219: return model.getColumnName(aColumn);
220: }
221:
222: public Class getColumnClass(int aColumn) {
223: return model.getColumnClass(aColumn);
224: }
225:
226: public boolean isCellEditable(int row, int column) {
227: return model.isCellEditable(row, column);
228: }
229:
230: public void tableChanged(TableModelEvent e) {
231: fireTableChanged(e);
232: }
233:
234: private void log(String s) {
235: System.out.println("DisplayTableSorter::" + s); //NOI18N
236: }
237:
238: } // DisplayTableSorter
|