1 56 package org.objectstyle.cayenne.modeler.util; 57 58 import java.util.Arrays ; 59 import java.util.Collection ; 60 import java.util.Collections ; 61 import java.util.HashMap ; 62 import java.util.Iterator ; 63 import java.util.Map ; 64 65 import javax.swing.event.TreeModelListener ; 66 import javax.swing.tree.TreeModel ; 67 import javax.swing.tree.TreePath ; 68 69 import org.objectstyle.cayenne.map.Attribute; 70 import org.objectstyle.cayenne.map.Entity; 71 import org.objectstyle.cayenne.map.Relationship; 72 73 79 public class EntityTreeModel implements TreeModel { 80 protected Entity root; 81 protected Map sortedChildren; 82 83 protected boolean hideAttributes; 86 87 public EntityTreeModel(Entity root) { 88 this.root = root; 89 sortedChildren = Collections.synchronizedMap(new HashMap ()); 90 } 91 92 public Object getRoot() { 93 return root; 94 } 95 96 public boolean isHideAttributes() { 97 return hideAttributes; 98 } 99 100 public void setHideAttributes(boolean hideAttributes) { 101 this.hideAttributes = hideAttributes; 102 } 103 104 public Object getChild(Object node, int index) { 105 return sortedChildren(node)[index]; 106 } 107 108 public int getChildCount(Object node) { 109 return (node instanceof Attribute) ? 0 : sortedChildren(node).length; 110 } 111 112 public boolean isLeaf(Object node) { 113 return getChildCount(node) == 0; 114 } 115 116 public void valueForPathChanged(TreePath arg0, Object arg1) { 117 } 119 120 public int getIndexOfChild(Object node, Object child) { 121 if (node instanceof Attribute) { 122 return -1; 123 } 124 125 return Arrays.binarySearch( 128 sortedChildren(node), 129 child, 130 Comparators.getNamedObjectComparator()); 131 } 132 133 public void addTreeModelListener(TreeModelListener listener) { 134 } 136 137 public void removeTreeModelListener(TreeModelListener listener) { 138 } 140 141 private Object [] sortedChildren(Object node) { 142 Entity entity = entityForNonLeafNode(node); 143 144 if(entity == null) { 146 return new Object [0]; 147 } 148 149 synchronized (sortedChildren) { 150 String key = entity.getName(); 151 Object [] sortedForNode = (Object []) sortedChildren.get(key); 152 153 if (sortedForNode == null) { 154 Collection attributes = entity.getAttributes(); 155 Collection relationships = entity.getRelationships(); 156 157 int alen = (hideAttributes) ? 0 : attributes.size(); 159 int rlen = relationships.size(); 160 sortedForNode = new Object [alen + rlen]; 161 162 if (!hideAttributes) { 163 Iterator ait = attributes.iterator(); 164 for (int i = 0; i < alen; i++) { 165 sortedForNode[i] = ait.next(); 166 } 167 } 168 169 Iterator rit = relationships.iterator(); 170 for (int i = 0; i < rlen; i++) { 171 sortedForNode[alen + i] = rit.next(); 172 } 173 174 Arrays.sort(sortedForNode, Comparators.getEntityChildrenComparator()); 175 sortedChildren.put(key, sortedForNode); 176 } 177 178 return sortedForNode; 179 } 180 } 181 182 private Entity entityForNonLeafNode(Object node) { 183 if (node instanceof Entity) { 184 return (Entity) node; 185 } 186 else if (node instanceof Relationship) { 187 return ((Relationship) node).getTargetEntity(); 188 } 189 190 String className = (node != null) ? node.getClass().getName() : "null"; 191 throw new IllegalArgumentException ("Unexpected non-leaf node: " + className); 192 } 193 } 194 | Popular Tags |