001/* The action to listen to debug messages of a NamedObj.
002
003 Copyright (c) 2009-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.kernel;
029
030import java.awt.event.ActionEvent;
031
032import ptolemy.actor.ActorExecutionAspect;
033import ptolemy.actor.gui.Configuration;
034import ptolemy.actor.gui.DebugListenerTableau;
035import ptolemy.actor.gui.Effigy;
036import ptolemy.actor.gui.ExecutionAspectPlotterEditorFactory;
037import ptolemy.actor.gui.Tableau;
038import ptolemy.actor.gui.TextEffigy;
039import ptolemy.kernel.util.KernelException;
040import ptolemy.kernel.util.NamedObj;
041import ptolemy.util.MessageHandler;
042import ptolemy.vergil.basic.BasicGraphController;
043import ptolemy.vergil.basic.BasicGraphFrame;
044import ptolemy.vergil.toolbox.FigureAction;
045
046///////////////////////////////////////////////////////////////////
047//// ListenToAction
048
049/** An action to listen to debug messages in the NamedObj.
050 *  This is static so that other classes can use it.
051 *
052 *  @author Man-Kit Leung
053 *  @version $Id$
054 *  @since Ptolemy II 8.0
055 *  @Pt.ProposedRating Red (mankit)
056 *  @Pt.AcceptedRating Red (mankit)
057 */
058@SuppressWarnings("serial")
059public class ListenToAction extends FigureAction {
060    /** Construct an action that listens to NamedObj messages.
061     *  @param controller The corresponding controller.
062     *  @param componentType A String that names the listened to component.
063     */
064    public ListenToAction(BasicGraphController controller,
065            String componentType) {
066        super("Listen to " + componentType);
067        _controller = controller;
068    }
069
070    /** Construct an action that listens to NamedObj messages.
071     *  @param target The target
072     *  @param controller The corresponding controller.
073     */
074    public ListenToAction(NamedObj target, BasicGraphController controller) {
075        super("Listen to " + target.getName());
076        _target = target;
077        _controller = controller;
078    }
079
080    /** Open a TextEffigy that displays debug messages.
081     *  @param event The action event, used to determine which entity
082     *  was selected for the listen to NamedObj action
083     */
084    @Override
085    public void actionPerformed(ActionEvent event) {
086        if (_configuration == null) {
087            MessageHandler.error(
088                    "Cannot listen to component without a configuration.");
089            return;
090        }
091
092        // Determine which entity was selected for the listen to
093        // NamedObj action.
094        super.actionPerformed(event);
095
096        NamedObj object = _target;
097
098        if (object == null) {
099            object = getTarget();
100        }
101
102        try {
103            BasicGraphFrame frame = _controller.getFrame();
104            Tableau tableau = frame.getTableau();
105
106            // effigy is the whole model.
107            Effigy effigy = (Effigy) tableau.getContainer();
108
109            // We want to open a new window that behaves as a
110            // child of the model window.  So, we create a new text
111            // effigy inside this one.  Specify model's effigy as
112            // a container for this new effigy.
113            Effigy textEffigy = new TextEffigy(effigy,
114                    effigy.uniqueName("debugListener" + object.getName()));
115
116            DebugListenerTableau debugTableau = new DebugListenerTableau(
117                    textEffigy,
118                    textEffigy.uniqueName("debugListener" + object.getName()));
119            debugTableau.setDebuggable(object);
120
121            // If the actor is an ExecutionAspect, open Plot as well.
122            if (object instanceof ActorExecutionAspect) {
123                //Effigy plotEffigy = new
124                ExecutionAspectPlotterEditorFactory factory = new ExecutionAspectPlotterEditorFactory(
125                        object, object.uniqueName("_editorFactory"));
126
127                ((ActorExecutionAspect) object).addExecutingListener(factory);
128                factory.createEditor(object, this.getFrame());
129            }
130        } catch (KernelException ex) {
131            MessageHandler.error("Failed to create debug listener.", ex);
132        }
133    }
134
135    /** Set the configuration for use by the help screen.
136     *  @param configuration The configuration.
137     */
138    public void setConfiguration(Configuration configuration) {
139        _configuration = configuration;
140    }
141
142    private Configuration _configuration;
143
144    private BasicGraphController _controller;
145
146    private NamedObj _target;
147}