/**
 * Bases.java
 *
 *
 * Created: Mon Sep 13 15:03:18 1999
 *
 * @author Gareth
 * @version
 */

import java.awt.*;
import java.awt.event.*;

public class Bases extends Frame
    implements ActionListener {

Choice fromList, toList;
Button convert;
TextField input, result;
static Frame program;
    
    public Bases() {
	// Frame appearance
	this.setSize( 300, 150 );
	this.setTitle( "The bases machine!" );
	this.setLayout( new FlowLayout() );
	fromList = getList();
	toList = getList();
	input = new TextField(10);
	result = new TextField(10);
	convert = new Button( "Convert" );
	convert.addActionListener( this );
	this.add( fromList );
	this.add( input );
	this.add( toList );
	this.add( result );
	this.add( convert );
	this.setVisible( true );
	this.addWindowListener( new windowListener() );
	program = this;
	}

    public static void main( String[] args ){
	new Bases();
	}

    /**
     * React to button presses
     * @param e The event object for the button press
     */
    public void actionPerformed( ActionEvent e ){
	System.out.println( "Button: " + e.getActionCommand() );
	Conversion c = new Conversion(input.getText(), 
				      Integer.parseInt( fromList.getSelectedItem() ),
				      Integer.parseInt( toList.getSelectedItem() ) );
	result.setText( c.getResult() );
	}

    /**
     * Returns a list containing the numbers for the possible bases
     * @return A Choice object with the numbers
     */
    private Choice getList(){
	final int FIRST_BASE = 2;
	final int LAST_BASE = 16;
	Choice c = new Choice();
	for( int i=FIRST_BASE; i<=LAST_BASE; i++ )
	    c.addItem(String.valueOf(i));
	return c;
	}

    /**
     * Gets rid of the window on closing it, exits the program
     */
    static class windowListener extends WindowAdapter{
	public void windowClosing( WindowEvent e ){
	    program.dispose();
	    System.exit(0);
	    }
	}

    } // Bases

/**
 * Performs and holds a single conversion
 */
class Conversion{
    
    final String[] letters = {"a", "b", "c", "d", "e", "f"};
    String result="";
    int _from, _to;

    public Conversion( String input, int from, int to ){
      	int decResult;
	_from = from;
	_to = to;
	decResult = decConvert( input );
	anyConvert( decResult );
	}
    
    private int decConvert( String input ){
	int r=0;
	int next=0;
	for( int i=0; i<input.length(); i++ ){
	    try{
		next = Integer.parseInt( String.valueOf( input.charAt(i) ) );}
	    catch( NumberFormatException e ){
		if( input.charAt(i) == 'a' ) next = 10;	    
		if( input.charAt(i) == 'b' ) next = 11;
		if( input.charAt(i) == 'c' ) next = 12;
		if( input.charAt(i) == 'd' ) next = 13;
		if( input.charAt(i) == 'e' ) next = 14;
		if( input.charAt(i) == 'f' ) next = 15;
		}
	    r = _from*r + next;
	    }
	System.out.println( "decConvert returning " + r );
	return r;
	}
    
    private String anyConvert( int input ){
        int rem;
	if( input == 0 )
	    return null;
	else{
	    rem = input%_to; 
	    if( rem < 10 )
		result = String.valueOf( rem ) + result;
	    else
		result = letters[rem-10] + result;
	    System.out.println( "Result now " + result );
	    return anyConvert( input/_to );
	    }
	}

    /**
     * Get the value of result.
     * @return Value of result.
     */
    public String getResult() {return result;}
    
    /**
     * Set the value of result.
     * @param v  Value to assign to result.
     */
    public void setResult(String  v) {this.result = v;}
    
    }
