[6152] | 1 | /* |
---|
| 2 | Copyright 2006 by Sean Luke |
---|
| 3 | Licensed under the Academic Free License version 3.0 |
---|
| 4 | See the file "LICENSE" for more information |
---|
| 5 | */ |
---|
| 6 | |
---|
| 7 | |
---|
| 8 | /* |
---|
| 9 | * Created on Apr 5, 2005 8:24:19 PM |
---|
| 10 | * |
---|
| 11 | * By: spaus |
---|
| 12 | */ |
---|
| 13 | package ec.util; |
---|
| 14 | |
---|
| 15 | import java.util.Arrays; |
---|
| 16 | import java.util.Comparator; |
---|
| 17 | import java.util.Enumeration; |
---|
| 18 | import java.util.Vector; |
---|
| 19 | |
---|
| 20 | import javax.swing.tree.DefaultMutableTreeNode; |
---|
| 21 | import javax.swing.tree.TreeNode; |
---|
| 22 | |
---|
| 23 | /** |
---|
| 24 | * @author spaus |
---|
| 25 | */ |
---|
| 26 | class ParameterDatabaseTreeNode |
---|
| 27 | extends DefaultMutableTreeNode |
---|
| 28 | implements Comparable { |
---|
| 29 | |
---|
| 30 | /** |
---|
| 31 | * |
---|
| 32 | */ |
---|
| 33 | public ParameterDatabaseTreeNode() { |
---|
| 34 | super(); |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | /** |
---|
| 38 | * @param userObject |
---|
| 39 | */ |
---|
| 40 | public ParameterDatabaseTreeNode(Object userObject) { |
---|
| 41 | super(userObject); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | /** |
---|
| 45 | * @param userObject |
---|
| 46 | * @param allowsChildren |
---|
| 47 | */ |
---|
| 48 | public ParameterDatabaseTreeNode(Object userObject, boolean allowsChildren) { |
---|
| 49 | super(userObject, allowsChildren); |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | * @param index |
---|
| 54 | * @param visibleLeaves |
---|
| 55 | * @return |
---|
| 56 | */ |
---|
| 57 | public Object getChildAt(int index, boolean visibleLeaves) { |
---|
| 58 | if (children == null) { |
---|
| 59 | throw new ArrayIndexOutOfBoundsException("node has no children"); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | if (!visibleLeaves) { |
---|
| 63 | int nonLeafIndex = -1; |
---|
| 64 | Enumeration e = children.elements(); |
---|
| 65 | while (e.hasMoreElements()) { |
---|
| 66 | TreeNode n = (TreeNode)e.nextElement(); |
---|
| 67 | if (!n.isLeaf()) { |
---|
| 68 | if (++nonLeafIndex == index) |
---|
| 69 | return n; |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | throw new ArrayIndexOutOfBoundsException("index = "+index+", children = "+getChildCount(visibleLeaves)); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | return super.getChildAt(index); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | /** |
---|
| 80 | * @param visibleLeaves |
---|
| 81 | * @return |
---|
| 82 | */ |
---|
| 83 | public int getChildCount(boolean visibleLeaves) { |
---|
| 84 | if (!visibleLeaves) { |
---|
| 85 | int nonLeafCount = 0; |
---|
| 86 | Enumeration e = children.elements(); |
---|
| 87 | while (e.hasMoreElements()) { |
---|
| 88 | TreeNode n = (TreeNode)e.nextElement(); |
---|
| 89 | if (!n.isLeaf()) ++nonLeafCount; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | return nonLeafCount; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | return super.getChildCount(); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | /* (non-Javadoc) |
---|
| 99 | * @see java.lang.Comparable#compareTo(java.lang.Object) |
---|
| 100 | */ |
---|
| 101 | public int compareTo(Object o) { |
---|
| 102 | ParameterDatabaseTreeNode n = (ParameterDatabaseTreeNode)o; |
---|
| 103 | |
---|
| 104 | return ((Comparable)userObject).compareTo(n.userObject); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | /** |
---|
| 108 | * @param comp |
---|
| 109 | */ |
---|
| 110 | public void sort(Comparator comp) { |
---|
| 111 | if (children == null) |
---|
| 112 | return; |
---|
| 113 | |
---|
| 114 | Object[] childArr = children.toArray(); |
---|
| 115 | Arrays.sort(childArr, comp); |
---|
| 116 | children = new Vector(Arrays.asList(childArr)); |
---|
| 117 | |
---|
| 118 | Enumeration e = children.elements(); |
---|
| 119 | while (e.hasMoreElements()) { |
---|
| 120 | ParameterDatabaseTreeNode n = |
---|
| 121 | (ParameterDatabaseTreeNode)e.nextElement(); |
---|
| 122 | n.sort(comp); |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | } |
---|