001    /*
002      Copyright (C) 2002 Renaud Pawlak, Laurent Martelli
003      
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful, but
010      WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012      Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public
015      License along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017      USA */
018    
019    package org.objectweb.jac.aspects.gui;
020    
021    import org.apache.log4j.Logger;
022    import org.objectweb.jac.core.rtti.CollectionItem;
023    import org.objectweb.jac.core.rtti.FieldItem;
024    
025    /**
026     * This tree node represents a relation. */ 
027    
028    public class RelationNode extends AbstractNode implements CollectionUpdate {
029        static Logger logger = Logger.getLogger("gui.treeview");
030        static Logger loggerEvents = Logger.getLogger("gui.events");
031    
032        Object substance;
033       
034        /**
035         * Constructs the node.
036         *
037         * @param model the tree model
038         * @param substance the object that holds the relation
039         * @param relation the substance relation */
040    
041        public RelationNode(TreeView model, 
042                            Object substance, FieldItem relation) {
043            super(model,relation,true);
044            this.substance = substance;
045            rebuildData();
046        }
047    
048        /**
049         * Rebuild the node's data. */
050    
051        protected void rebuildData() {
052            Object object = getUserObject();
053            logger.debug("rebuildData on "+this);
054            if ( object instanceof FieldItem ) {
055                text = ((FieldItem)object).getName();
056                icon = GuiAC.getIcon((FieldItem)object);
057            }
058        }
059    
060        /**
061         * Unregister all the events this node is listening to. */
062        public void unregisterEvents() {
063            Utils.unregister(substance,this);
064        }
065    
066        // CollectionUpdate interface
067    
068        public void onChange(Object substance, CollectionItem collection, 
069                             Object value, Object param) {
070            loggerEvents.debug("collectionUpdated");
071            int[] indices = new int[getChildCount()];
072            for(int i=0;i<indices.length;i++) {
073                indices[i]=i;
074            }
075            AbstractNode[] removedNodes = new AbstractNode[getChildCount()];
076            for(int i=0;i<indices.length;i++) {
077                removedNodes[i]=(AbstractNode)getChildAt(i);
078            }
079            removeAllChildren();
080            model.nodesWereRemoved(this,indices,removedNodes);
081        }
082    
083        public void onAdd(Object substance, CollectionItem collection, 
084                          Object value, Object added, Object param) {
085            onChange(substance,collection,value,param);
086        }
087    
088        public void onRemove(Object substance, CollectionItem collection, 
089                             Object value, Object removed, Object param) {
090            onChange(substance,collection,value,param);
091        }
092    
093        public String toString() {
094            return "RelationNode["+getUserObject()+"]";
095        }
096    }