001/* A tableau factory that opens a contained component.
002
003 Copyright (c) 1997-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
028 */
029package ptolemy.actor.gui;
030
031import java.util.List;
032
033import ptolemy.data.expr.StringParameter;
034import ptolemy.kernel.CompositeEntity;
035import ptolemy.kernel.util.IllegalActionException;
036import ptolemy.kernel.util.NameDuplicationException;
037import ptolemy.kernel.util.NamedObj;
038
039///////////////////////////////////////////////////////////////////
040//// LevelSkippingTableauFactory
041
042/**
043 This class is an attribute that creates a tableau to view an object
044 contained by the model associated with the specified effigy.
045 When a model is opened, this object looks for a contained entity
046 with the name given by <i>entityName</i>, or looks for the first
047 contained entity if no name is given, and opens that entity rather
048 than the model associated with the specified effigy.
049
050 @author Edward A. Lee
051 @version $Id$
052 @since Ptolemy II 1.0
053 @Pt.ProposedRating Yellow (eal)
054 @Pt.AcceptedRating Red (eal)
055 */
056public class LevelSkippingTableauFactory extends TableauFactory {
057    /** Create a factory with the given name and container.
058     *  @param container The container.
059     *  @param name The name.
060     *  @exception IllegalActionException If the container is incompatible
061     *   with this attribute.
062     *  @exception NameDuplicationException If the name coincides with
063     *   an attribute already in the container.
064     */
065    public LevelSkippingTableauFactory(NamedObj container, String name)
066            throws IllegalActionException, NameDuplicationException {
067        super(container, name);
068
069        entityName = new StringParameter(this, "entityName");
070        entityName.setExpression("");
071    }
072
073    ///////////////////////////////////////////////////////////////////
074    ////                         parameters                        ////
075
076    /** The name of the contained entity to open, or an empty string
077     *  to just open the first one found.  This is a string that defaults
078     *  to empty.
079     */
080    public StringParameter entityName;
081
082    ///////////////////////////////////////////////////////////////////
083    ////                         public methods                    ////
084
085    /** Create a tableau for the specified effigy by identifying an
086     *  object contained by the specified effigy as given by <i>entityName</i>,
087     *  or the first entity contained by that object if no <i>entityName</i>
088     *  is given.  If the specified effigy is not an instance of PtolemyEffigy,
089     *  this simply return null. If the model associated with the effigy
090     *  does not contain the specified entity, then also return null.
091     *  @param effigy The model effigy.
092     *  @return A tableau for the effigy, or null if one cannot be created.
093     *  @exception Exception If the factory should be able to create a
094     *   Tableau for the effigy, but something goes wrong.
095     */
096    @Override
097    public Tableau createTableau(Effigy effigy) throws Exception {
098        if (!(effigy instanceof PtolemyEffigy)) {
099            return null;
100        }
101
102        NamedObj model = ((PtolemyEffigy) effigy).getModel();
103
104        if (model instanceof CompositeEntity) {
105            String name = entityName.stringValue();
106            NamedObj toOpen = null;
107
108            if (!name.trim().equals("")) {
109                toOpen = ((CompositeEntity) model).getEntity(name);
110            } else {
111                List entities = ((CompositeEntity) model).entityList();
112
113                if (entities.size() > 0) {
114                    toOpen = (NamedObj) entities.get(0);
115                }
116            }
117
118            if (toOpen != null) {
119                Configuration configuration = (Configuration) effigy.toplevel();
120                return configuration.openModel(toOpen);
121            }
122        }
123
124        return null;
125    }
126}