package com.vci.client.ui.treeTable; import java.util.Enumeration; import java.util.Hashtable; import java.util.Vector; import javax.swing.tree.TreeNode; public class TreeTableModel extends AbstractTreeTableModel { private String[] columnNames = null; private Class[] columnClasses = null; public TreeTableModel(Object root, String[] columnNames, Class[] columnClasses) { super(root); if (columnNames == null || columnClasses == null || (columnNames.length != columnClasses.length)) { columnNames = new String[0]; columnClasses = new Class[0]; } this.columnNames = columnNames; this.columnClasses = columnClasses; } public int getColumnCount() { return columnNames.length; } public String getColumnName(int column) { return this.columnNames[column]; } public Class getColumnClass(int column) { return this.columnClasses[column]; } public Object getValueAt(Object node, int column) { TreeTableNode treeNode = (TreeTableNode) node; try { return treeNode.getPropertyValueByName(columnNames[column]); } catch (Exception e) { e.printStackTrace(); } return null; } public Object[] getChildren(Object parent) { TreeTableNode baseNode = (TreeTableNode) parent; return baseNode.getChildren(); } public int getChildCount(Object parent) { Object[] children = getChildren(parent); int i = (children == null) ? 0 : children.length; return i; } public Object getChild(Object node, int i) { return getChildren(node)[i]; } /** * get the root node * @return */ public TreeTableNode getRootNode() { return (TreeTableNode) root; } public class TreeTableNode implements TreeNode{ protected Hashtable htTable = null; private Object obj = null; protected TreeTableNode parent; protected Vector children; private boolean isExpand = false; private boolean isLeaf = false; public TreeTableNode(TreeTableNode parent, Object obj) { this.parent = parent; this.obj = obj; } public TreeTableNode(Object obj) { this(null, obj); } public void setPropertyValueByName(String strName,Object obj) { if (htTable == null) { htTable = new Hashtable(); } htTable.put(strName,obj); } public Object getPropertyValueByName(String strPropertyName) { if (this.htTable == null) { return ""; } else { if (this.htTable.containsKey(strPropertyName)) { return htTable.get(strPropertyName); } else { return ""; } } } public void setPropertyValues(Hashtable ht) { htTable = (Hashtable)(ht.clone()); nodeChanged(); } public void addChild(TreeTableNode node) { if (this.children == null) { children = new Vector(); } children.add(node); TreeTableNode[] path = getPath(); fireTreeStructureChanged(TreeTableModel.this, path, null, null); } protected TreeTableNode[] getPathToRoot(TreeNode aNode, int depth) { TreeTableNode[] retNodes; if(aNode == null) { if(depth == 0) { return null; } else { retNodes = new TreeTableNode[depth]; } } else { depth++; retNodes = getPathToRoot(aNode.getParent(), depth); retNodes[retNodes.length - depth] = (TreeTableNode) aNode; } return retNodes; } public boolean loadedChildren() { return (children != null); } public void setChildren(Vector newNode) { children = newNode; } public TreeTableNode[] getChildren() { if (this.children == null ) { return null; } TreeTableNode[] pns = new TreeTableNode[this.children.size()]; for(int i = 0; i < pns.length; i++) { pns[i] = (TreeTableNode) (this.children.get(i)); } return pns; } protected void nodeChanged() { TreeTableNode parent = (TreeTableNode) getParent(); if (parent != null) { TreeTableNode[] path = parent.getPath(); int[] index = {getIndexOfChild(parent, this)}; Object[] children = { this }; fireTreeNodesChanged(TreeTableModel.this, path, index,children); } } public void removeChild(TreeTableNode pnChild) { if (this.children == null) { return; } try { this.children.remove(pnChild); } catch(Exception e) { e.printStackTrace(); } } public void removeAll() { if (this.children != null) { for(int i = this.children.size()-1; i >= 0; i--) { TreeTableNode pn = ( TreeTableNode ) this.children.get(i); pn.removeAll(); removeChild(pn); } } } public TreeTableNode[] getPath() { return getPathToRoot(this, 0); } public void refreshChildren() { if (this.children == null) { this.children = new Vector(); } this.children.clear(); } public String toString() { return obj.toString(); } public Object getObj() { return obj; } public void setObj(Object obj) { this.obj = obj; } public Enumeration children() { // TODO Auto-generated method stub return null; } public boolean getAllowsChildren() { // TODO Auto-generated method stub return false; } public TreeNode getChildAt(int childIndex) { if (children == null) { return null; } int size = children.size(); if (size == 0) { return null; } if (childIndex < size) { return (TreeTableNode)children.get(childIndex); } else { return null; } } public int getChildCount() { // TODO Auto-generated method stub return 0; } public int getIndex(TreeNode node) { // TODO Auto-generated method stub return 0; } public TreeNode getParent() { return this.parent; } public void setLeaf(boolean isLeaf) { this.isLeaf = isLeaf; } public boolean isLeaf() { return isLeaf; } /** * @return Returns the isExpand. */ public boolean isExpand() { return isExpand; } /** * @param isExpand The isExpand to set. */ public void setExpand(boolean isExpand) { this.isExpand = isExpand; } } }