/*
 * ISBNForm.java
 *
 * Created on October 10, 2003, 12:00 AM
 */

package org.tienshiao.BookCheck;

import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

/**
 *
 * @author  tma
 */
public class ISBNForm extends Canvas {
    private int BLINK_RATE = 100;
    private int MAXLENGTH = 10;
    private int MAXWIDTH;
    private int XOFFSET;

    private Timer timer;
    private StringBuffer isbn;
    private int currPos = 0;
    private Font font = null;
    
    private boolean done = false;
    
    
    /** Creates a new instance of ISBNForm */
    public ISBNForm() {
        isbn = new StringBuffer(MAXLENGTH);
    }

    protected void keyPressed(int keyCode) {
        if (keyCode >= KEY_NUM0 && keyCode <= KEY_NUM9) {
            if (currPos < MAXLENGTH && isbn.length() < MAXLENGTH) {
                isbn.insert(currPos, keyCode - KEY_NUM0);
                currPos++;
                repaint();
            }
        } else if (keyCode == KEY_STAR) {
            if (currPos < MAXLENGTH && isbn.length() < MAXLENGTH) {
                isbn.insert(currPos, 'X');
                currPos++;
                repaint();
            }
        } else if (keyCode == -3 || keyCode == -8 || 
            keyCode == -12 || keyCode == KEY_POUND) {
            if (currPos > 0) {
                currPos--;
                isbn.deleteCharAt(currPos);
                repaint();
            }
        }
    }
    
    protected void paint(Graphics g) {
        if (font == null) {
            font = g.getFont();
            MAXWIDTH = 0;
            int w;
            for (int i = 0; i < 10; i++) {
                w = font.stringWidth(Integer.toString(i));
                if (w > MAXWIDTH) {
                    MAXWIDTH = w;
                }
            }
            w = font.stringWidth("X");
            if (w > MAXWIDTH) {
                MAXWIDTH = w;
            }
            MAXWIDTH *= 10;
            XOFFSET = (getWidth() - MAXWIDTH) / 2;            
        }
        g.setColor(255, 255, 255);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(0, 0, 0);
        g.drawString("Enter ISBN (* = X):", getWidth()/2, 0, Graphics.TOP|Graphics.HCENTER);
        g.drawRect(XOFFSET - 2, font.getHeight(),  MAXWIDTH + 4, font.getHeight() + 4);
        g.drawString(isbn.toString(), XOFFSET, font.getHeight() + 2, Graphics.TOP|Graphics.LEFT);
        
        // Draw cursor
        if ((System.currentTimeMillis() / BLINK_RATE) % 2 == 1) {
            int x = XOFFSET + font.substringWidth(isbn.toString(), 0, currPos);
            g.drawLine(x, font.getHeight() + 2, x, 2 * font.getHeight() + 2);
        }
    }
    
    public void run() {
        while (done == false) {
                repaint();
                serviceRepaints();
        }
    }
    
    protected void showNotify() {
        // Create a Timer to update the display.
        TimerTask task = new TimerTask() {
            public void run() {
                repaint();
            }
        };
        timer = new Timer();
        timer.schedule(task, 0, BLINK_RATE / 2);  
    }
    
    protected void hideNotify() {
        timer.cancel();
    }
        
    public String getISBN() {
        return isbn.toString();
    }
}
