001/* An icon editor for Ptolemy II models.
002
003 Copyright (c) 2003-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.icon;
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.kernel.util.IllegalActionException;
037import ptolemy.kernel.util.NameDuplicationException;
038import ptolemy.kernel.util.NamedObj;
039import ptolemy.kernel.util.Workspace;
040import ptolemy.moml.LibraryAttribute;
041
042///////////////////////////////////////////////////////////////////
043//// EditIconTableau
044
045/**
046 This is an icon editor for ptolemy models.  It constructs an instance
047 of EditIconFrame, which contains an editor pane based on diva.
048
049 @see EditIconFrame
050 @author  Edward A. Lee
051 @version $Id$
052 @since Ptolemy II 4.0
053 @Pt.ProposedRating Red (neuendor)
054 @Pt.AcceptedRating Red (johnr)
055 */
056public class EditIconTableau extends Tableau {
057    /** Create a tableau in the specified workspace.
058     *  @param workspace The workspace.
059     *  @exception IllegalActionException If thrown by the parent
060     *  class or while setting an attribute.
061     *  @exception NameDuplicationException If the name coincides with
062     *   an attribute already in the container.
063     */
064    public EditIconTableau(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 parent
074     *  class or while setting an attribute.
075     *  @exception NameDuplicationException If the name coincides with
076     *   an attribute already in the container.
077     */
078    public EditIconTableau(PtolemyEffigy container, String name)
079            throws IllegalActionException, NameDuplicationException {
080        this(container, name, null);
081    }
082
083    /** Create a tableau with the specified container, name, and
084     *  default library.
085     *  @param container The container.
086     *  @param name The name.
087     *  @param defaultLibrary The default library, or null to not specify one.
088     *  @exception IllegalActionException If thrown by the parent
089     *  class or while setting an attribute.
090     *  @exception NameDuplicationException If the name coincides with
091     *   an attribute already in the container.
092     */
093    public EditIconTableau(PtolemyEffigy container, String name,
094            LibraryAttribute defaultLibrary)
095            throws IllegalActionException, NameDuplicationException {
096        super(container, name);
097
098        NamedObj model = container.getModel();
099
100        if (model == null) {
101            return;
102        }
103
104        if (model instanceof EditorIcon) {
105            EditorIcon entity = (EditorIcon) model;
106
107            EditIconFrame frame = new EditIconFrame(entity, this,
108                    defaultLibrary);
109            setFrame(frame);
110            frame.setBackground(BACKGROUND_COLOR);
111            return;
112        } else {
113            throw new IllegalActionException(this, "Cannot edit an icon "
114                    + "that is not an instance of EditorIcon.");
115        }
116    }
117
118    ///////////////////////////////////////////////////////////////////
119    ////                         public methods                    ////
120
121    /** Override the base class to return a more reasonable title.
122     *  @return The string "Icon editor for name", where name is the
123     *   name of the object whose icon this is editing.
124     */
125    @Override
126    public String getTitle() {
127        NamedObj container = getContainer();
128
129        if (container instanceof PtolemyEffigy) {
130            NamedObj model = ((PtolemyEffigy) container).getModel();
131
132            if (model != null) {
133                NamedObj modelContainer = model.getContainer();
134
135                if (modelContainer != null) {
136                    return "Icon editor for " + modelContainer.getName();
137                }
138            }
139        }
140
141        // If anything goes wrong, just defer to the base class.
142        return super.getTitle();
143    }
144
145    ///////////////////////////////////////////////////////////////////
146    ////                         private members                   ////
147    // The background color.
148    private static Color BACKGROUND_COLOR = new Color(0xe5e5e5);
149
150    ///////////////////////////////////////////////////////////////////
151    ////                     public inner classes                  ////
152
153    /** A factory that creates icon editing tableaux for Ptolemy models.
154     */
155    public static class Factory extends TableauFactory {
156        /** Create an factory with the given name and container.
157         *  @param container The container.
158         *  @param name The name.
159         *  @exception IllegalActionException If the container is incompatible
160         *   with this attribute.
161         *  @exception NameDuplicationException If the name coincides with
162         *   an attribute already in the container.
163         */
164        public Factory(NamedObj container, String name)
165                throws IllegalActionException, NameDuplicationException {
166            super(container, name);
167        }
168
169        /** Create a tableau in the default workspace with no name for the
170         *  given Effigy.  The tableau will created with a new unique name
171         *  in the given model effigy.  If this factory cannot create a tableau
172         *  for the given effigy (perhaps because the effigy is not of the
173         *  appropriate subclass) then return null.
174         *  It is the responsibility of callers of this method to check the
175         *  return value and call show().
176         *
177         *  @param effigy The model effigy.
178         *  @return A new EditIconTableau, if the effigy is a
179         *  PtolemyEffigy, or null otherwise.
180         *  @exception Exception If an exception occurs when creating the
181         *  tableau.
182         */
183        @Override
184        public Tableau createTableau(Effigy effigy) throws Exception {
185            if (effigy instanceof PtolemyEffigy) {
186                // First see whether the effigy already contains a graphTableau.
187                EditIconTableau tableau = (EditIconTableau) effigy
188                        .getEntity("editIconTableau");
189
190                if (tableau == null) {
191                    // Check to see whether this factory contains a
192                    // default library.
193                    LibraryAttribute library = (LibraryAttribute) getAttribute(
194                            "_library", LibraryAttribute.class);
195                    tableau = new EditIconTableau((PtolemyEffigy) effigy,
196                            "editIconTableau", library);
197                }
198
199                // Don't call show() here, it is called for us in
200                // TableauFrame.ViewMenuListener.actionPerformed()
201                return tableau;
202            } else {
203                return null;
204            }
205        }
206    }
207}