001/* A constant source that is placeable.
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.actor.lib.gui;
029
030import ptolemy.actor.injection.ActorModuleInitializer;
031import ptolemy.actor.injection.PortableContainer;
032import ptolemy.actor.injection.PortablePlaceable;
033import ptolemy.actor.injection.PtolemyInjector;
034import ptolemy.actor.lib.Const;
035import ptolemy.kernel.CompositeEntity;
036import ptolemy.kernel.util.IllegalActionException;
037import ptolemy.kernel.util.NameDuplicationException;
038
039///////////////////////////////////////////////////////////////////
040//// ConstPlaceable
041
042/**
043 Produce a constant output from an actor that is placeable in a GUI.
044
045 <p>The value of the
046 output is that of the token contained by the <i>value</i> parameter,
047 which by default is an IntToken with value 1. The type of the output
048 is that of <i>value</i> parameter.
049 <p>
050 If the trigger port is connected, then this actor fires only if a token is
051 provided on any channel of the trigger port. The value of that token
052 does not matter. Specifically, if there is no such token, the prefire()
053 method returns false.</p>
054
055 @author Yuhong Xiong, Edward A. Lee
056 @version $Id$
057 @since Ptolemy II 10.0
058 @Pt.ProposedRating Green (eal)
059 @Pt.AcceptedRating Green (bilung)
060 */
061public class ConstPlaceable extends Const implements PortablePlaceable {
062    /** Construct a constant source with the given container and name.
063     *  Create the <i>value</i> parameter, initialize its value to
064     *  the default value of an IntToken with value 1.
065     *  @param container The container.
066     *  @param name The name of this actor.
067     *  @exception IllegalActionException If the entity cannot be contained
068     *   by the proposed container.
069     *  @exception NameDuplicationException If the container already has an
070     *   actor with this name.
071     */
072    public ConstPlaceable(CompositeEntity container, String name)
073            throws NameDuplicationException, IllegalActionException {
074        super(container, name);
075    }
076
077    ///////////////////////////////////////////////////////////////////
078    ////                         public methods                    ////
079
080    /** Send the token in the <i>value</i> parameter to the output.
081     *  @exception IllegalActionException If it is thrown by the
082     *   send() method sending out the token.
083     */
084    @Override
085    public void fire() throws IllegalActionException {
086        super.fire();
087        _getImplementation().setValue(value.getToken());
088    }
089
090    /** Place the visual representation of the actor into the specified container.
091     *  @param container The container in which to place the object, or
092     *   null to specify that there is no current container.
093     */
094    @Override
095    public void place(PortableContainer container) {
096        _getImplementation().place(container);
097    }
098
099    ///////////////////////////////////////////////////////////////////
100    ////                         private methods                   ////
101
102    /** Get the right instance of the implementation depending upon the
103     *  of the dependency specified through dependency injection.
104     *  If the instance has not been created, then it is created.
105     *  If the instance already exists then return the same.
106     *
107     *        <p>This code is used as part of the dependency injection needed for the
108     *  HandSimDroid project, see $PTII/ptserver.  This code uses dependency
109     *  inject to determine what implementation to use at runtime.
110     *  This method eventually reads ptolemy/actor/ActorModule.properties.
111     *  {@link ptolemy.actor.injection.ActorModuleInitializer#initializeInjector()}
112     *  should be called before this method is called.  If it is not
113     *  called, then a message is printed and initializeInjector() is called.</p>
114     *  @return the implementation.
115     */
116    private TextFieldContainerInterface _getImplementation() {
117        if (_implementation == null) {
118            if (PtolemyInjector.getInjector() == null) {
119                System.err.println("Warning: main() did not call "
120                        + "ActorModuleInitializer.initializeInjector(), "
121                        + "so Const is calling it for you.");
122                ActorModuleInitializer.initializeInjector();
123            }
124            _implementation = PtolemyInjector.getInjector()
125                    .getInstance(TextFieldContainerInterface.class);
126        }
127        return _implementation;
128    }
129
130    ///////////////////////////////////////////////////////////////////
131    ////                         private variables                 ////
132
133    /** Implementation of the ConstInterface.  This code is used as part
134     *  of the dependency injection needed for the HandSimDroid project, see
135     *  $PTII/ptserver.  Note that if you get a NullPointerException here,
136     *  then the solution is to have your main() method call
137     *  ActorModuleInitializer.initializeInjector().
138     */
139    private TextFieldContainerInterface _implementation;
140}