/* PathChooser actor enables the user to choose the execution path between * two branches according to the displayed information. * * Copyright (c) 2004 The Regents of the University of California. * All rights reserved. * * Permission is hereby granted, without written agreement and without * license or royalty fees, to use, copy, modify, and distribute this * software and its documentation for any purpose, provided that the * above copyright notice and the following two paragraphs appear in * all copies of this software. * * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN * IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY * OF SUCH DAMAGE. * * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY * OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, * UPDATES, ENHANCEMENTS, OR MODIFICATIONS. * * PT_COPYRIGHT_VERSION_2 * COPYRIGHTENDKEY */ package org.cipres.kepler; import javax.swing.JOptionPane; import ptolemy.actor.TypedAtomicActor; import ptolemy.kernel.CompositeEntity; import ptolemy.kernel.util.*; import ptolemy.actor.TypedIOPort; import ptolemy.data.Token; import ptolemy.data.StringToken; import ptolemy.data.type.BaseType; ////////////////////////////////////////////////////////////////////////// ////PathChooser /** * This actor displays the information imported from the input port "displayInfo" * and enables the user to choose the execution path from two branches: Yes * branch or No branch. According to the user's choice, the actor sends the * token imported from the input port "inputPort" to the corresponding output port. * This actor wraps the branch flow control as an actor. * * @author Zhijie Guan * @version $Id: $ */ public class PathChooser extends TypedAtomicActor { // TODO: assign a proper hash value to this static final long serialVersionUID = 1L; /** Construct PathChooser source with the given container and name. * @param container The container. * @param name The name of this actor. * @exception IllegalActionException If the entity cannot be contained * by the proposed container. * @exception NameDuplicationException If the container already has an * actor with this name. */ public PathChooser(CompositeEntity container, String name) throws NameDuplicationException, IllegalActionException { super(container, name); // construct the input ports displayInfo = new TypedIOPort(this, "displayInfo", true, false); displayInfo.setDisplayName("Display Information"); displayInfo.setTypeEquals(BaseType.STRING); inputPort = new TypedIOPort(this, "inputPort", true, false); inputPort.setDisplayName("Input Port"); inputPort.setTypeEquals(BaseType.GENERAL); // construct the output ports yesBranch = new TypedIOPort(this, "yesBranch", false, true); yesBranch.setDisplayName("Yes Branch"); noBranch = new TypedIOPort(this, "noBranch", false, true); noBranch.setDisplayName("No Branch"); // Set the type constraint. yesBranch.setTypeEquals(BaseType.GENERAL); noBranch.setTypeEquals(BaseType.GENERAL); } /////////////////////////////////////////////////////////////////// //// ports and parameters //// /** * The information that is going to be displayed to the user is sent * into the actor through this input port. This port is an input port * of type STRING. */ public TypedIOPort displayInfo = null; /** * The token that will be guided to the Yes/No branch is sent into the * actor through this input port. This port is an input port of type GENERAL. * This port is an output port of type STRING. */ public TypedIOPort inputPort = null; /** * The Yes Branch output port will be used to send out the token got * from the inputPort when the user choose "Yes" in the PathChooser * dialog. This port is an output of type GENERAL. */ public TypedIOPort yesBranch = null; /** * The No Branch output port will be used to send out the token got * from the inputPort when the user choose "No" in the PathChooser * dialog. This port is an output of type GENERAL. */ public TypedIOPort noBranch = null; /////////////////////////////////////////////////////////////////// //// functional variables //// /////////////////////////////////////////////////////////////////// //// public methods //// /** Display the information and let the user choose the execution path. * @exception IllegalActionException If it is thrown by the * send() method sending out the token. */ public void fire() throws IllegalActionException { if (displayInfo.hasToken(0) && inputPort.hasToken(0)) { String information = ((StringToken)displayInfo.get(0)).stringValue(); Token transportedToken = inputPort.get(0); // display the PathChooser dialog int choice = JOptionPane.showConfirmDialog(null, information + "\n\n" + "Would you like to continue?", "Path Chooser", JOptionPane.YES_NO_OPTION); if ( choice == JOptionPane.YES_OPTION) { // send the token from the input port to the Yes branch yesBranch.send(0, transportedToken); } else { // send the token from the input port to the No branch noBranch.send(0, transportedToken); } } } /** Post fire the actor. Return false to indicated that the * process has finished. If it returns true, the process will * continue indefinitely. */ public boolean postfire() { return false; } }