/*
   * Generate initializers for any declared states, as well as any event
   * initializers for states.
   */
  private void genStateInitializers(StatesModel model, CodeFragmentList list, String indent) {
    Set<String> states = model.info.getStateNames();
    int count = 0;
    for (Iterator<String> iter = states.iterator(); iter.hasNext(); ) {
      State state = (State) model.stateByName((String) iter.next());
      if (state != null) {
        // Declaration initializer
        if (state.isDeclared()) {
          list.add(indent, state.getId() + "= states[" + count + "];", 0);
        }

        // Event handlers
        for (Iterator<Initializer> initializers = state.getEvents(); initializers.hasNext(); ) {
          EventInitializer ei = (EventInitializer) initializers.next();
          list.add(
              indent,
              "states["
                  + count
                  + "].addEventListener(\""
                  + ei.getName()
                  + "\", "
                  + ei.getValueExpr()
                  + " );",
              0);
        }
      }
      count++;
    }
  }
  /*
   * Generate AST initializers for any declared states, as well as any event
   * initializers for states.
   */
  private StatementListNode genStateInitializersAST(
      StatesModel model,
      NodeFactory nodeFactory,
      HashSet<String> configNamespaces,
      boolean generateDocComments,
      StatementListNode statementList) {
    StatementListNode result = statementList;
    Set<String> states = model.info.getStateNames();
    int count = 0;
    for (Iterator<String> iter = states.iterator(); iter.hasNext(); ) {
      State state = (State) model.stateByName((String) iter.next());
      if (state != null) {
        String identifier = state.getId().intern();
        IdentifierNode stateIdentifier = nodeFactory.identifier(identifier, false);
        IdentifierNode statesIdentifier = nodeFactory.identifier(STATES, false);

        LiteralNumberNode numberNode = nodeFactory.literalNumber(count);
        ArgumentListNode getIndexArgList = nodeFactory.argumentList(null, numberNode);
        GetExpressionNode getIndexExpression = nodeFactory.getExpression(getIndexArgList);
        getIndexExpression.setMode(Tokens.LEFTBRACKET_TOKEN);

        GetExpressionNode getStatesExpression = nodeFactory.getExpression(statesIdentifier);
        MemberExpressionNode base = nodeFactory.memberExpression(null, getStatesExpression);

        MemberExpressionNode getExpr = nodeFactory.memberExpression(base, getIndexExpression);

        // Declaration initializer
        if (state.isDeclared()) {
          ArgumentListNode argList = nodeFactory.argumentList(null, getExpr);
          SetExpressionNode selector = nodeFactory.setExpression(stateIdentifier, argList, false);
          MemberExpressionNode outer = nodeFactory.memberExpression(null, selector);
          ListNode list = nodeFactory.list(null, outer);
          ExpressionStatementNode expressionStatement = nodeFactory.expressionStatement(list);
          result = nodeFactory.statementList(result, expressionStatement);
        }

        // Event handlers
        for (Iterator<Initializer> initializers = state.getEvents(); initializers.hasNext(); ) {
          EventInitializer ei = (EventInitializer) initializers.next();
          IdentifierNode addEventIdentifier = nodeFactory.identifier(ADDEVENTLISTENER, false);
          LiteralStringNode eventName = nodeFactory.literalString(ei.getName());
          macromedia.asc.parser.Node valueNode =
              ei.generateValueExpr(nodeFactory, configNamespaces, generateDocComments);
          ArgumentListNode addEventArgs = nodeFactory.argumentList(null, eventName);
          addEventArgs = nodeFactory.argumentList(addEventArgs, valueNode);

          CallExpressionNode addListener =
              (CallExpressionNode) nodeFactory.callExpression(addEventIdentifier, addEventArgs);
          addListener.setRValue(false);
          MemberExpressionNode outer = nodeFactory.memberExpression(getExpr, addListener);
          ListNode list = nodeFactory.list(null, outer);
          ExpressionStatementNode expressionStatement = nodeFactory.expressionStatement(list);
          result = nodeFactory.statementList(result, expressionStatement);
        }
      }
      count++;
    }
    return result;
  }