001/* A tableau that creates a run control panel for a ptolemy model.
002
003 Copyright (c) 1998-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 */
027package ptolemy.actor.gui.run;
028
029import java.util.List;
030
031import javax.swing.JFrame;
032
033import ptolemy.actor.CompositeActor;
034import ptolemy.actor.Manager;
035import ptolemy.actor.gui.Effigy;
036import ptolemy.actor.gui.PtolemyEffigy;
037import ptolemy.actor.gui.Tableau;
038import ptolemy.actor.gui.TableauFactory;
039import ptolemy.kernel.util.IllegalActionException;
040import ptolemy.kernel.util.NameDuplicationException;
041import ptolemy.kernel.util.NamedObj;
042
043///////////////////////////////////////////////////////////////////
044//// InterfaceTableau
045
046/**
047 A tableau that creates a new run control panel for a ptolemy model.
048 This panel has controls for parameters of the top-level entity
049 and its director, if any, a set of buttons to control execution
050 of the model, and a panel displaying the placeable entities within
051 the model.
052
053 FIXME: Customization
054
055 @author Edward A. Lee
056 @version $Id$
057 @since Ptolemy II 8.0
058 @Pt.ProposedRating Red (neuendor)
059 @Pt.AcceptedRating Red (neuendor)
060 */
061public class InterfaceTableau extends Tableau {
062    /** Create a new run control panel for the model with the given
063     *  effigy.  The tableau is itself an entity contained by the effigy
064     *  and having the specified name.  The frame is not made visible
065     *  automatically.  You must call show() to make it visible.
066     *  @param container The containing effigy.
067     *  @param name The name of this tableau within the specified effigy.
068     *  @exception IllegalActionException If the tableau is not acceptable
069     *   to the specified container.
070     *  @exception NameDuplicationException If the container already contains
071     *   an entity with the specified name.
072     */
073    public InterfaceTableau(PtolemyEffigy container, String name)
074            throws IllegalActionException, NameDuplicationException {
075        super(container, name);
076
077        NamedObj model = container.getModel();
078
079        if (!(model instanceof CompositeActor)) {
080            throw new IllegalActionException(this,
081                    "Cannot run a model that is not a CompositeActor."
082                            + " It is: " + model);
083        }
084
085        _manager = ((CompositeActor) model).getManager();
086        // Create a manager if necessary.
087        if (_manager == null) {
088            try {
089                _manager = new Manager(model.workspace(), "manager");
090                ((CompositeActor) model).setManager(_manager);
091            } catch (IllegalActionException ex) {
092                throw new IllegalActionException(this, ex,
093                        "Failed to set manager.  This can occur if "
094                                + "you try to run a non-toplevel model that "
095                                + "is a component of a toplevel model.  "
096                                + "The solution is invoke View -> Run while in a "
097                                + "toplevel window.");
098            }
099        }
100
101        JFrame frame = new CustomizableRunFrame((CompositeActor) model, this);
102        setFrame(frame);
103    }
104
105    ///////////////////////////////////////////////////////////////////
106    ////                         private variables                 ////
107
108    /** The manager. */
109    private Manager _manager;
110
111    ///////////////////////////////////////////////////////////////////
112    ////                         inner classes                     ////
113
114    /** A factory that creates run control panel tableaux for Ptolemy models.
115     */
116    public static class Factory extends TableauFactory {
117        /** Create a factory with the given name and container.
118         *  @param container The container.
119         *  @param name The name.
120         *  @exception IllegalActionException If the container is incompatible
121         *   with this attribute.
122         *  @exception NameDuplicationException If the name coincides with
123         *   an attribute already in the container.
124         */
125        public Factory(NamedObj container, String name)
126                throws IllegalActionException, NameDuplicationException {
127            super(container, name);
128        }
129
130        ///////////////////////////////////////////////////////////////////
131        ////                         public methods                    ////
132
133        /** If the specified effigy already contains a tableau named
134         *  "runTableau", then return that tableau; otherwise, create
135         *  a new instance of RunTableau for the effigy, and
136         *  name it "runTableau".  If the specified effigy is not an
137         *  instance of PtolemyEffigy, then do not create a tableau
138         *  and return null. It is the responsibility of callers of
139         *  this method to check the return value and call show().
140         *
141         *  @param effigy The model effigy.
142         *  @return A new run tableau if the effigy is a PtolemyEffigy,
143         *    or null otherwise.
144         *  @exception Exception If the factory should be able to create a
145         *   tableau for the effigy, but something goes wrong.
146         */
147        @Override
148        public Tableau createTableau(Effigy effigy) throws Exception {
149            if (effigy instanceof PtolemyEffigy) {
150                // First see whether the effigy already contains an InterfaceTableau.
151                List<InterfaceTableau> list = effigy
152                        .entityList(InterfaceTableau.class);
153                InterfaceTableau tableau;
154                if (list.size() > 0) {
155                    // Return the last one (most recently created) in the list.
156                    tableau = list.get(list.size() - 1);
157                } else {
158                    tableau = new InterfaceTableau((PtolemyEffigy) effigy,
159                            effigy.uniqueName("interfaceTableau"));
160                }
161                // Don't call show() here, it is called for us in
162                // TableauFrame.ViewMenuListener.actionPerformed()
163                return tableau;
164            } else {
165                return null;
166            }
167        }
168    }
169}