////////////////////////////////////////////////////////////////////////////////
//																			  //
// AUTHOR:      Tevfik Metin Sezgin	                        				  //
//				Massachusetts Institute of Technology						  //
//				Department of Electrical Engineering and Computer Science	  //
//				Artificial Intelligence Laboratory							  //
//																			  //
// E-MAIL:		mtsezgin@ai.mit.edu, mtsezgin@mit.edu						  //
//																			  //
// COPYRIGHT:   Tevfik Metin Sezgin                    						  //
//              All rights reserved. This code can not be copied, modified,   //
//              or distributed in whole or partially without the written 	  //
//				permission of the author. Also see the COPYRIGHT file.		  //
//																			  //
////////////////////////////////////////////////////////////////////////////////

  /**
  *
  * See the end of the file for the log of changes.
  *
  * $Author: mtsezgin $
  * $Date: 2000/06/08 03:20:07 $
  * $Revision: 1.18 $
  * $Headers$
  * $Id: Tablet.java,v 1.18 2000/06/08 03:20:07 mtsezgin Exp $
  * $Name:  $
  * $Locker:  $
  * $Source: /mit/mtsezgin/CVS/Repository/REC/Tablet.java,v $
  *
  **/


import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToolBar;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Label;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.Panel;
import java.awt.Toolkit;

import java.util.Vector;

import java.awt.geom.AffineTransform;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

/**
  * Acronyms: sdr -> StrokeDataReader, sd -> StrokeData
  *
  * This class handles the updates to various frames.
  *
  **/

class      Tablet
extends    TabletGUIBehavior
implements KeyListener
{
    /**
    *
    * The constructor.
    *
    **/
    public Tablet( int     max_data_points, 
				   double  speed_average_scale, 
				   String  cursor_image_name,
				   boolean platform_is_windows9x,
				   boolean platform_is_windowsNT )
    {
        super( max_data_points,
			   speed_average_scale,
			   cursor_image_name,
			   platform_is_windows9x,
			   platform_is_windowsNT );

        addKeyListener( this );
		addAllComponentsAsKeyListeners();
    }


    /**
    *
    * Add Components As Key Listeners
    *
    **/
    public void
    addAllComponentsAsKeyListeners()
	{
		Component components[] = AWTUtil.getAllComponentsRecursively( this );

		for ( int i=0; i<components.length; i++ ) {
			components[i].addKeyListener( this );
		}
		System.out.println( components.length + " components detected..." );
	}


    /**
    *
    * Handle key stroke events
    *
    **/
    public void
    keyPressed( KeyEvent k )
    {
    }
     

    /**
    *
    * Handle key stroke events
    *
    **/
    public void
    keyReleased( KeyEvent k )
    {    
    }


    /**
    *
    * Handle key stroke events
    *
    **/
    public void
    keyTyped( KeyEvent ke )
    {
        char key = ke.getKeyChar();
        
        if ( key == 'c' ) {
            repaint();
            return;
        }
        
        if ( key == '1' ) {
            debug = !debug;
            return;
        }
        
        if ( key == '2' ) {
            debug2 = !debug2;
            return;
        }
        
        if ( key == 'r' ) {
            object_manager.reset();
            GraphicsUtil.clearComponent( this );
            return;
        }
        
        if ( key == 't' ) {
			toggle();
        }
        
		if ( key == '@' ) {
			Blackboard.paintGeneralPath( Blackboard.general_path );
        }
        
		if ( key == '#' ) {
			Blackboard.paintGeneralPathInSegments( Blackboard.general_path );
        }
        
        if ( key == 'i' ) {
			debug_graphics.setColor( Color.red );
			
			for ( int i=0; i<direction_fit.length-1; i++ ) {
				debug_graphics.drawLine( direction_fit[i].x,
				                         direction_fit[i].y,
				                         direction_fit[i+1].x,
				                         direction_fit[i+1].y );
			}

			for ( int i=0; i<direction_fit.length; i++ ) {
				debug_graphics.fillOval( 
					direction_fit[i].x-
					(int)(direction_fit[i].certainty*20)/2,
					direction_fit[i].y-
					(int)(direction_fit[i].certainty*20)/2,
					(int)(direction_fit[i].certainty*20),
					(int)(direction_fit[i].certainty*20) );
			}
			debug_graphics.setColor( Color.black );
			for ( int i=0; i<direction_fit.length; i++ ) {
				debug_graphics.drawLine(
					direction_fit[i].x,
					direction_fit[i].y,
					direction_fit[i].x,
					direction_fit[i].y );
			}
			setStatusMessage( "Output obtained by direction information..." );
        }
        
        if ( key == 'k' ) {
			debug_graphics.setColor( Color.blue );
			
			for ( int i=0; i<speed_fit.length-1; i++ ) {
				debug_graphics.drawLine( speed_fit[i].x,
				                         speed_fit[i].y,
				                         speed_fit[i+1].x,
				                         speed_fit[i+1].y );
			}

			for ( int i=0; i<speed_fit.length; i++ ) {
				debug_graphics.fillOval( 
					speed_fit[i].x - 
						(int)(speed_fit[i].certainty*20)/2,
					speed_fit[i].y - 
						(int)(speed_fit[i].certainty*20)/2,
					(int)(speed_fit[i].certainty*20),
					(int)(speed_fit[i].certainty*20) );
			}
			debug_graphics.setColor( Color.white );
			for ( int i=0; i<speed_fit.length; i++ ) {
				debug_graphics.drawLine(
					speed_fit[i].x,
					speed_fit[i].y,
					speed_fit[i].x,
					speed_fit[i].y );
			}
			setStatusMessage( "Output obtained by speed information..." );
        }

        if ( key == 'p' ) {
			for ( int i=0; i<data.vertices.length; i++ ) {
				System.out.println( data.vertices[i] );
			}
        }

        if ( key == 'w' ) {
            bar_statistics_module.setVisible( 
                !bar_statistics_module.isVisible() );
                
            dsw_statistics_module.setVisible( 
                !dsw_statistics_module.isVisible() );
                
            ddsw_statistics_module.setVisible( 
                !ddsw_statistics_module.isVisible() );
                
            sliding_window_statistics_module.setVisible( 
                !sliding_window_statistics_module.isVisible() );
                
            System.out.println( "Statistics window." );
            return;
        }
        
        if ( key == 'e' ) {
            dsw_statistics_module.setVisible( 
                !dsw_statistics_module.isVisible() );
            ddsw_statistics_module.setVisible( 
                !ddsw_statistics_module.isVisible() );

            System.out.println( "Statistics window." );
            return;
        }
        
		if ( key == 'b' ) {
			double d[] = data.d;
			setStatusMessage( "Direction obtained by " + 
							 LinearFit.fitMethodToString( data.fit_method ) );
			debug_graphics.setColor( Color.green );

	        for ( int i=0; i<d.length; i++ ) {
				double a = d[i];
				int mid_point_x = data.vertices[i].x;
				int mid_point_y = data.vertices[i].y;
				int dx          = (int)(Math.cos(a)*100);
				int dy          = (int)(Math.sin(a)*100);
				debug_graphics.drawLine( mid_point_x - dx,
				 						 mid_point_y - dy,
				 						 mid_point_x + dx,
				 						 mid_point_y + dy );
	        }
        }

        if ( key == 'z' ) {
            object_manager.undo();
            return;
        }
        
        if ( key == 'q' ) {
            System.exit( 0 );
            return;
        }
    }
}

/**
  *
  * $Log: Tablet.java,v $
  *
  *
  **/
