001/* The node controller for actor instances.
002
003 Copyright (c) 2003-2016 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.event.ActionEvent;
031
032import diva.graph.GraphController;
033import ptolemy.kernel.util.Instantiable;
034import ptolemy.kernel.util.NamedObj;
035import ptolemy.moml.MoMLChangeRequest;
036import ptolemy.vergil.toolbox.FigureAction;
037import ptolemy.vergil.toolbox.MenuActionFactory;
038
039///////////////////////////////////////////////////////////////////
040//// ActorInstanceController
041
042/**
043 This class provides interaction with nodes that represent Ptolemy II
044 actor instances (i.e., not classes).  This extends the base class by
045 providing a mechanism in the context menu for converting the instance
046 into a class.
047 <p>
048 NOTE: There should be only one instance of this class associated with
049 a given GraphController. This is because this controller listens for
050 changes to the graph and re-renders the ports of any actor instance
051 in the graph when the graph changes. If there is more than one instance,
052 this rendering will be done twice, which can result in bugs like port
053 labels appearing twice.
054
055 @author Edward A. Lee and Steve Neuendorffer
056 @version $Id$
057 @since Ptolemy II 4.0
058 @Pt.ProposedRating Red (eal)
059 @Pt.AcceptedRating Red (johnr)
060 */
061public class ActorInstanceController extends ActorController {
062    /** Create an actor instance controller associated with the specified graph
063     *  controller with full access.
064     *  @param controller The associated graph controller.
065     */
066    public ActorInstanceController(GraphController controller) {
067        this(controller, FULL);
068    }
069
070    /** Create an entity controller associated with the specified graph
071     *  controller with the specified access.
072     *  @param controller The associated graph controller.
073     *  @param access The access.
074     */
075    public ActorInstanceController(GraphController controller, Access access) {
076        super(controller, access);
077
078        if (access == FULL) {
079            // The following do not require a configuration.
080            _menuFactory.addMenuItemFactory(
081                    new MenuActionFactory(_convertToClassAction));
082        }
083    }
084
085    ///////////////////////////////////////////////////////////////////
086    ////                         protected variables               ////
087
088    /** The action that handles creating an instance from a class.
089     */
090    protected ConvertToClassAction _convertToClassAction = new ConvertToClassAction(
091            "Convert to Class");
092
093    ///////////////////////////////////////////////////////////////////
094    ////                         inner classes                     ////
095    ///////////////////////////////////////////////////////////////////
096    //// ConvertToClassAction
097    // An action to convert an instance to a class.
098    @SuppressWarnings("serial")
099    private class ConvertToClassAction extends FigureAction {
100        public ConvertToClassAction(String commandName) {
101            super(commandName);
102        }
103
104        @Override
105        public void actionPerformed(ActionEvent e) {
106            // If access is not full, do nothing.
107            if (_access != FULL) {
108                return;
109            }
110
111            // Determine which entity was selected for the create instance action.
112            super.actionPerformed(e);
113
114            NamedObj object = getTarget();
115            NamedObj container = object.getContainer();
116
117            // Assumes MoML parser will convert to class.
118            // NOTE: This cast should be safe because this controller is
119            // used for actors.
120            if (((Instantiable) object).isClassDefinition()) {
121                // Object is already a class. Do nothing.
122                return;
123            }
124
125            String moml = "<class name=\"" + object.getName() + "\"/>";
126            MoMLChangeRequest request = new MoMLChangeRequest(this, container,
127                    moml);
128            container.requestChange(request);
129        }
130    }
131}