DefaultColumnTreeTableModel.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.gui.table |
![]() |
![]() |
XNap 3 |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* XNap - A P2P framework and client.
*
* See the file AUTHORS for copyright information.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.xnap.gui.table;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
public class DefaultColumnTreeTableModel extends AbstractColumnTreeTableModel
implements SortableModel {
//--- Constant(s) ---
//--- Data field(s) ---
private boolean ascending;
private boolean maintainSortOrder;
private int sortedColumn = -1;
/**
* Stores the data.
*/
private ArrayList rows = new ArrayList();
//--- Constructor(s) ---
public DefaultColumnTreeTableModel()
{
}
//--- Method(s) ---
/**
* Removes all nodes.
*/
void clear()
{
rows.clear();
fireTreeStructureChanged(this, new Object[] { this }, null, null);
}
/**
* Invoked by a {@link TreeTableNode} object when a child item was
* added.
*/
void childInserted(TreeTableNode node, int index, Object child)
{
fireTreeNodesInserted(this, new Object[] { this, node },
new int[] { index },
new Object[] { child });
}
/**
* Invoked by a {@link TreeTableNode} object when a child item has
* changed its state.
*/
void childChanged(TreeTableNode node, int index, Object child)
{
fireTreeNodesChanged(this, new Object[] { this, node },
new int[] { index },
new Object[] { child });
}
/**
* Invoked by a {@link TreeTableNode} object when a child item was
* removed.
*/
void childRemoved(TreeTableNode node, int index, Object child)
{
fireTreeNodesRemoved(this, new Object[] { this, node },
new int[] { index },
new Object[] { child });
}
/**
* Invoked by a {@link TreeTableNode} object when it has
* changed its state.
*/
void nodeChanged(TreeTableNode node)
{
int index = rows.indexOf(node);
if (index != -1) {
fireTreeNodesChanged(this, new Object[] { this },
new int[] { index },
new Object[] { node });
}
}
public void add(TreeTableNode node)
{
rows.add(node);
fireTreeNodesInserted(this, new Object[] { this },
new int[] { rows.size() - 1 },
new Object[] { node });
if (maintainSortOrder && sortedColumn != -1) {
sortByColumn(sortedColumn, ascending, false);
}
}
public Object getChild(Object node, int index)
{
if (node == this) {
return rows.get(index);
}
else {
return ((TreeTableNode)node).getChildAt(index);
}
}
public int getChildCount(Object node)
{
if (node == this) {
return rows.size();
}
else if (node instanceof TreeTableNode) {
return ((TreeTableNode)node).getChildCount();
}
else {
return 0;
}
}
/**
* Returns the node that stores <code>data</code>.
*
* @return -1, if data could not be found
*/
public int indexOfByData(Object data)
{
for (int i = 0; i < rows.size(); i++) {
TreeTableNode node = (TreeTableNode)rows.get(i);
if (node.getData() == data) {
return i;
}
}
return -1;
}
public Object getValueAt(Object node, int column)
{
return null;
}
public Iterator iterator()
{
return rows.iterator();
}
public void remove(TreeTableNode node)
{
int index = rows.indexOf(node);
if (index != -1) {
remove(index);
}
}
public void remove(int index)
{
Object node = rows.get(index);
rows.remove(index);
fireTreeNodesRemoved(this, new Object[] { this },
new int[] { index },
new Object[] { node });
}
public int getSortedColumn()
{
return sortedColumn;
}
public boolean isSortedAscending()
{
return ascending;
}
/**
* Sets the maintain sort order flag.
*/
public void setMaintainSortOrder(boolean newValue)
{
maintainSortOrder = newValue;
}
/**
* Sorts the table by <code>column</code>.
*/
public boolean sortByColumn(int column, boolean ascending, boolean revert)
{
if (column < 0 || column >= getColumnCount()) {
throw new IllegalArgumentException();
}
this.ascending = ascending;
sortedColumn = column;
ArrayList copy = null;
if (revert) {
copy = (ArrayList)rows.clone();
}
// the first column only contains the icon, but the value is from
// the invisible second icon
boolean hack
= (sortedColumn == 0 && getColumnAt(0).getKey().equals("icon"));
Comparator comparator = new TreeNodeComparator(hack ? 1 : sortedColumn);
Collections.sort(rows, comparator);
if (revert && rows.equals(copy)) {
this.ascending = !ascending;
Collections.reverse(rows);
}
for (Iterator i = rows.iterator(); i.hasNext();) {
((TreeTableNode)i.next()).sort(comparator);
}
fireTreeStructureChanged(this, new Object[] { getRoot() }, null, null);
return this.ascending;
}
/**
* Compares {@link TreeTableNode} data objects.
*/
private class TreeNodeComparator implements Comparator
{
private int column;
public TreeNodeComparator(int column)
{
this.column = column;
}
public int compare(Object node1, Object node2)
{
int order = compareByData(node1, node2);
return ascending ? order : -order;
}
public int compareByData(Object node1, Object node2)
{
Class type = getColumnClass(column);
// check for nulls
Object o1 = getValueAt(node1, column);
Object o2 = getValueAt(node2, column);
// If both values are null return 0
// define: null is less than anything
if (o1 == null && o2 == null) {
return 0;
} else if (o1 == null) {
return -1;
} else if (o2 == null) {
return 1;
}
if (type.equals(String.class)) {
String s1 = (String)o1;
String s2 = (String)o2;
int result = s1.compareToIgnoreCase(s2);
// empty strings come last
if (s1.length() == 0 ^ s2.length() == 0) {
result = -result;
}
return result;
}
else if (o1 instanceof Comparable) {
return ((Comparable)o1).compareTo(o2);
}
else if (type == Boolean.class) {
boolean b1 = ((Boolean)o1).booleanValue();
boolean b2 = ((Boolean)o2).booleanValue();
// define: false < true
if (b1 == b2) {
return 0;
} else if (b1) {
return 1;
} else {
return -1;
}
}
else {
// now what?
return o1.toString().compareTo(o2.toString());
}
}
}
}
The table below shows all metrics for DefaultColumnTreeTableModel.java.




