001/* Display image inputs in the icon.
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.lib;
029
030import java.awt.Image;
031import java.awt.Toolkit;
032import java.net.URL;
033
034import ptolemy.actor.lib.Sink;
035import ptolemy.data.ImageToken;
036import ptolemy.data.expr.FileParameter;
037import ptolemy.data.type.BaseType;
038import ptolemy.kernel.CompositeEntity;
039import ptolemy.kernel.util.IllegalActionException;
040import ptolemy.kernel.util.NameDuplicationException;
041import ptolemy.kernel.util.Workspace;
042import ptolemy.vergil.icon.ImageIcon;
043
044///////////////////////////////////////////////////////////////////
045//// MonitorImage
046
047/**
048 Display image inputs in the icon.
049
050 @author Edward A. Lee
051 @version $Id$
052 @since Ptolemy II 0.3
053 @Pt.ProposedRating Yellow (eal)
054 @Pt.AcceptedRating Red (bilung)
055 */
056public class MonitorImage extends Sink {
057    /** Construct an actor.
058     *  @param container The container.
059     *  @param name The name of this actor.
060     *  @exception IllegalActionException If the entity cannot be contained
061     *   by the proposed container.
062     *  @exception NameDuplicationException If the container already has an
063     *   actor with this name.
064     */
065    public MonitorImage(CompositeEntity container, String name)
066            throws NameDuplicationException, IllegalActionException {
067        super(container, name);
068
069        input.setTypeEquals(BaseType.OBJECT);
070
071        _icon = new ImageIcon(this, "_icon");
072        _icon.setPersistent(false);
073        FileParameter source = new FileParameter(this, "source");
074        source.setExpression(
075                "$CLASSPATH/ptolemy/vergil/kernel/attributes/ptIIplanetIcon.gif");
076        URL url = source.asURL();
077        Toolkit tk = Toolkit.getDefaultToolkit();
078        Image image = tk.getImage(url);
079        _icon.setImage(image);
080    }
081
082    ///////////////////////////////////////////////////////////////////
083    ////                         public methods                    ////
084
085    /** Clone the actor into the specified workspace.
086     *  @param workspace The workspace for the new object.
087     *  @return A new actor.
088     *  @exception CloneNotSupportedException If a derived class contains
089     *   an attribute that cannot be cloned.
090     */
091    @Override
092    public Object clone(Workspace workspace) throws CloneNotSupportedException {
093        MonitorImage newObject = (MonitorImage) super.clone(workspace);
094        newObject._icon = (ImageIcon) newObject.getAttribute("_icon");
095        return newObject;
096    }
097
098    /** Read at most one token from the input and record its value.
099     *  @exception IllegalActionException If the input token does not
100     *   contain an image, or if there is no director.
101     *  @return True.
102     */
103    @Override
104    public boolean postfire() throws IllegalActionException {
105        if (input.hasToken(0)) {
106            ImageToken token = (ImageToken) input.get(0);
107            Image value = token.asAWTImage();
108            _icon.setImage(value);
109        }
110
111        return super.postfire();
112    }
113
114    ///////////////////////////////////////////////////////////////////
115    ////                         private members                   ////
116    // The image icon.
117    private ImageIcon _icon;
118}