001         package com.croftsoft.apps.mars.controller;
002    
003         import java.awt.*;
004         import java.awt.event.*;
005    
006         import com.croftsoft.core.animation.clock.Timekeeper;
007         import com.croftsoft.core.lang.NullArgumentException;
008    
009         /*********************************************************************
010         * Adjusts the time factor.
011         *
012         * @version
013         *   2003-04-18
014         * @since
015         *   2003-04-02
016         * @author
017         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
018         *********************************************************************/
019    
020         public final class  TimeController
021           extends KeyAdapter
022         //////////////////////////////////////////////////////////////////////
023         //////////////////////////////////////////////////////////////////////
024         {
025    
026         private final Timekeeper  timekeeper;
027    
028         private final double      timeFactorDefault;
029    
030         private final double      timeFactorDelta;
031    
032         //////////////////////////////////////////////////////////////////////
033         //////////////////////////////////////////////////////////////////////
034    
035         public  TimeController (
036           Timekeeper  timekeeper,
037           double      timeFactorDefault,
038           double      timeFactorDelta,
039           Component   component )
040         //////////////////////////////////////////////////////////////////////
041         {
042           NullArgumentException.check ( this.timekeeper = timekeeper );
043    
044           this.timeFactorDefault = timeFactorDefault;
045    
046           this.timeFactorDelta   = timeFactorDelta;
047    
048           component.addKeyListener ( this );
049    
050           component.requestFocus ( );
051         }
052    
053         //////////////////////////////////////////////////////////////////////
054         // interface KeyListener methods
055         //////////////////////////////////////////////////////////////////////
056    
057         public void  keyPressed ( KeyEvent  keyEvent )
058         //////////////////////////////////////////////////////////////////////
059         {
060           char  keyChar = keyEvent.getKeyChar ( );
061    
062           if ( ( keyChar == '+' )
063             || ( keyChar == '=' )
064             || ( keyChar == '-' ) )
065           {
066             double  timeFactor = timekeeper.getTimeFactor ( );
067    
068             if ( keyChar == '+' )
069             {
070               timeFactor += timeFactorDelta;
071             }
072             else if ( keyChar == '-' )
073             {
074               timeFactor -= timeFactorDelta;
075             }
076             else
077             {
078               timeFactor = timeFactorDefault;
079             }
080    
081             timekeeper.setTimeFactor ( timeFactor );
082    
083             System.out.println ( "Time Factor:  " + timeFactor );
084           }
085         }
086    
087         //////////////////////////////////////////////////////////////////////
088         //////////////////////////////////////////////////////////////////////
089         }