001/*
002 * Copyright (c) 1998-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: aschultz $'
006 * '$Date: 2010-12-23 19:01:04 +0000 (Thu, 23 Dec 2010) $' 
007 * '$Revision: 26600 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.gui;
031
032import java.util.Collections;
033import java.util.List;
034import java.util.Vector;
035
036import org.apache.commons.logging.Log;
037import org.apache.commons.logging.LogFactory;
038
039import ptolemy.kernel.CompositeEntity;
040import ptolemy.kernel.Entity;
041import ptolemy.kernel.Port;
042import ptolemy.kernel.util.NamedObj;
043import ptolemy.vergil.tree.ClassAndEntityTreeModel;
044
045/**
046 * This class displays all class and entities as well as directors in the Tree.
047 * 
048 * @author Aaron Schultz
049 * 
050 */
051public class WorkflowOutlineTreeModel extends ClassAndEntityTreeModel {
052        private static final Log log = LogFactory
053                        .getLog(WorkflowOutlineTreeModel.class.getName());
054        private static final boolean isDebugging = log.isDebugEnabled();
055
056        public boolean includeAttributes = false;
057        public boolean includePorts = true;
058        public boolean includeRelations = false;
059        
060        
061        /**
062         * @param root
063         */
064        public WorkflowOutlineTreeModel(CompositeEntity root) {
065                super(root);
066                if (isDebugging) {
067                        log.debug("Construct KeplerVisibleTreeModel");
068                }
069        }
070
071        // /////////////////////////////////////////////////////////////////
072        // // public methods ////
073
074        /**
075         * Get the child of the given parent at the given index. If the child does
076         * not exist, then return null.
077         * 
078         * @param parent
079         *            A node in the tree.
080         * @param index
081         *            The index of the desired child.
082         * @return A node, or null if there is no such child.
083         */
084        public Object getChild(Object parent, int index) {
085
086                List<Object> visibleAtts = _visibleAttributes(parent);
087                int numVisibleAtts = visibleAtts.size();
088
089                if (index >= numVisibleAtts) {
090                        return super.getChild(parent, index - numVisibleAtts);
091                } else if (index >= 0) {
092                        return visibleAtts.get(index);
093                } else {
094                        return null;
095                }
096        }
097
098        /**
099         * Return the number of children of the given parent. This is the number
100         * directors and contained entities.
101         * 
102         * @param parent
103         *            A parent node.
104         * @return The number of children.
105         */
106        public int getChildCount(Object parent) {
107                List<Object> visibleAtts = _visibleAttributes(parent);
108                int numVisibleAtts = visibleAtts.size();
109
110                return numVisibleAtts + super.getChildCount(parent);
111        }
112
113        /**
114         * Return the index of the given child within the given parent. If the
115         * parent is not contained in the child, return -1.
116         * 
117         * @param parent
118         *            The parent.
119         * @param child
120         *            The child.
121         * @return The index of the specified child.
122         */
123        public int getIndexOfChild(Object parent, Object child) {
124                List<Object> visibleAtts = _visibleAttributes(parent);
125
126                int index = visibleAtts.indexOf(child);
127
128                if (index >= 0) {
129                        return index;
130                }
131
132                return -1;
133        }
134
135        /**
136         * Return true if the object is a leaf node. An object is a leaf node if it
137         * has no children that are instances of one of the classes specified by
138         * setFilter(), if a filter has been specified.
139         * 
140         * @param object
141         *            The object.
142         * @return True if the node has no children.
143         */
144        public boolean isLeaf(Object object) {
145                if (_visibleAttributes(object).size() > 0) {
146                        return false;
147                }
148
149                return super.isLeaf(object);
150        }
151
152        // /////////////////////////////////////////////////////////////////
153        // // protected methods ////
154
155        /**
156         * Return the list of Attributes that we want to be visible in the outline
157         * view, or an empty list if there are none. Override this method if you
158         * wish to show only a subset of the attributes.
159         * 
160         * @param object
161         *            The object.
162         * @return A list of attributes.
163         */
164        protected List<Object> _visibleAttributes(Object object) {
165                if (!(object instanceof NamedObj)) {
166                        return Collections.emptyList();
167                }
168                Vector<Object> visibleAtts = new Vector<Object>(1);
169
170                if (includeAttributes) { 
171                        //  Look for attributes that we want to include
172                        List<?> attributes = ((NamedObj) object).attributeList();
173                        for (Object att : attributes) {
174                                visibleAtts.add(att);
175                        }
176                }
177        
178                if (includePorts) {
179                        if (object instanceof Entity) {
180                
181                                // Look for ports we want to include
182                                List<?> ports = ((Entity) object).portList();
183                                for (Object port : ports) {
184                                        if (port instanceof Port) {
185                                                visibleAtts.add(port);
186                                        }
187                                }
188                        }
189                }
190
191                if (includeRelations) {
192                if ((object instanceof CompositeEntity)) {
193                        List<?> relations = ((CompositeEntity) object).relationList();
194                        for (Object relation : relations) {
195                                visibleAtts.add(relation);
196                        }
197                }
198                }
199
200                return visibleAtts;
201        }
202}