/*
 * @(#)JIntegerTextField.java  1.0  August 1, 2007
 *
 * Copyright (c) 2007 by the original authors of JHotDraw
 * and all its contributors.
 * All rights reserved.
 *
 * The copyright of this software is owned by the authors and  
 * contributors of the JHotDraw project ("the copyright holders").  
 * You may not use, copy or modify this software, except in  
 * accordance with the license agreement you entered into with  
 * the copyright holders. For details see accompanying license terms. 
 */

package org.jhotdraw.gui;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;

/**
 * A JTextField which only accepts integer values as input.
 * <p>
 * Use methods setValue/getValue to retrieve the integer value of the
 * JTextField.
 *
 * @author Werner Randelshofer
 * @version 1.0 August 1, 2007 Created.
 */
public class JIntegerTextField extends JTextField {
    private int value;
    private int minimum = Integer.MIN_VALUE;
    private int maximum = Integer.MAX_VALUE;
    
    private class DocumentHandler implements DocumentListener {
        public void insertUpdate(DocumentEvent e) {
            updateValue();
        }
        
        public void removeUpdate(DocumentEvent e) {
            updateValue();
        }
        
        public void changedUpdate(DocumentEvent e) {
            updateValue();
        }
        
    };
    private DocumentHandler documentHandler;
    
    /** Creates new instance. */
    public JIntegerTextField() {
        initComponents();
        
        addFocusListener(new FocusListener() {
            public void focusGained(FocusEvent e) {
                selectAll();
            }
            
            public void focusLost(FocusEvent e) {
                updateText();
            }
            
        });
    }
    
    public void setDocument(Document newValue) {
        Document oldValue = getDocument();
        super.setDocument(newValue);
        
        if (documentHandler == null) {
            documentHandler = new DocumentHandler();
        }
        
        if (oldValue != null) {
            oldValue.removeDocumentListener(documentHandler);
        }
        if (newValue != null) {
            newValue.addDocumentListener(documentHandler);
        }
        updateValue();
    }
    
    protected void updateValue() {
        try {
            int newValue = Integer.decode(getText());
            if (newValue >= minimum && newValue <= maximum) {
                setValue(newValue);
            }
        } catch (NumberFormatException ex) {
            //ex.printStackTrace(); do nothing
        }
    }
    
    protected void updateText() {
        if (! isFocusOwner()) {
            setText(Integer.toString(value));
        }
    }
    
    public void setValue(int newValue) {
        int oldValue = value;
        value = newValue;
        
        if (newValue != oldValue) {
            firePropertyChange("value", oldValue, newValue);
            updateText();
        }
    }
    
    public int getValue() {
        return value;
    }
    
    public void setMinimum(int newValue) {
        int oldValue = value;
        minimum = newValue;
        firePropertyChange("minimum", oldValue, newValue);
    }
    
    public int getMinimum() {
        return minimum;
    }
    
    public void setMaximum(int newValue) {
        int oldValue = value;
        maximum = newValue;
        firePropertyChange("maximum", oldValue, newValue);
    }
    
    public int getMaximum() {
        return maximum;
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {

    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    
}
