/** * The intelligence behind the AlphaCanvas. * Contract of this component: Collects chars until it has a monogram, * then asks AlphaCanvas to use that monogram instead. * Waits to be handed chars; imposes new monogram on AlphaCanvas. */ public class AlphaSmarts { protected AlphaCanvas canvas; // The canvas private boolean charWaiting = false; // Have I already got a char? private char firstChar; // If so, the char. /** * Remember the canvas you're being smart for. */ public AlphaSmarts( AlphaCanvas c ) { this.canvas = c; } /** * What to do when a char comes in. (Store it or tell Canvas * about new monogram. * @param c the char that came in. */ public synchronized void handleChar( char c ) { if( ! this.charWaiting ){ // remember first char for later this.charWaiting = true; this.firstChar = c; } else { // two chars makes a monogram this.charWaiting = false; this.record( new SafeMonogram( this.firstChar, c ) ); } } /** * How to tell the canvas it's time to update the monogram. */ protected void record( SafeMonogram m ) { this.canvas.setText( m ); } }