001    package org.shiftone.jrat.provider.tree.ui.hierarchy;
002    
003    import org.jdesktop.swingx.treetable.AbstractTreeTableModel;
004    import org.shiftone.jrat.desktop.util.Table;
005    import org.shiftone.jrat.provider.tree.ui.hierarchy.nodes.HierarchyNode;
006    import org.shiftone.jrat.provider.tree.ui.hierarchy.nodes.MethodHierarchyNode;
007    import org.shiftone.jrat.provider.tree.ui.hierarchy.nodes.PackageHierarchyNode;
008    
009    import java.util.List;
010    
011    /**
012     * @author jeff@shiftone.org (Jeff Drost)
013     */
014    public class HierarchyTreeTableModel extends AbstractTreeTableModel {
015    
016        public static final Table TABLE = new Table();
017        public static final Table.Column CLASS = TABLE.column("Class");
018        public static final Table.Column METHODS = TABLE.column("Methods");
019        public static final Table.Column TOTAL_EXITS = TABLE.column("Exists", false);
020        public static final Table.Column UNCALLED = TABLE.column("Uncalled", false);
021        public static final Table.Column COVERAGE = TABLE.column("Coverage %", false);
022        public static final Table.Column EXCEPTIONS = TABLE.column("Exceptions", false);
023        public static final Table.Column ERROR_RATE = TABLE.column("Error Rate", false);
024        public static final Table.Column TOTAL = TABLE.column("Total ms", false);
025        public static final Table.Column TOTAL_METHOD = TABLE.column("Total Method ms");
026        public static final Table.Column PERCENT_METHOD = TABLE.column("Method Time %");
027    
028        private final PackageHierarchyNode root;
029    
030        public HierarchyTreeTableModel(PackageHierarchyNode root) {
031            this.root = root;
032        }
033    
034        public static List getColumns() {
035            return TABLE.getColumns();
036        }
037    
038        public Class getColumnClass(int i) {
039            return TABLE.getColumn(i).getType();
040        }
041    
042        public int getColumnCount() {
043            return TABLE.getColumnCount();
044        }
045    
046        public String getColumnName(int i) {
047            return TABLE.getColumn(i).getName();
048        }
049    
050        public Object getValueAt(Object o, int i) {
051    
052            HierarchyNode node = (HierarchyNode) o;
053    
054            if (CLASS.getIndex() == i) {
055                return o.toString();
056            }
057            if (METHODS.getIndex() == i) {
058                return getTotalMethods(node);
059            }
060            if (UNCALLED.getIndex() == i) {
061                return getUncalledMethods(node);
062            }
063            if (COVERAGE.getIndex() == i) {
064                return node.getCoverage();
065            }
066            if (TOTAL.getIndex() == i) {
067                return new Long(node.getTotalDuration());
068            }
069            if (TOTAL_METHOD.getIndex() == i) {
070                return node.getTotalMethodDuration();
071            }
072            if (PERCENT_METHOD.getIndex() == i) {
073                return node.getTotalMethodPercent();
074            }
075            if (TOTAL_EXITS.getIndex() == i) {
076                return new Long(node.getTotalExits());
077            }
078            if (EXCEPTIONS.getIndex() == i) {
079                return new Long(node.getTotalErrors());
080            }
081            if (ERROR_RATE.getIndex() == i) {
082                return node.getErrorRate();
083            }
084            return null;
085        }
086    
087        private static Integer getTotalMethods(HierarchyNode node) {
088            return (node instanceof MethodHierarchyNode) ? null : new Integer(node.getTotalMethods());
089        }
090    
091        private static Integer getUncalledMethods(HierarchyNode node) {
092            return (node instanceof MethodHierarchyNode) ? null : new Integer(node.getUncalledMethods());
093        }
094    
095        public boolean isCellEditable(Object o, int i) {
096            return false;
097        }
098    
099        public void setValueAt(Object o, Object o1, int i) {
100            throw new UnsupportedOperationException();
101        }
102    
103        public Object getRoot() {
104            return root;
105        }
106    
107        public Object getChild(Object parent, int index) {
108            return node(parent).getChild(index);
109        }
110    
111        public int getChildCount(Object parent) {
112            return node(parent).getChildCount();
113        }
114    
115        public int getIndexOfChild(Object parent, Object child) {
116            return node(parent).getIndexOfChild((HierarchyNode) child);
117        }
118    
119        public boolean isLeaf(Object o) {
120            return node(o).isLeaf();
121        }
122    
123        private HierarchyNode node(Object o) {
124            return (HierarchyNode) o;
125        }
126    }