001/* A tree model for Ptolemy II objects, for use with JTree.
002
003 Copyright (c) 2003-2014 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027 */
028package ptolemy.vergil.tree;
029
030import java.util.Collections;
031import java.util.List;
032
033import ptolemy.kernel.CompositeEntity;
034import ptolemy.kernel.util.NamedObj;
035
036///////////////////////////////////////////////////////////////////
037//// ClassAndEntityTreeModel
038
039/**
040 A tree model for Ptolemy II models that includes class definitions and
041 entities. The indexes of the class definitions are 0 to a-1, where a is the
042 number of class definitions.  The indexes of the entities are a to a+p-1,
043 where p is the number of entities.
044
045 @author Edward A. Lee
046 @version $Id$
047 @since Ptolemy II 4.0
048 @Pt.ProposedRating Red (eal)
049 @Pt.AcceptedRating Red (johnr)
050 */
051public class ClassAndEntityTreeModel extends EntityTreeModel {
052    /** Create a new tree model with the specified root.
053     *  @param root The root of the tree.
054     */
055    public ClassAndEntityTreeModel(NamedObj root) {
056        super(root);
057    }
058
059    ///////////////////////////////////////////////////////////////////
060    ////                         public methods                    ////
061
062    /** Get the child of the given parent at the given index.
063     *  If the child does not exist, then return null.
064     *  @param parent A node in the tree.
065     *  @param index The index of the desired child.
066     *  @return A node, or null if there is no such child.
067     */
068    @Override
069    public Object getChild(Object parent, int index) {
070        List classes = _classes(parent);
071        int numClasses = classes.size();
072
073        if (index >= numClasses) {
074            return super.getChild(parent, index - numClasses);
075        } else if (index >= 0) {
076            return classes.get(index);
077        } else {
078            return null;
079        }
080    }
081
082    /** Return the number of children of the given parent.
083     *  This is the number classes and contained
084     *  entities, filtered by the filter specified by setFilter(),
085     *  if any has been specified.
086     *  @param parent A parent node.
087     *  @return The number of children.
088     */
089    @Override
090    public int getChildCount(Object parent) {
091        List classes = _classes(parent);
092        int numClasses = classes.size();
093
094        return numClasses + super.getChildCount(parent);
095    }
096
097    /** Return the index of the given child within the given parent.
098     *  If the parent is not contained in the child, return -1.
099     *  @param parent The parent.
100     *  @param child The child.
101     *  @return The index of the specified child.
102     */
103    @Override
104    public int getIndexOfChild(Object parent, Object child) {
105        List classes = _classes(parent);
106
107        int index = classes.indexOf(child);
108
109        if (index >= 0) {
110            return index;
111        } else {
112            // Object is not a class definition. Defer to the base class.
113            int numClasses = classes.size();
114            index = super.getIndexOfChild(parent, child);
115
116            if (index >= 0) {
117                return index + numClasses;
118            }
119        }
120
121        return -1;
122    }
123
124    /** Return true if the object is a leaf node.  An object is a leaf
125     *  node if it has no children that are instances of one of the classes
126     *  specified by setFilter(), if a filter has been specified.
127     *  @param object The object.
128     *  @return True if the node has no children.
129     */
130    @Override
131    public boolean isLeaf(Object object) {
132        // FIXME: Ignoring setFilter for now.
133        if (_classes(object).size() > 0) {
134            return false;
135        }
136
137        return super.isLeaf(object);
138    }
139
140    ///////////////////////////////////////////////////////////////////
141    ////                         protected methods                 ////
142
143    /** Return the list of classes, or an empty list if there are none.
144     *  Override this method if you wish to show only a subset of the
145     *  classes.
146     *  @param object The object.
147     *  @return A list of classes.
148     */
149    protected List _classes(Object object) {
150        if (!(object instanceof CompositeEntity)) {
151            return Collections.EMPTY_LIST;
152        }
153
154        return ((CompositeEntity) object).classDefinitionList();
155    }
156}