/* $Id$
 *
 * Copyright (c) 1999 Xenonsoft Limited. All Rights Reserved.
 *
 * This software is protected by copyright. You are hereby notified from
 * now by reading this message. This software is also the confidential 
 * and proprietary information of Xenonsoft Limited.  ("Confidential
 * Information"). 
 *
 * This software is distributed under the Xenonsoft Public end user 
 * License ("XPeL"), where the machine-readable source code is provided 
 * under the "Open Source" model.
 * For more information, please read the file "LICENSE-XPL.txt" 
 */

// File:`AbstractUserKeyControl.java'
//
//
// DESCRIPTION
//	Default User Defined Key tuple
// 
// Author
//
//	Peter Pilgrim
//	Thu Aug 05 19:38:34 BST 1999
// 
// RCS HEADER ``AbstractUserKeyTuple.java''
// 
//	$Author$
//	$Date$
//	$Revision$    $State$    $Locker$
//

package xenon.gamekit;

import java.awt.*;				// for Component, Panel
import java.awt.event.*;			// for KeyEvent, InputEvent
import java.io.Serializable;


/**
 * An abstract GUI control that allows game key to be user defined.
 * 
 * @author Peter Pilgrim, Thu Aug 05 19:38:52 BST 1999
 * @version $Id$
 */
public abstract class AbstractUserKeyControl {
    
    protected boolean		duplicates;
    protected UserKeyTuple []	userkeys;

    /**
     * constructs a GUI control for user defined game keys
     * @param userkeys the user defined key 
     * @param duplicate if true then allow duplicate user defined keys
     */
    public AbstractUserKeyControl( 
	UserKeyTuple [] userkeys, boolean duplicates )
    {
	this.userkeys = userkeys;
	this.duplicates = duplicates;
	
	checkit();
    }

    /** get the custom control for the user defined key GUI 
     * <p>
     * <b>subclasser</b> should override this method.
     */
    public abstract Component getControl();

    /** reset the user defined keys to their default key codes
     * programmatically.
     */
    public void resetToDefaultKeys()
    {
	for ( int r=0; r<userkeys.length; ++r ) {
	    setNewUserDefinedKeyCode( userkeys[r].defKeyCode, r );
	}
    }
    
    /** gets the boolean flag that allow duplicate user defined keys */
    public boolean isAllowDuplicates()
    {
	return (duplicates);
    }
    
    /** sets the boolean flag that allow duplicate user defined keys */
    public void setAllowDuplicates( boolean flag )
    {
	duplicates = flag;
    }
    
    /** protected convenience method to get the string
     * of the change key buttons from a supplied <b>KeyEvent</b> 
     * keycode 
     * @param keycode the keycode
     * @return formatted <code>String</code> for the key button.
     */
    protected String getKeyCodeToString( int keycode )
    {
	StringBuffer buf = new StringBuffer();
	buf.append( KeyEvent.getKeyText( keycode ) );
	for ( int q=buf.length(); q<10; ++q ) {
	    buf.append( ' ' );
	}
	return buf.toString();
    }
    
    /**
     * A convenience method for <b>subclassers</code>
     * sets the keycode for a new user defined keycode.
     *
     * <p>
     * <b>Subclassers</b> should override/implement this method
     * 
     * @param keycode the new keycode
     * @param idx the index of the user defined key array
     *
     * @return <code>boolean</code> true if key can be set,
     * or false if the key has been duplicated, and
     * if the allow duplicates property is set to false.
     *
     */
    protected boolean setNewUserDefinedKeyCode( int keycode , int idx )
    {
	if ( !duplicates ) {
	    // Check for duplicates keycodes
	    for ( int r=0; r<userkeys.length; ++r ) {
		if ( r != idx && keycode == userkeys[r].keyCode )
		    return (false);
	    }
	}
	
	UserKeyTuple keytuple = userkeys[idx];
	keytuple.keyCode = keycode;

	return (true);
    }
    
    /** convenience method to check user defined key parameter
     * at construction time.
     */
    protected void checkit()
    {
	if ( userkeys == null )
	    throw new NullPointerException(
		"null pointer user keys supplied" );
	if ( userkeys.length == 0 )
	    throw new IllegalArgumentException(
		"user keys is emptied." );

	for ( int j=0; j<userkeys.length; ++j ) 
	    for ( int k=j+1; k<userkeys.length; ++k ) {
		if ( userkeys[j].keyDescrip == null )
		    throw new NullPointerException(
			"no user key description supplied at index:"+j );
		if ( userkeys[j].defKeyCode == 0 )
		    throw new IllegalArgumentException(
			"zero user keycode at index:"+j );
		if ( userkeys[j].defKeyCode == 0 )
		    throw new IllegalArgumentException(
			"zero default user keycode at index:"+j );
		if ( userkeys[j].defKeyCode == userkeys[k].defKeyCode )
		    throw new IllegalArgumentException(
			"default user keycode at index:"+j+
			" is the same at index:"+k+" ( VK_"+
			KeyEvent.getKeyText( userkeys[j].defKeyCode )+" )" );
	    }
    }
    

}

// fini
