Example usage for org.apache.commons.scxml.model SCXML getInitialTarget

List of usage examples for org.apache.commons.scxml.model SCXML getInitialTarget

Introduction

In this page you can find the example usage for org.apache.commons.scxml.model SCXML getInitialTarget.

Prototype

public final TransitionTarget getInitialTarget() 

Source Link

Document

Get the initial TransitionTarget.

Usage

From source file:org.dishevelled.piccolo.sprite.statemachine.AbstractStateMachineSprite.java

/**
 * Initialize the specified state machine.  Animations are loaded for all
 * the state ids and the current animation is set to the initial target, if any.
 *
 * <p>//from w ww  .j a  va 2  s . co  m
 * <b>Note:</b> this method should be called from the constructor
 * of a subclass after its state machine has been instantiated.
 * </p>
 *
 * @param stateMachine state machine to initialize, must not be null
 */
protected final void initializeStateMachine(final SCXML stateMachine) {
    if (stateMachine == null) {
        throw new IllegalArgumentException("stateMachine must not be null");
    }
    // load animations for state ids
    for (Iterator<?> entries = stateMachine.getTargets().entrySet().iterator(); entries.hasNext();) {
        Map.Entry<?, ?> entry = (Map.Entry<?, ?>) entries.next();
        String id = (String) entry.getKey();
        Object target = entry.getValue();
        if (target instanceof State) {
            Animation animation = createAnimation(id);
            if (animation != null) {
                animations.put(id, animation);
            }
        }
    }
    // set the current animation to the initial target, if any
    String initialTargetId = (stateMachine.getInitialTarget() == null) ? null
            : stateMachine.getInitialTarget().getId();
    if (animations.containsKey(initialTargetId)) {
        currentAnimation = animations.get(initialTargetId);
    }
    // create a state machine support class that delegates to this
    stateMachineSupport = new StateMachineSupport(this, stateMachine);
    // update current animation on entry to a new state
    stateMachineSupport.getExecutor().addListener(stateMachine, new AbstractSCXMLListener() {
        @Override
        public void onEntry(final TransitionTarget state) {
            Animation animation = animations.get(state.getId());
            if (animation != null) {
                currentAnimation = animation;
            }
        }
    });
}