001/* A simple Tableau that does not do much.
002
003 Copyright (c) 2006-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.test;
028
029import java.io.File;
030import java.io.IOException;
031
032import ptolemy.actor.gui.Effigy;
033import ptolemy.actor.gui.PtolemyEffigy;
034import ptolemy.actor.gui.Tableau;
035import ptolemy.actor.gui.TableauFactory;
036import ptolemy.kernel.util.IllegalActionException;
037import ptolemy.kernel.util.KernelRuntimeException;
038import ptolemy.kernel.util.NameDuplicationException;
039import ptolemy.kernel.util.NamedObj;
040
041//////////////////////////////////////////////////////////////////////////
042//// SimpleTableau
043
044/**
045 A simple tableau.
046
047
048 @author Steve Neuendorffer and Edward A. Lee
049 @version $Id$
050 @since Ptolemy II 5.2
051 @Pt.ProposedRating Red (neuendor)
052 @Pt.AcceptedRating Red (neuendor)
053 */
054public class SimpleTableau extends Tableau {
055    /** Create a new run control panel for the model with the given
056     *  effigy.  The tableau is itself an entity contained by the effigy
057     *  and having the specified name.  The frame is not made visible
058     *  automatically.  You must call show() to make it visible.
059     *  @param container The containing effigy.
060     *  @param name The name of this tableau within the specified effigy.
061     *  @exception IllegalActionException If the tableau is not acceptable
062     *   to the specified container.
063     *  @exception NameDuplicationException If the container already contains
064     *   an entity with the specified name.
065     */
066    public SimpleTableau(PtolemyEffigy container, String name)
067            throws IllegalActionException, NameDuplicationException {
068        super(container, name);
069    }
070
071    /** Close this tableau.
072     *  @return Always return true.
073     */
074    @Override
075    public boolean close() {
076        Effigy effigy = (Effigy) getContainer();
077        System.out.println("SimpleTableau.close(): effigy: " + effigy);
078        if (effigy.isModified()) {
079            File file = effigy.getWritableFile();
080            System.out.println("Writing " + file);
081            try {
082                effigy.writeFile(file);
083                effigy.setModified(false);
084            } catch (IOException ex) {
085                throw new KernelRuntimeException(effigy,
086                        "Failed to write " + file);
087            }
088        }
089        return true;
090    }
091
092    /** A factory that creates run control panel tableaux for Ptolemy models.
093     */
094    public static class Factory extends TableauFactory {
095        /** Create a factory with the given name and container.
096         *  @param container The container.
097         *  @param name The name.
098         *  @exception IllegalActionException If the container is incompatible
099         *   with this attribute.
100         *  @exception NameDuplicationException If the name coincides with
101         *   an attribute already in the container.
102         */
103        public Factory(NamedObj container, String name)
104                throws IllegalActionException, NameDuplicationException {
105            super(container, name);
106        }
107
108        ///////////////////////////////////////////////////////////////////
109        ////                         public methods                    ////
110
111        /** If the specified effigy already contains a tableau named
112         *  "simpleTableau", then return that tableau; otherwise, create
113         *  a new instance of SimpleTableau for the effigy, and
114         *  name it "simpleTableau".  If the specified effigy is not an
115         *  instance of PtolemyEffigy, then do not create a tableau
116         *  and return null. It is the responsibility of callers of
117         *  this method to check the return value and call show().
118         *
119         *  @param effigy The model effigy.
120         *  @return A new run tableau if the effigy is a PtolemyEffigy,
121         *    or null otherwise.
122         *  @exception Exception If the factory should be able to create a
123         *   tableau for the effigy, but something goes wrong.
124         */
125        @Override
126        public Tableau createTableau(Effigy effigy) throws Exception {
127            if (effigy instanceof PtolemyEffigy) {
128                // First see whether the effigy already contains a SimpleTableau.
129                SimpleTableau tableau = (SimpleTableau) effigy
130                        .getEntity("simpleTableau");
131
132                if (tableau == null) {
133                    tableau = new SimpleTableau((PtolemyEffigy) effigy,
134                            "simpleTableau");
135                }
136
137                // Don't call show() here, it is called for us in
138                // TableauFrame.ViewMenuListener.actionPerformed()
139                return tableau;
140            } else {
141                return null;
142            }
143        }
144    }
145
146    /** A factory that creates run control panel tableaux for the model
147     *  associated with a top-level effigy (one that has a file
148     *  representation).
149     */
150    public static class TopFactory extends Factory {
151        /** Create a factory with the given name and container.
152         *  @param container The container.
153         *  @param name The name.
154         *  @exception IllegalActionException If the container is incompatible
155         *   with this attribute.
156         *  @exception NameDuplicationException If the name coincides with
157         *   an attribute already in the container.
158         */
159        public TopFactory(NamedObj container, String name)
160                throws IllegalActionException, NameDuplicationException {
161            super(container, name);
162        }
163
164        ///////////////////////////////////////////////////////////////////
165        ////                         public methods                    ////
166
167        /** Create a tableau to run the model associated with the specified
168         *  effigy.  The top-level effigy, as returned by
169         *  {@link Effigy#masterEffigy()}, is the one that is run.
170         *  If that effigy already contains a tableau named
171         *  "simpleTableau", then return that tableau; otherwise, create
172         *  a new instance of SimpleTableau for the top effigy, and
173         *  name it "simpleTableau".  If the specified effigy is not an
174         *  instance of PtolemyEffigy, then do not create a tableau
175         *  and return null. It is the responsibility of callers of
176         *  this method to check the return value and call show().
177         *
178         *  @param effigy The model effigy.
179         *  @return A new run tableau if the effigy is a PtolemyEffigy,
180         *    or null otherwise.
181         *  @exception Exception If the factory should be able to create a
182         *   tableau for the effigy, but something goes wrong.
183         */
184        @Override
185        public Tableau createTableau(Effigy effigy) throws Exception {
186            return super.createTableau(effigy.masterEffigy());
187        }
188    }
189}