001/* An ase graph view for Ptolemy models
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
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.actor.lib.hoc.Case;
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//// CaseGraphTableau
046
047/** An editor tableau for case constructs in Ptolemy II. FIXME: more
048
049 @author  Edward A. Lee
050 @version $Id$
051 @since Ptolemy II 8.0
052 @Pt.ProposedRating Yellow (eal)
053 @Pt.AcceptedRating Red (johnr)
054 */
055public class CaseGraphTableau extends Tableau {
056    /** Create a new case editor tableau with the specified container
057     *  and name.
058     *  @param container The container.
059     *  @param name The name.
060     *  @exception IllegalActionException If the model associated with
061     *   the container effigy is not an instance of Case.
062     *  @exception NameDuplicationException If the container already
063     *   contains an object with the specified name.
064     */
065    public CaseGraphTableau(PtolemyEffigy container, String name)
066            throws IllegalActionException, NameDuplicationException {
067        this(container, name, null);
068    }
069
070    /** Create a new case editor tableau with the specified container,
071     *  name, and default library.
072     *  @param container The container.
073     *  @param name The name.
074     *  @param defaultLibrary The default library, or null to not specify one.
075     *  @exception IllegalActionException If the model associated with
076     *   the container effigy is not an instance of Case.
077     *  @exception NameDuplicationException If the container already
078     *   contains an object with the specified name.
079     */
080    public CaseGraphTableau(PtolemyEffigy container, String name,
081            LibraryAttribute defaultLibrary)
082            throws IllegalActionException, NameDuplicationException {
083        super(container, name);
084
085        NamedObj model = container.getModel();
086
087        if (!(model instanceof Case)) {
088            throw new IllegalActionException(this,
089                    "Cannot edit a model that is not an Case.");
090        }
091
092        createGraphFrame((Case) model, defaultLibrary);
093    }
094
095    ///////////////////////////////////////////////////////////////////
096    ////                         public methods                    ////
097
098    /** Create the graph frame that displays the model associated with
099     *  this tableau. This method creates a CaseGraphFrame.
100     *  @param model The Ptolemy II model to display in the graph frame.
101     */
102    public void createGraphFrame(CompositeEntity model) {
103        createGraphFrame(model, null);
104    }
105
106    /** Create the graph frame that displays the model associated with
107     *  this tableau together with the specified library.
108     *  This method creates a CaseGraphFrame. If a subclass
109     *  uses another frame, this method should be overridden to create
110     *  that frame.
111     *  @param model The Ptolemy II model to display in the graph frame.
112     *  @param defaultLibrary The default library, or null to not specify
113     *   one.
114     */
115    public void createGraphFrame(CompositeEntity model,
116            LibraryAttribute defaultLibrary) {
117        if (!(model instanceof Case)) {
118            throw new InternalErrorException(this, null, "Composite Entity \""
119                    + model.getFullName() + "\" is not an instance of Case.");
120        }
121        CaseGraphFrame frame = new CaseGraphFrame((Case) model, this,
122                defaultLibrary);
123
124        try {
125            setFrame(frame);
126        } catch (IllegalActionException ex) {
127            throw new InternalErrorException(ex);
128        }
129
130        frame.setBackground(BACKGROUND_COLOR);
131        frame.pack();
132        frame.centerOnScreen();
133        frame.setVisible(true);
134    }
135
136    ///////////////////////////////////////////////////////////////////
137    ////                     public inner classes                  ////
138
139    /** A factory that creates graph editing tableaux for Ptolemy models.
140     */
141    public static class Factory extends TableauFactory {
142        /** Create an factory with the given name and container.
143         *  @param container The container.
144         *  @param name The name of the entity.
145         *  @exception IllegalActionException If the container is incompatible
146         *   with this attribute.
147         *  @exception NameDuplicationException If the name coincides with
148         *   an attribute already in the container.
149         */
150        public Factory(NamedObj container, String name)
151                throws IllegalActionException, NameDuplicationException {
152            super(container, name);
153        }
154
155        /** Create an instance of CaseGraphTableau for the specified effigy,
156         *  if it is an effigy for an instance of FSMActor.
157         *  @param effigy The effigy for an FSMActor.
158         *  @return A new CaseGraphTableau, if the effigy is a PtolemyEffigy
159         *   that references an FSMActor, or null otherwise.
160         *  @exception Exception If an exception occurs when creating the
161         *   tableau.
162         */
163        @Override
164        public Tableau createTableau(Effigy effigy) throws Exception {
165            if (!(effigy instanceof PtolemyEffigy)) {
166                return null;
167            }
168
169            NamedObj model = ((PtolemyEffigy) effigy).getModel();
170
171            if (model instanceof Case) {
172                // Check to see whether this factory contains a
173                // default library.
174                LibraryAttribute library = (LibraryAttribute) getAttribute(
175                        "_library", LibraryAttribute.class);
176
177                CaseGraphTableau tableau = new CaseGraphTableau(
178                        (PtolemyEffigy) effigy, effigy.uniqueName("tableau"),
179                        library);
180                return tableau;
181            } else {
182                return null;
183            }
184        }
185    }
186
187    ///////////////////////////////////////////////////////////////////
188    ////                         private variables                 ////
189
190    /** Background color. */
191    private static Color BACKGROUND_COLOR = new Color(0xe5e5e5);
192}