001/* An attribute that creates an editor pane to configure an entity
002 contained by its container.
003
004 Copyright (c) 2003-2014 The Regents of the University of California.
005 All rights reserved.
006 Permission is hereby granted, without written agreement and without
007 license or royalty fees, to use, copy, modify, and distribute this
008 software and its documentation for any purpose, provided that the above
009 copyright notice and the following two paragraphs appear in all copies
010 of this software.
011
012 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
013 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
014 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
015 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
016 SUCH DAMAGE.
017
018 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
021 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
022 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
023 ENHANCEMENTS, OR MODIFICATIONS.
024
025 PT_COPYRIGHT_VERSION_2
026 COPYRIGHTENDKEY
027
028 */
029package ptolemy.actor.gui;
030
031import java.awt.Component;
032import java.util.Iterator;
033
034import ptolemy.kernel.CompositeEntity;
035import ptolemy.kernel.util.IllegalActionException;
036import ptolemy.kernel.util.NameDuplicationException;
037import ptolemy.kernel.util.NamedObj;
038
039///////////////////////////////////////////////////////////////////
040//// InsideEntityEditorPaneFactory
041
042/**
043 This is an attribute that can create a pane (called a "configuration
044 widget") for interactively configuring an entity contained by its container.
045 That is, it adds a level of indirection, making it appear as if you
046 were configuring the container, when in fact you are configuring an
047 entity contained by the container.  If the container is not an instance
048 of CompositeEntity, or it does not contain any entities, then this behaves
049 just like the base class.  If the container contains more than one entity,
050 then only the first one encountered is configured.  To use this,
051 place an instance of this class (or a derived class) inside a Ptolemy II
052 object.  When the user double clicks on the icon for that object,
053 or selects Configure from the context menu, then a dialog is opened
054 containing the pane returned by createEditorPane().
055
056 @author Edward A. Lee
057 @version $Id$
058 @since Ptolemy II 4.0
059 @Pt.ProposedRating Yellow (eal)
060 @Pt.AcceptedRating Red (johnr)
061 */
062public class InsideEntityEditorPaneFactory extends EditorPaneFactory {
063    /** Construct a factory with the specified container and name.
064     *  @param container The container.
065     *  @param name The name of the factory.
066     *  @exception IllegalActionException If the factory is not of an
067     *   acceptable attribute for the container.
068     *  @exception NameDuplicationException If the name coincides with
069     *   an attribute already in the container.
070     */
071    public InsideEntityEditorPaneFactory(NamedObj container, String name)
072            throws IllegalActionException, NameDuplicationException {
073        super(container, name);
074    }
075
076    ///////////////////////////////////////////////////////////////////
077    ////                         public methods                    ////
078
079    /** Override the base class to look for an entity contained by
080     *  the container and return a configurer for that.
081     *  @return A new widget for configuring the container.
082     */
083    @Override
084    public Component createEditorPane() {
085        NamedObj object = getContainer();
086
087        if (object instanceof CompositeEntity) {
088            Iterator entities = ((CompositeEntity) object).entityList()
089                    .iterator();
090
091            if (entities.hasNext()) {
092                object = (NamedObj) entities.next();
093            }
094        }
095
096        return super.createEditorPane(object);
097    }
098}