/*
 *  Copyright 2002-2009 Mikhail Kryshen <mikhail@kryshen.net>
 *
 *  This file is part of BMTron.
 *
 *  BMTron is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  BMTron is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with BMTron.  If not, see <http://www.gnu.org/licenses/>.
 */
 
package kryshen.bmtron;

import java.awt.Container;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class TextField extends Container 
        implements MouseListener, KeyListener, FocusListener {
    private final Game bmt;
    private boolean active = false;
    private java.awt.TextField tf;
    
    private String note = null;

    private final int width = 333;
    private final int height = 35;

    TextField(String text, Game bmt) {
	this.bmt = bmt;

        setLayout(null);
    
        tf = new java.awt.TextField(text);
        tf.setSize(width, height);
        tf.setLocation(0, 0);	
        tf.setVisible(false);
	tf.setBackground(bmt.skin.buttonBackColor);
	tf.setForeground(bmt.skin.buttonTextColor);
	tf.setFont(bmt.skin.buttonFont);
        add(tf);
        
        tf.addKeyListener(this);
        tf.addFocusListener(this);

        setSize(width, height);
        addMouseListener(this);
    }
    
    public void setText(String text) {
        tf.setText(text);
        repaint();
    }
    
    public String getText() {
        return tf.getText();
    }
    
    public void setNote(String note) {
        this.note = note;
        tf.setVisible(false);
        repaint();
    }

    public String getNote() {
        return note;
    }

    public void paint(Graphics g) {
        g.setColor(bmt.skin.buttonBackColor);
        g.fillRect(0, 0, width - 1, height - 1);
        
        if (active) 
	    g.setColor(bmt.skin.activeButtonBorderColor); 
	else 
	    g.setColor(bmt.skin.buttonBorderColor);

        g.drawRect(0, 0, width - 1, height - 1);

	g.setColor(bmt.skin.buttonTextColor);

        String s = tf.getText();
        if (note != null)
            s += ' ' + note;

        g.setFont(bmt.skin.buttonFont);
        FontMetrics fm = g.getFontMetrics();
        
        int sw = fm.stringWidth(s);
        int sh = fm.getHeight();        
        g.drawString(s, (width - sw) / 2, (height - sh) / 2 + fm.getAscent());
        
        super.paint(g);
    }
    
    public void mouseEntered(MouseEvent e) { active = true; repaint(); }
    public void mouseExited(MouseEvent e) { active = false; repaint(); }
    
    public void mousePressed(MouseEvent e) {
        tf.setVisible(true);
        tf.requestFocus();
    }
        
    public void mouseReleased(MouseEvent e) {}    
    public void mouseClicked(MouseEvent e) {}
    
    public void keyPressed(KeyEvent e) {
        //System.err.println(e.getKeyCode());
        if (e.getKeyCode() == 10) {
            tf.setVisible(false);
	    requestFocus();
	}
    }
    public void keyReleased(KeyEvent e) {}
    public void keyTyped(KeyEvent e) {}

    public void focusGained(FocusEvent e) {}
    
    public void focusLost(FocusEvent e) {
        tf.setVisible(false);
    }
    
//    public void addMouseListener(MouseListener ml) {
//        super.addMouseListener(ml);
//        tf.addMouseListener(ml);
//    }
//    
//    public void removeMouseListener(MouseListener ml) {
//        super.removeMouseListener(ml);
//        tf.removeMouseListener(ml);
//    }
}
