001/* Monitor input values and include the model time of the director.
002
003 Copyright (c) 2015-2018 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.actor.lib.gui;
029
030import ptolemy.actor.Director;
031import ptolemy.data.StringToken;
032import ptolemy.data.Token;
033import ptolemy.kernel.CompositeEntity;
034import ptolemy.kernel.util.IllegalActionException;
035import ptolemy.kernel.util.NameDuplicationException;
036
037///////////////////////////////////////////////////////////////////
038//// TimedMonitorValue
039
040/**
041 Monitor inputs by setting the <i>value</i> parameter equal
042 to each arriving token.  This actor can be used with
043 an icon that displays the value of a parameter to get
044 on-screen display of values in a diagram. The value is
045 updated only in postfire.
046
047 <p>Note that the icon for this actor is defined in
048 <code>ptolemy/actor/lib/timedsinks.xml</code>, which looks something
049 like
050 <pre>
051&lt;entity name="TimedMonitorValue" class="ptolemy.actor.lib.TimedMonitorValue"&gt;
052&lt;doc&gt;Monitor and display values&lt;/doc&gt;
053   &lt;property name="displayWidth" class="ptolemy.data.expr.Parameter" value="20"/&gt;
054   &lt;property name="_icon" class="ptolemy.vergil.icon.UpdatedValueIcon"&gt;
055      &lt;property name="attributeName" value="value"/&gt;
056      &lt;property name="displayWidth" value="displayWidth"/&gt;
057   &lt;/property&gt;
058&lt;/entity&gt;
059 </pre>
060 @author Christopher Brooks
061 @version $Id$
062 @since Ptolemy II 11.0
063 @Pt.ProposedRating Yellow (cxh)
064 @Pt.AcceptedRating Red (cxh)
065 */
066public class TimedMonitorValue extends MonitorValue {
067    /** Construct an actor.
068     *  @param container The container.
069     *  @param name The name of this actor.
070     *  @exception IllegalActionException If the entity cannot be contained
071     *   by the proposed container.
072     *  @exception NameDuplicationException If the container already has an
073     *   actor with this name.
074     */
075    public TimedMonitorValue(CompositeEntity container, String name)
076            throws NameDuplicationException, IllegalActionException {
077        super(container, name);
078    }
079
080    ///////////////////////////////////////////////////////////////////
081    ////                         protected methods                 ////
082
083    /** Return a token from the named input channel.
084     *  This is a protected method to allow subclasses to override
085     *  how inputs are observed.
086     *  @param i The channel
087     *  @return A token from the input channel or null if there is
088     *   nothing to display.
089     *  @exception IllegalActionException If reading the input fails.
090     */
091    @Override
092    protected Token _getInputToken(int i) throws IllegalActionException {
093        if (input.hasToken(i)) {
094            Token token = input.get(i);
095            Director director = getDirector();
096            if (director != null) {
097                return new StringToken(director.getModelTime() + ": " + token);
098            } else {
099                return token;
100            }
101        }
102        return null;
103    }
104}