001/* An object that can create a tableau for a Ptolemy II model.
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.Iterator;
032
033import ptolemy.kernel.util.IllegalActionException;
034import ptolemy.kernel.util.NameDuplicationException;
035import ptolemy.kernel.util.NamedObj;
036
037///////////////////////////////////////////////////////////////////
038//// PtolemyTableauFactory
039
040/**
041 This is an intermediate container tableau factory that is designed to contain
042 all tableau factories in a configuration that are capable of displaying a
043 Ptolemy II model.  This class sets up the effigy with a set of available
044 views.  Tableaux can use that to set up a View menu which offers alternative
045 views besides the default view. Subclasses of this class will usually
046 be inner classes of a Tableau, and will create the Tableau.
047
048 @author Steve Neuendorffer and Edward A. Lee
049 @version $Id$
050 @since Ptolemy II 1.0
051 @Pt.ProposedRating Yellow (eal)
052 @Pt.AcceptedRating Red (cxh)
053 @see Configuration
054 @see Effigy
055 @see Tableau
056 */
057public class PtolemyTableauFactory extends TableauFactory {
058    /** Create a factory with the given name and container.
059     *  @param container The container.
060     *  @param name The name.
061     *  @exception IllegalActionException If the container is incompatible
062     *   with this entity.
063     *  @exception NameDuplicationException If the name coincides with
064     *   an entity already in the container.
065     */
066    public PtolemyTableauFactory(NamedObj container, String name)
067            throws IllegalActionException, NameDuplicationException {
068        super(container, name);
069    }
070
071    ///////////////////////////////////////////////////////////////////
072    ////                         public methods                    ////
073
074    /** Create a tableau for the specified effigy. The tableau will
075     *  created with a new unique name with the specified effigy as its
076     *  container.  If the effigy is not an instance of PtolemyEffigy,
077     *  then return null.  Otherwise, set up the list of alternative
078     *  views in the PtolemyEffigy and then delegate to the first
079     *  contained factory that can display the model.
080     *  @param effigy The model effigy.
081     *  @return A tableau for the effigy, or null if one cannot be created.
082     *  @exception Exception If the factory should be able to create a
083     *   Tableau for the effigy, but something goes wrong.
084     */
085    @Override
086    public Tableau createTableau(Effigy effigy) throws Exception {
087        if (!(effigy instanceof PtolemyEffigy)) {
088            return null;
089        }
090
091        // Indicate to the effigy that this factory contains effigies
092        // offering multiple views of the effigy data.
093        effigy.setTableauFactory(this);
094
095        // Delegate to the first contained effigy to open a view.
096        Tableau tableau = null;
097        Iterator<TableauFactory> factories = attributeList(TableauFactory.class)
098                .iterator();
099
100        if (factories.hasNext()) {
101            TableauFactory factory = factories.next();
102            tableau = factory.createTableau(effigy);
103            if (tableau != null) {
104                factory._configureTableau(tableau);
105            }
106        }
107
108        return tableau;
109    }
110}