001/* A tree model for Ptolemy II Composites, for use with JTree. 002 003 Copyright (c) 2012-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 ptolemy.actor.CompositeActor; 031import ptolemy.kernel.CompositeEntity; 032 033/////////////////////////////////////////////////////////////////// 034//// CompositeTreeModel 035 036/** 037 A tree model for Ptolemy II models. Nodes in this tree contain 038 only CompositeEntities. 039 040 <p>The indexes of the attributes are 0 to a-1, where a is the 041 number of attributes. The indexes of the ports are a to a+p-1, 042 where p is the number of ports, and so on. 043 Subclasses may return a subset of the attributes, ports, and 044 relations by overriding the protected methods that list these 045 contained objects. 046 047 @author Christopher Brooks, based on FullTreeModel by Steve Neuendorffer and Edward A. Lee 048 @version $Id$ 049 @since Ptolemy II 10.0 050 @Pt.ProposedRating Red (eal) 051 @Pt.AcceptedRating Red (johnr) 052 */ 053public class CompositeTreeModel extends EntityTreeModel { 054 /** Create a new tree model with the specified root. 055 * @param root The root of the tree. 056 */ 057 public CompositeTreeModel(CompositeEntity root) { 058 super(root); 059 } 060 061 /////////////////////////////////////////////////////////////////// 062 //// public methods //// 063 064 /** Get the child of the given parent at the given index. 065 * If the child does not exist, then return null. 066 * In this base class, a child is a contained entity. 067 * @param parent A node in the tree. 068 * @param index The index of the desired child. 069 * @return A node, or null if there is no such child. 070 */ 071 @Override 072 public Object getChild(Object parent, int index) { 073 if (index > getChildCount(parent)) { 074 return null; 075 } 076 077 CompositeEntity entity = (CompositeEntity) parent; 078 return entity.entityList(CompositeActor.class).get(index); 079 } 080 081 /** Return the number of children of the given parent, which in 082 * this base class is the number of contained entities. 083 * @param parent A parent node. 084 * @return The number of contained entities. 085 */ 086 @Override 087 public int getChildCount(Object parent) { 088 if (!(parent instanceof CompositeEntity)) { 089 return 0; 090 } 091 092 CompositeEntity entity = (CompositeEntity) parent; 093 return entity.entityList(CompositeActor.class).size(); 094 } 095 096 /** Return the index of the given child within the given parent. 097 * If the parent is not contained in the child or is not an instance 098 * of CompositeEntity return -1. 099 * @param parent The parent, which is usually a CompositeEntity. 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 if (!(parent instanceof CompositeEntity)) { 106 return -1; 107 } 108 109 CompositeEntity entity = (CompositeEntity) parent; 110 return entity.entityList(CompositeActor.class).indexOf(child); 111 } 112 113 /** Return true if the object is a leaf node. In this base class, 114 * an object is a leaf node if it is not an instance of CompositeEntity. 115 * @param object The object in question. 116 * @return True if the node has no children. 117 */ 118 @Override 119 public boolean isLeaf(Object object) { 120 if (!(object instanceof CompositeEntity)) { 121 return true; 122 } 123 124 // NOTE: The following is probably not a good idea because it 125 // will force evaluation of the contents of a Library prematurely. 126 if (getChildCount(object) == 0) { 127 return true; 128 } 129 return false; 130 } 131 132}