1 19 20 package org.objectweb.jac.aspects.gui; 21 22 import java.util.Hashtable ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import javax.swing.tree.MutableTreeNode ; 26 import javax.swing.tree.TreePath ; 27 import org.apache.log4j.Logger; 28 import org.objectweb.jac.core.Collaboration; 29 import org.objectweb.jac.core.rtti.ClassRepository; 30 import org.objectweb.jac.core.rtti.CollectionItem; 31 import org.objectweb.jac.core.rtti.FieldItem; 32 import org.objectweb.jac.util.Stack; 33 34 36 37 public class ObjectNode extends AbstractNode 38 implements ObjectUpdate, FieldUpdate, CollectionUpdate 39 { 40 static Logger logger = Logger.getLogger("gui.treeview"); 41 static Logger loggerEvents = Logger.getLogger("gui.events"); 42 static Logger loggerCol = Logger.getLogger("gui.collectionupdate"); 43 44 FieldItem relation; 45 Object substance; 46 47 55 public ObjectNode(TreeView model, Object value, 56 Object substance, FieldItem relation, 57 boolean showRelations) 58 { 59 super(model,value, showRelations); 60 this.substance = substance; 61 this.relation = relation; 62 Utils.registerObject(value,this); 63 if (!(relation instanceof CollectionItem)) { 64 Utils.registerField(value,relation,this); 65 } 66 if (GuiAC.getGraphicContext()!=null) 67 context.addAll(GuiAC.getGraphicContext()); 68 if (relation!=null) 69 context.push(relation); 70 rebuildData(); 71 } 72 73 74 Hashtable relationIndices; 75 76 81 public int addNode(ObjectNode node) { 82 logger.debug("Inserting "+node.getText()+"..."); 83 FieldItem relation = node.getRelation(); 84 if (relationIndices==null) 85 relationIndices = new Hashtable (); 86 Integer relIndex = (Integer )relationIndices.get(relation); 87 if (relIndex==null) { 88 relIndex = new Integer (getChildCount()); 89 relationIndices.put(relation,relIndex); 90 } 91 int i; 93 for (i=relIndex.intValue(); i<getChildCount(); i++) { 94 ObjectNode current = (ObjectNode)getChildAt(i); 95 logger.debug(" current "+current); 96 if (current.getText().compareToIgnoreCase(node.getText())>0) 97 break; 98 } 99 logger.debug("Inserting "+node.getText()+" at "+relation.getName()+"["+i+"]"); 100 insert(node, i); 101 102 Iterator it = relationIndices.entrySet().iterator(); 104 while (it.hasNext()) { 105 Map.Entry entry = (Map.Entry )it.next(); 106 int index = ((Integer )entry.getValue()).intValue(); 107 if (index>=i && entry.getKey()!=relation) { 108 entry.setValue(new Integer (index+1)); 109 } 110 } 111 return i; 112 } 113 114 public void removeAllChildren() { 115 super.removeAllChildren(); 116 if (relationIndices!=null) 117 relationIndices.clear(); 118 } 119 120 Stack context = new Stack(); 121 122 125 public FieldItem getRelation() { 126 return relation; 127 } 128 129 131 public Object getSubstance() { 132 return substance; 133 } 134 135 137 138 protected void rebuildData() { 139 Object object = getUserObject(); 140 logger.debug("refresh("+object+")"); 141 if (object==null) { 142 icon = null; 143 text = "<null>"; 144 tooltip = null; 145 } else { 146 text = GuiAC.toString(object,context); 147 tooltip = GuiAC.getToolTip(object,context); 148 logger.debug("text="+text); 149 icon = GuiAC.getIcon(ClassRepository.get().getClass(object),object); 150 } 151 } 152 153 155 156 public void unregisterEvents() { 157 Object value = getUserObject(); 158 Utils.unregister(value,this); 159 } 160 161 168 protected ObjectNode findNode(FieldItem relation, Object userObject) { 169 logger.debug("Looking for node "+relation+" -> "+userObject); 170 for (int i=0; i<getChildCount(); i++) { 171 if (getChildAt(i) instanceof ObjectNode) { 172 ObjectNode current = (ObjectNode)getChildAt(i); 173 if (current.getUserObject()==userObject && 174 current.getRelation()==relation) 175 return current; 176 } 177 } 178 return null; 179 } 180 181 183 public void objectUpdated(Object substance, Object param) { 184 rebuildData(); 185 model.nodeChanged(this); 186 } 187 188 190 public void fieldUpdated(Object substance, FieldItem collection, 191 Object value, Object param) { 192 logger.debug("fieldUpdated on "+this); 193 rebuildData(); 194 TreeModel.addNodes(model,this,getUserObject(),showRelations); 195 ObjectNode newNode = findNode(collection,value); 196 if (newNode!=null) { 198 model.setSelection(new TreePath (newNode.getPath())); 199 } 200 model.nodeChanged(this); 201 } 202 203 205 public void onChange(Object substance, CollectionItem collection, 206 Object value, Object param) { 207 loggerCol.debug("ObjectNode collectionUpdated "+collection.getLongName()); 208 int[] indices = new int[getChildCount()]; 210 AbstractNode[] removedNodes = new AbstractNode[getChildCount()]; 211 for(int i=0; i<indices.length; i++) { 212 indices[i] = i; 213 removedNodes[i] = (AbstractNode)getChildAt(i); 214 } 215 removeAllChildren(); 216 model.nodesWereRemoved(this,indices,removedNodes); 217 218 TreeModel.addNodes(model,this,getUserObject(),showRelations); 220 221 indices = new int[getChildCount()]; 222 for(int i=0;i<indices.length;i++) { 223 indices[i] = i; 224 } 225 model.nodesWereInserted(this,indices); 226 } 227 228 public void onAdd(Object substance, CollectionItem collection, 229 Object value, Object added, Object param) { 230 loggerCol.debug("onAdd "+collection.getLongName()+" "+added+" - "+areChildrenUptodate); 231 ObjectNode newNode; 232 if (areChildrenUptodate) { 233 newNode = new ObjectNode(model,added,substance,collection,showRelations); 234 newNode.setLeaf(TreeModel.isLeafNode(model,newNode,added,showRelations)); 235 int pos = addNode(newNode); 236 model.nodesWereInserted(this,new int[] {pos}); 237 setChildrenUptodate(true); 238 } else { 239 TreeModel.addNodes(model,this,getUserObject(),showRelations); 240 int[] indices = new int[getChildCount()]; 241 for(int i=0; i<indices.length; i++) { 242 indices[i] = i; 243 } 244 newNode = findNode(collection,added); 245 model.nodesWereInserted(this,indices); 246 } 247 if (newNode!=null) { 248 model.setSelection(new TreePath (newNode.getPath())); 249 } 250 } 251 252 public void onRemove(Object substance, CollectionItem collection, 253 Object value, Object removed, Object param) { 254 loggerCol.debug("onRemove "+collection.getLongName()+" "+removed); 255 AbstractNode removedNode = 256 (AbstractNode)Collaboration.get().getAttribute(GuiAC.REMOVED_NODE); 257 if (removedNode!=null) { 258 loggerEvents.debug("removing = "+removedNode+" from "+this); 259 int removedIndex = getIndex(removedNode); 260 remove(removedNode); 261 model.nodesWereRemoved( 262 this, 263 new int[] {removedIndex}, 264 new Object [] {removedNode}); 265 } else { 266 onChange(substance,collection,value,param); 267 } 268 } 269 270 public void updateChildren() { 271 if (!areChildrenUptodate()) { 272 loggerEvents.debug("updateChildren "+this); 273 TreeModel.addNodes(model,this,getUserObject(),showRelations); 274 int[] indices = new int[getChildCount()]; 275 for(int i=0; i<indices.length; i++) { 276 indices[i] = i; 277 } 278 model.nodesWereInserted(this,indices); 279 setChildrenUptodate(true); 280 } else { 281 loggerEvents.debug("children are uptodate for "+this); 282 } 283 } 284 285 public String toString() { 286 return substance+"."+relation+" -> "+getUserObject(); 287 } 288 } 289 290 | Popular Tags |