001/* An FSM graph view for Ptolemy models.
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
027 */
028package ptolemy.vergil.modal;
029
030import java.awt.Color;
031
032import ptolemy.actor.gui.Effigy;
033import ptolemy.actor.gui.PtolemyEffigy;
034import ptolemy.actor.gui.Tableau;
035import ptolemy.actor.gui.TableauFactory;
036import ptolemy.domains.modal.kernel.FSMActor;
037import ptolemy.kernel.CompositeEntity;
038import ptolemy.kernel.util.IllegalActionException;
039import ptolemy.kernel.util.InternalErrorException;
040import ptolemy.kernel.util.NameDuplicationException;
041import ptolemy.kernel.util.NamedObj;
042import ptolemy.moml.LibraryAttribute;
043
044///////////////////////////////////////////////////////////////////
045//// FSMGraphTableau
046
047/** An editor tableau for finite state machines.
048
049 @author  Steve Neuendorffer and Edward A. Lee
050 @version $Id$
051 @since Ptolemy II 8.0
052 @Pt.ProposedRating Red (neuendor)
053 @Pt.AcceptedRating Red (johnr)
054 */
055public class FSMGraphTableau extends Tableau {
056
057    /** Create a new FSM editor tableau with the specified container
058     *  and name, with no default library.
059     *  @param container The container.
060     *  @param name The name.
061     *  @exception IllegalActionException If the model associated with
062     *   the container effigy is not an FSMActor.
063     *  @exception NameDuplicationException If the container already
064     *   contains an object with the specified name.
065     */
066    public FSMGraphTableau(PtolemyEffigy container, String name)
067            throws IllegalActionException, NameDuplicationException {
068        this(container, name, null);
069    }
070
071    /** Create a new FSM editor tableau with the specified container,
072     *  name, and default library.
073     *  @param container The container.
074     *  @param name The name.
075     *  @param defaultLibrary The default library, or null to not specify one.
076     *  @exception IllegalActionException If the model associated with
077     *   the container effigy is not an FSMActor.
078     *  @exception NameDuplicationException If the container already
079     *   contains an object with the specified name.
080     */
081    public FSMGraphTableau(PtolemyEffigy container, String name,
082            LibraryAttribute defaultLibrary)
083            throws IllegalActionException, NameDuplicationException {
084        super(container, name);
085
086        NamedObj model = container.getModel();
087
088        if (!(model instanceof FSMActor)) {
089            throw new IllegalActionException(this,
090                    "Cannot edit a model that is not an FSMActor.");
091        }
092
093        createGraphFrame((FSMActor) model, defaultLibrary);
094    }
095
096    ///////////////////////////////////////////////////////////////////
097    ////                         public methods                    ////
098
099    /** Create the graph frame that displays the model associated with
100     *  this tableau. This method creates a FSMGraphFrame. If subclass
101     *  uses another frame, this method should be overridden to create
102     *  that frame.
103     *  @param model The Ptolemy II model to display in the graph frame.
104     */
105    public void createGraphFrame(CompositeEntity model) {
106        createGraphFrame(model, null);
107    }
108
109    /** Create the graph frame that displays the model associated with
110     *  this tableau together with the specified library.
111     *  This method creates a FSMGraphFrame. If a subclass
112     *  uses another frame, this method should be overridden to create
113     *  that frame.
114     *  @param model The Ptolemy II model to display in the graph frame.
115     *  @param defaultLibrary The default library, or null to not specify
116     *   one.
117     */
118    public void createGraphFrame(CompositeEntity model,
119            LibraryAttribute defaultLibrary) {
120        FSMGraphFrame frame = new FSMGraphFrame(model, this, defaultLibrary);
121
122        try {
123            setFrame(frame);
124        } catch (IllegalActionException ex) {
125            throw new InternalErrorException(ex);
126        }
127
128        frame.setBackground(BACKGROUND_COLOR);
129        frame.pack();
130        frame.centerOnScreen();
131        frame.setVisible(true);
132    }
133
134    ///////////////////////////////////////////////////////////////////
135    ////                           public fields                   ////
136
137    /** The default background color.  To change the background color,
138     *  use a {@link ptolemy.actor.gui.PtolemyPreferences}.
139     */
140    public static final Color BACKGROUND_COLOR = new Color(0xe5e5e5);
141
142    ///////////////////////////////////////////////////////////////////
143    ////                     public inner classes                  ////
144
145    /** A factory that creates graph editing tableaux for Ptolemy models.
146     */
147    public static class Factory extends TableauFactory {
148        /** Create an factory with the given name and container.
149         *  @param container The container.
150         *  @param name The name of the entity.
151         *  @exception IllegalActionException If the container is incompatible
152         *   with this attribute.
153         *  @exception NameDuplicationException If the name coincides with
154         *   an attribute already in the container.
155         */
156        public Factory(NamedObj container, String name)
157                throws IllegalActionException, NameDuplicationException {
158            super(container, name);
159        }
160
161        /** Create an instance of FSMGraphTableau for the specified effigy,
162         *  if it is an effigy for an instance of FSMActor.
163         *  @param effigy The effigy for an FSMActor.
164         *  @return A new FSMGraphTableau, if the effigy is a PtolemyEffigy
165         *   that references an FSMActor, or null otherwise.
166         *  @exception Exception If an exception occurs when creating the
167         *   tableau.
168         */
169        @Override
170        public Tableau createTableau(Effigy effigy) throws Exception {
171            if (!(effigy instanceof PtolemyEffigy)) {
172                return null;
173            }
174
175            Tableau tableau = (Tableau) effigy.getEntity("fsmGraphTableau");
176            if (tableau != null) {
177                return tableau;
178            }
179
180            NamedObj model = ((PtolemyEffigy) effigy).getModel();
181
182            // If we save a plot, then the model of the effigy will be null.
183            if (model instanceof FSMActor) {
184                // Check to see whether this factory contains a
185                // default library.
186                LibraryAttribute library = (LibraryAttribute) getAttribute(
187                        "_library", LibraryAttribute.class);
188
189                tableau = new FSMGraphTableau((PtolemyEffigy) effigy,
190                        "fsmGraphTableau", library);
191            } else {
192                return null;
193            }
194            return tableau;
195        }
196    }
197}