1 19 20 package org.objectweb.jac.aspects.gui; 21 22 import java.util.Arrays ; 23 import java.util.Collection ; 24 import java.util.Iterator ; 25 import java.util.Vector ; 26 import javax.swing.event.TreeSelectionEvent ; 27 import javax.swing.tree.DefaultTreeModel ; 28 import javax.swing.tree.TreePath ; 29 import org.apache.log4j.Logger; 30 import org.objectweb.jac.core.NameRepository; 31 import org.objectweb.jac.core.rtti.ClassItem; 32 import org.objectweb.jac.core.rtti.ClassRepository; 33 import org.objectweb.jac.core.rtti.CollectionItem; 34 import org.objectweb.jac.core.rtti.FieldItem; 35 import org.objectweb.jac.core.rtti.MetaItem; 36 37 40 41 public class TreeModel extends DefaultTreeModel implements TreeView { 42 static Logger logger = Logger.getLogger("gui.treeview"); 43 static Logger loggerPerf = Logger.getLogger("perf"); 44 45 Vector treeObjects = new Vector (); 46 Vector treeNodes = new Vector (); 47 boolean showRelations; 48 String rootObjects; 49 50 57 public TreeModel(RootNode rootNode, 58 String rootObjects, boolean showRelations) { 59 super(rootNode); 60 this.showRelations = showRelations; 61 this.rootObjects = rootObjects; 62 rootNode.setModel(this); 63 initTree(this,rootObjects,showRelations); 64 } 65 66 public void setRootNode(AbstractNode root) { 67 setRoot(root); 68 } 69 70 public void addNode(AbstractNode parent, AbstractNode child) { 71 logger.debug("addNode "+child+" to "+parent); 72 parent.add(child); 73 } 74 75 public void setSelection(TreePath selection) { 76 Object [] listeners = listenerList.getListenerList(); 77 TreeSelectionEvent e = null; 78 for (int i = listeners.length-2; i>=0; i-=2) { 81 if (listeners[i]==TreeListener.class) { 82 ((TreeListener)listeners[i+1]).setSelection(selection); 84 } 85 } 86 } 87 88 public void addTreeListener(TreeListener listener) { 89 listenerList.add(TreeListener.class,listener); 90 } 91 92 public void unregisterEvents() { 93 Iterator i = treeNodes.iterator(); 94 while (i.hasNext()) { 95 AbstractNode node = (AbstractNode)i.next(); 96 node.unregisterEvents(); 97 } 98 } 99 100 106 public static void addNodes(TreeView tree, ObjectNode rootNode, 107 Object o, boolean showRelations) { 108 logger.debug("addNodes("+rootNode+","+o+", showRelations="+showRelations+")..."); 109 if (o==null) 110 return; 111 long start = System.currentTimeMillis(); 112 ClassItem cli = ClassRepository.get().getClass(o); 114 FieldItem[] rels = (FieldItem[])cli.getAttribute(GuiAC.TREE_ATTRIBUTES_ORDER); 115 Collection related = null; 116 rootNode.setChildrenUptodate(true); 117 if (rels==null) 118 return; 119 for (int itRel=0; itRel<rels.length; itRel++) { 120 FieldItem rel = rels[itRel]; 121 if (!GuiAC.isVisible(o,(MetaItem)rel)) 122 continue; 123 if (rel instanceof CollectionItem) { 124 CollectionItem collection = (CollectionItem)rel; 125 Collection cRel = collection.getActualCollectionThroughAccessor(o); 126 related = cRel; 127 128 if (showRelations && 129 collection.getAttribute(GuiAC.HIDDEN_TREE_RELATION)==null) { 130 RelationNode relationNode = 132 new RelationNode(tree,o,collection); 133 logger.debug("adding relation node for collection "+collection.getName()); 134 Utils.registerCollection(o,collection,relationNode); 135 rootNode.add(relationNode); 136 Iterator it = related.iterator(); 137 while (it.hasNext()) { 138 Object newObject = it.next(); 139 ObjectNode newNode = new ObjectNode(tree,newObject,o,collection,showRelations); 140 newNode.setLeaf(isLeafNode(tree,newNode,o,showRelations)); 141 relationNode.add(newNode); 142 } 143 } else { 144 logger.debug("adding nodes for collection "+collection.getName()+": "+ 145 Arrays.asList(related.toArray())); 146 Utils.registerCollection(o,collection,rootNode); 147 Iterator it = related.iterator(); 149 while (it.hasNext()) { 150 Object newObject = it.next(); 151 logger.debug("adding node for collection "+ 152 collection.getName()+": "+newObject); 153 ObjectNode newNode = new ObjectNode(tree,newObject,o,collection,showRelations); 154 newNode.setLeaf(isLeafNode(tree,newNode,newObject,showRelations)); 155 rootNode.addNode(newNode); 156 } 157 } 158 } else if (rel instanceof FieldItem) { 159 if (showRelations && 160 rel.getAttribute(GuiAC.HIDDEN_TREE_RELATION)==null) 161 { 162 RelationNode relationNode = 163 new RelationNode(tree,o,rel); 164 rootNode.add(relationNode); 165 Object relatedObject = rel.getThroughAccessor(o); 166 if (relatedObject!=null) { 167 ObjectNode newNode = new ObjectNode(tree,relatedObject,o,rel,showRelations); 168 newNode.setLeaf(isLeafNode(tree,newNode,relatedObject,showRelations)); 169 relationNode.add(newNode); 170 } 171 } else { 172 Object relatedObject = rel.getThroughAccessor(o); 173 if (relatedObject!=null) { 174 ObjectNode newNode = new ObjectNode(tree,relatedObject,o,rel,showRelations); 175 newNode.setLeaf(isLeafNode(tree,newNode,relatedObject,showRelations)); 176 rootNode.addNode(newNode); 177 } 178 } 179 } 180 } 181 loggerPerf.info("Added nodes in "+(System.currentTimeMillis()-start)+"ms"); 182 } 183 184 185 194 public static boolean isLeafNode(TreeView tree, ObjectNode node, 195 Object o, boolean showRelations) { 196 if (o==null) 197 return true; 198 ClassItem cli = ClassRepository.get().getClass(o); 200 FieldItem[] rels = (FieldItem[])cli.getAttribute(GuiAC.TREE_ATTRIBUTES_ORDER); 201 Collection related = null; 202 boolean isLeaf=true; 203 if (rels==null) 204 return true; 205 for (int itRel=0; itRel<rels.length; itRel++) { 206 FieldItem rel = rels[itRel]; 207 if (!GuiAC.isVisible(o,(MetaItem)rel)) 208 continue; 209 if (rel instanceof CollectionItem) { 210 CollectionItem collection = (CollectionItem)rel; 211 Utils.registerCollection(o,collection,node); 212 if (isLeaf && 213 !collection.getActualCollectionThroughAccessor(o).isEmpty()) { 214 isLeaf=false; 218 } 219 } else { 220 Utils.registerField(o,rel,node); 221 if (isLeaf && rel.getThroughAccessor(o)!=null) { 222 isLeaf=false; 223 } 224 } 225 } 226 return isLeaf; 227 } 228 229 235 public static void initTree(TreeView tree, String rootObjects, 236 boolean showRelations) { 237 logger.debug("initTree "+rootObjects+"..."); 238 239 AbstractNode rootNode = new RootNode(); 241 tree.setRootNode(rootNode); 242 Iterator it = NameRepository.getObjects(rootObjects).iterator(); 243 while (it.hasNext()) { 244 Object object = it.next(); 245 ObjectNode objectNode = new ObjectNode(tree,object,null,null,showRelations); 246 rootNode.add(objectNode); 247 objectNode.setLeaf(isLeafNode(tree,objectNode,object,showRelations)); 248 } 249 250 logger.debug("initTree "+rootObjects+" DONE"); 251 } 252 253 } 254 | Popular Tags |