001/* A graph editor for Ptolemy II 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.actor;
029
030import java.awt.Color;
031
032import ptolemy.actor.gui.Effigy;
033import ptolemy.actor.gui.PlotEffigy;
034import ptolemy.actor.gui.PtolemyEffigy;
035import ptolemy.actor.gui.Tableau;
036import ptolemy.actor.gui.TableauFactory;
037import ptolemy.kernel.CompositeEntity;
038import ptolemy.kernel.util.IllegalActionException;
039import ptolemy.kernel.util.NameDuplicationException;
040import ptolemy.kernel.util.NamedObj;
041import ptolemy.kernel.util.Workspace;
042import ptolemy.moml.LibraryAttribute;
043
044///////////////////////////////////////////////////////////////////
045//// ActorGraphTableau
046
047/**
048 This is a graph editor for ptolemy models.  It constructs an instance
049 of ActorGraphFrame, which contains an editor pane based on diva.
050
051 @see ActorGraphFrame
052 @author  Steve Neuendorffer and Edward A. Lee
053 @version $Id$
054 @since Ptolemy II 2.0
055 @Pt.ProposedRating Red (neuendor)
056 @Pt.AcceptedRating Red (johnr)
057 */
058public class ActorGraphTableau extends Tableau {
059    /** Create a tableau in the specified workspace.
060     *  @param workspace The workspace.
061     *  @exception IllegalActionException If thrown by the superclass.
062     *  @exception NameDuplicationException If thrown by the superclass.
063     */
064    public ActorGraphTableau(Workspace workspace)
065            throws IllegalActionException, NameDuplicationException {
066        super(workspace);
067    }
068
069    /** Create a tableau with the specified container and name, with
070     *  no specified default library.
071     *  @param container The container.
072     *  @param name The name.
073     *  @exception IllegalActionException If thrown by the superclass.
074     *  @exception NameDuplicationException If thrown by the superclass.
075     */
076    public ActorGraphTableau(PtolemyEffigy container, String name)
077            throws IllegalActionException, NameDuplicationException {
078        this(container, name, null);
079    }
080
081    /** Create a tableau with the specified container, name, and
082     *  default library.
083     *  @param container The container.
084     *  @param name The name.
085     *  @param defaultLibrary The default library, or null to not specify one.
086     *  @exception IllegalActionException If the model is not a CompositeEntity.
087     *  @exception NameDuplicationException If thrown by the superclass.
088     */
089    public ActorGraphTableau(PtolemyEffigy container, String name,
090            LibraryAttribute defaultLibrary)
091            throws IllegalActionException, NameDuplicationException {
092        super(container, name);
093
094        NamedObj model = container.getModel();
095
096        if (model == null) {
097            return;
098        }
099
100        if (!(model instanceof CompositeEntity)) {
101            throw new IllegalActionException(this,
102                    "Cannot graphically edit a model "
103                            + "that is not a CompositeEntity. Model is a "
104                            + model + ", which is a "
105                            + model.getClass().getName());
106        }
107
108        CompositeEntity entity = (CompositeEntity) model;
109
110        ActorGraphFrame frame = new ActorGraphFrame(entity, this,
111                defaultLibrary);
112        setFrame(frame);
113        frame.setBackground(BACKGROUND_COLOR);
114    }
115
116    /** Invoke the close() method of the superclass and optionally
117     *  print a debugging message.
118     *  If {@link ptolemy.actor.gui.Tableau#_debugClosing} is
119     *  true, then a message is printed to standard out.
120     *  This method is used for debugging memory leaks.
121     *  @return True if the close completes, and false otherwise.
122     *  False is returned if the user cancels on a save query in the
123     *  super class.
124     */
125    @Override
126    public boolean close() {
127        if (_debugClosing) {
128            System.out.println(
129                    "ActorGraphTableau.close() : " + getFrame().getName());
130        }
131
132        return super.close();
133    }
134
135    ///////////////////////////////////////////////////////////////////
136    ////                         private members                   ////
137    // The background color.
138    private static Color BACKGROUND_COLOR = new Color(0xe5e5e5);
139
140    ///////////////////////////////////////////////////////////////////
141    ////                     public inner classes                  ////
142
143    /** A factory that creates graph editing tableaux for Ptolemy models.
144     */
145    public static class Factory extends TableauFactory {
146        /** Create an factory with the given name and container.
147         *  @param container The container.
148         *  @param name The name.
149         *  @exception IllegalActionException If the container is incompatible
150         *   with this attribute.
151         *  @exception NameDuplicationException If the name coincides with
152         *   an attribute already in the container.
153         */
154        public Factory(NamedObj container, String name)
155                throws IllegalActionException, NameDuplicationException {
156            super(container, name);
157        }
158
159        /** Create a tableau in the default workspace with no name for the
160         *  given Effigy.  The tableau will created with a new unique name
161         *  in the given model effigy.  If this factory cannot create a tableau
162         *  for the given effigy (perhaps because the effigy is not of the
163         *  appropriate subclass) then return null.
164         *  It is the responsibility of callers of this method to check the
165         *  return value and call show().
166         *
167         *  @param effigy The model effigy.
168         *  @return A new ActorGraphTableau, if the effigy is a
169         *  PtolemyEffigy, or null otherwise.
170         *  @exception Exception If an exception occurs when creating the
171         *  tableau.
172         */
173        @Override
174        public Tableau createTableau(Effigy effigy) throws Exception {
175            // Need to check for PlotEffigy here, or
176            // $PTII/bin/vergil $PTII/ptolemy/plot/demo/sinusoids.xml will not open the
177            // plot.
178            if (effigy instanceof PtolemyEffigy
179                    && !(effigy instanceof PlotEffigy)) {
180                // First see whether the effigy already contains a graphTableau.
181                ActorGraphTableau tableau = (ActorGraphTableau) effigy
182                        .getEntity("graphTableau");
183                if (tableau == null) {
184                    // Check to see whether this factory contains a
185                    // default library.
186                    LibraryAttribute library = (LibraryAttribute) getAttribute(
187                            "_library", LibraryAttribute.class);
188                    tableau = new ActorGraphTableau((PtolemyEffigy) effigy,
189                            "graphTableau", library);
190                }
191
192                // Don't call show() here, it is called for us in
193                // TableauFrame.ViewMenuListener.actionPerformed()
194                return tableau;
195            } else {
196                return null;
197            }
198        }
199    }
200}