// (c) MIT 2003.  All rights reserved.

// -*-Mode: Java-*- 
// $Id: EnterAnnotationModeRecognizer.java,v 1.2 2003/03/06 00:40:43 moltmans Exp $
// EnterAnnotationModeRecognizer.java -- 
// Author: Michael Oltmans <moltmans@ai.mit.edu> 
// Created: <Sat Nov  6 19:17:56 1999> 
<<<<<<< EnterAnnotationModeRecognizer.java
// Time-stamp: <2000-04-12 23:03:40 moltmans> 
=======
// Time-stamp: <2000-05-11 10:10:03 moltmans> 
>>>>>>> 1.8
package rec.annotate.recognizers;

import java.util.*;

import rec.annotate.*;
import rec.simulate.*;
import rec.simulate.widgets.*;
import rec.core.speech.*;
import rec.util.*;
import rec.sys.*;
import rec.speech.*;

public class EnterAnnotationModeRecognizer extends SimpleUtteranceRecognizer
{

  public EnterAnnotationModeRecognizer()
  {
    super( "enter_annotation_mode" );
  }

  public void performAction( Surface s, Object arg )
  {
    //    printSimObjects( s );
    addGrammarForSimObjects( s );
    ((SimulateSurface)s).findAttachments();
    s.getNotebook().assertDesign();
    s.enterEditMode( "Annotation" );
  }

  public void performUndo( Surface s, Object arg )
  {
    s.enterEditMode( EditModeManager.DEFAULT_MODE );
  }

  private void printSimObjects( Surface s )
  {
    Iterator it = s.getSurfaceWidgets().iterator();
    Object o;

    while ( it.hasNext() ) {
      o = it.next();
      if ( o instanceof WSimObject ) {
	DebugWindow.addLine( "Parts", ((WSimObject)o).getEnglishName() );
      }
    }
    
  }

  /** Add english names of all components to the grammar of components **/
  private void addGrammarForSimObjects( Surface s )
  {
    HashSet names = new HashSet();
    Iterator it = s.getSurfaceWidgets().iterator();
    AnnotationNotebook book = s.getAnnotationNotebook();
    Object o;

    while ( it.hasNext() ) {
      o = it.next();
      if ( o instanceof WSimObject ) {
	String basename = ((WSimObject)o).getEnglishName();
	if( basename.toLowerCase().endsWith( "body" ) )
	  basename = "body";
	if( basename == null )
	  continue;
	String name = basename;
	int i = 1;
	while( names.contains( name ) ) {
	  name = basename + " " + i++;
	}
	names.add( name );
	book.addName( name, (Widget)o );
      }
    }

    RecGrammar components =
      RecGrammar.getRecGrammar( "rec.annotate.recognizers.TopLevel" );

    components.addAlternatives( "lex-N_component", new Vector( names ) );
  }


}
