/*
   * Generate the states array initializer (AST).
   */
  private StatementListNode genStatesAST(
      StatesModel model,
      NodeFactory nodeFactory,
      HashSet<String> configNamespaces,
      boolean generateDocComments,
      StatementListNode statementList) {
    StatementListNode result = statementList;

    Set<String> states = model.info.getStateNames();
    if (!states.isEmpty()) {
      ArgumentListNode statesArgumentList = null;

      for (Iterator<String> iter = states.iterator(); iter.hasNext(); ) {
        State state = (State) model.stateByName((String) iter.next());
        if (state != null) {
          MemberExpressionNode stateExpression =
              state.generateDefinitionBody(
                  nodeFactory, configNamespaces,
                  generateDocComments, bindingsQueue);
          statesArgumentList = nodeFactory.argumentList(statesArgumentList, stateExpression);
        }
      }

      LiteralArrayNode literalArray = nodeFactory.literalArray(statesArgumentList);
      ArgumentListNode argList = nodeFactory.argumentList(null, literalArray);
      IdentifierNode statesIdentifier = nodeFactory.identifier(STATES, false);
      SetExpressionNode selector = nodeFactory.setExpression(statesIdentifier, argList, false);
      MemberExpressionNode memberExpression = nodeFactory.memberExpression(null, selector);
      ListNode list = nodeFactory.list(null, memberExpression);
      ExpressionStatementNode expressionStatement = nodeFactory.expressionStatement(list);
      result = nodeFactory.statementList(result, expressionStatement);
    }
    return result;
  }
  /*
   * 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;
  }
  /*
   * Generate the states array.
   */
  private void genStates(StatesModel model, CodeFragmentList list, String indent) {
    Set<String> states = model.info.getStateNames();

    if (!states.isEmpty()) {
      list.add(indent, "states = [", 0);

      indent += StatesGenerator.INDENT;
      for (Iterator<String> iter = states.iterator(); iter.hasNext(); ) {
        State state = (State) model.stateByName((String) iter.next());
        if (state != null) {
          state.getDefinitionBody(list, indent, bindingsQueue);
          if (iter.hasNext()) list.add(indent, ",", 0);
        }
      }
      indent = indent.substring(0, indent.length() - INDENT.length());
      list.add(indent, "];", 0);
    }
  }