alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.web.monitor.client;

import java.util.*;
import javax.swing.table.TableModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener; 
import org.openide.util.NbBundle;

/**
 * DisplayTableSorter.java
 *
 *
 * Created: Fri Jan 25 13:37:39 2002
 *
 * @author Ana von Klopp
 * @version
 */
public class DisplayTableSorter extends AbstractTableModel implements
    TableModelListener { 

    private int[] index = null;
    private int sort = DisplayTable.NEUTRAL; 
    protected TableModel model; 

    private static final boolean debug = false;
    
    public DisplayTableSorter(TableModel model) {
	setModel(model);
	resetIndices();
    }

    // PENDING: we could speed this up considerably given that we can
    // just reverse the order of the indices if we know that the table
    // hasn't changed, for example...
    public void sort(int sort) {
	
        this.sort = sort; 
	if(debug) log(" sort: " + String.valueOf(this.sort)); //NOI18N
	if(sort == DisplayTable.NEUTRAL) { 
	    tableChanged(new TableModelEvent(this)); 
	    return; 
	}

	if(debug) {
	    StringBuffer buf = 
		new StringBuffer("Order of indices before sorting: ");//NOI18N
	    for(int i=0; i= 4 && compare(from[middle-1], from[middle]) <= 0) {
	    for (int i = low; i < high; i++) {
		to[i] = from[i];
	    }
	    return;
        }

        for (int i = low; i < high; i++) {
            if (q >= high || (p < middle && compare(from[p], from[q]) <= 0)) {
                to[i] = from[p++];
            }
            else {
                to[i] = from[q++];
            }
        }
    }

    public int compare(int row1, int row2) {

	 
	String str1 = (String)model.getValueAt(row1, 0);
	String str2 = (String)model.getValueAt(row2, 0);

     
	if(debug) log(" comparing " + row1 + ":" + str1 + //NOI18N
		      " and " + row2 + ":" + str2); //NOI18N

	int result = str1.compareTo(str2); 
	if(result == 0) return 0;
	boolean ascending = (sort == DisplayTable.A2Z);
	return ascending ? result : -result; 
    }
    
    

    public void resetIndices() {
        int rowCount = getRowCount();
	
	if(debug) log("rows=" + //NOI18N
		      String.valueOf(rowCount));
	
        // Set up a new array of indexes with the right number of elements
        // for the new data model.
        index = new int[rowCount];

        // Initialise with the identity mapping.
        for (int row = 0; row < rowCount; row++) {
            index[row] = row;
        }
    }


    public TableModel getModel() {
        return model;
    }

    public void setModel(TableModel model) {
        this.model = model; 
        model.addTableModelListener(this); 
    }

    // By default, implement TableModel by forwarding all messages 
    // to the model. 

    public Object getValueAt(int aRow, int aColumn) {
	if(sort == DisplayTable.NEUTRAL) return model.getValueAt(aRow,
							       aColumn);
	
	if(debug) {
	    String value = (String)(model.getValueAt(index[aRow], aColumn)); 
	    value = value + String.valueOf(aRow) + "+" + index[aRow]; //NOI18N
	    return value;
	}
        return model.getValueAt(index[aRow], aColumn); 
    }
        
    public void setValueAt(Object aValue, int aRow, int aColumn) {
	if(sort == DisplayTable.NEUTRAL) { 
	    model.setValueAt(aValue, aRow, aColumn); 
	    return; 
	}
        model.setValueAt(aValue, index[aRow], aColumn); 
    }

    public int getRowCount() {
        return (model == null) ? 0 : model.getRowCount(); 
    }

    public int getColumnCount() {
        return (model == null) ? 0 : model.getColumnCount(); 
    }
        
    public String getColumnName(int aColumn) {
        return model.getColumnName(aColumn); 
    }

    public Class getColumnClass(int aColumn) {
        return model.getColumnClass(aColumn); 
    }
        
    public boolean isCellEditable(int row, int column) { 
         return model.isCellEditable(row, column); 
    }

    public void tableChanged(TableModelEvent e) {
        fireTableChanged(e);
    }

    private void log(String s) {
	System.out.println("DisplayTableSorter::" + s); //NOI18N
    }
    
} // DisplayTableSorter
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.