/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * published by the Free Software Foundation; either version 2.1 of
 * the license, or (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite
 * 330, Boston, MA  02111-1307, USA.
 * 
 */
 
package com.k_int.AdminApp.widgets;

import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.KeyboardFocusManager;

import javax.swing.event.CaretEvent;

import javax.swing.JTextArea;
import javax.swing.JTextField;

import java.util.Set;

import java.awt.Dimension;

/**
 * Title:       BorderedTextArea
 * @version     $Id: BorderedTextArea.java,v 1.3 2005/08/05 16:49:10 rob Exp $
 * Copyright:   Copyright (C) 1999 - 2004,Knowledge Integration Ltd.
 * @author      Rob Tice (rob.tice@k-int.com)
 * Company:     Knowledge Integration Ltd.
 * Description: 
 * 
 * Created 		Jul 26, 2004 12:46:58 PM
 * 
 * History:		
 * 				$Log: BorderedTextArea.java,v $
 * 				Revision 1.3  2005/08/05 16:49:10  rob
 * 				*** empty log message ***
 * 				
 * 				Revision 1.2  2005/01/12 14:06:15  rob
 * 				*** empty log message ***
 * 				
 * 				Revision 1.1  2004/11/15 13:47:50  rob
 * 				*** empty log message ***
 * 				
 * 				Revision 1.1  2004/08/05 18:42:29  rob
 * 				*** empty log message ***
 * 				
 *
 */
public class BorderedTextArea extends JTextArea implements javax.swing.event.CaretListener
{
    private boolean initialized;
    private static JTextField text_field = new JTextField();
    
    private int real_row_counter=0;
    private int last_y_position=0;
   
    public BorderedTextArea(int rows)
	{           
		super();
	    setFont(text_field.getFont());
	    setBorder(text_field.getBorder());
	    setRows(rows);
	    this.addCaretListener(this);
	    this.setPreferredSize(new Dimension(this.getWidth(), this.getHeight()));
	    this.setLineWrap(true);
	    this.setWrapStyleWord(true);
	    Set keys = text_field.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
	    this.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,keys);
	}
    
    
   
    
    public void caretUpdate(javax.swing.event.CaretEvent e)
    {  
        //System.out.println("caret update "+this.getCaret().getMagicCaretPosition());
        //System.out.println("Width = "+this.getWidth());
        if(this.getCaret().getMagicCaretPosition()!=null)
        {
            //System.out.println("Magic caret update "+this.getCaret().getMagicCaretPosition());
            
            int current_y_position = this.getCaret().getMagicCaretPosition().y;
            
            //System.out.println("Current y pos = "+current_y_position);
            //System.out.println("Last y pos = "+last_y_position);
            if (current_y_position > last_y_position)
            {
                //System.out.println("Current y > last y");
                last_y_position = current_y_position;
                if(real_row_counter==-1)
                {
                    //System.out.println("Real row counter originally -1");
                    real_row_counter = current_y_position/getRowHeight();
                    //real_row_counter=1;
                }
                real_row_counter++;
                
                //System.out.println("Real rows "+real_row_counter);
                //System.out.println("Get rows "+getRows());
                
                if(real_row_counter>getRows())
                {
                    //System.out.println("Real row counter > current_rows");
                    setRows(getRows()+1);
                    this.setPreferredSize(new Dimension(this.getWidth()-this.getInsets().left-this.getInsets().right, this.getHeight()+getRowHeight()));
                }
            }
        }
        
        if(getLineCount()>getRows())
        {
            this.setRows(getLineCount());
            this.setPreferredSize(new Dimension(this.getWidth()-this.getInsets().left-this.getInsets().right, this.getHeight()+getRowHeight()));
        } 
    }
    
    public void setEnabled(boolean enabled)
    {
        super.setEnabled(enabled);       
        text_field.setEnabled(enabled);
        this.setOpaque(enabled);
        this.setBackground(text_field.getBackground());
        this.setForeground(text_field.getForeground()); 
        //System.out.println("set enabled");
    } 
    
    
    public void paint(java.awt.Graphics g)
    {
        super.paint(g);
        if(initialized==false)
        {
            //int row_height = getRowHeight();
            int column_width = getColumnWidth();
            
         //   System.out.println("Width = "+this.getWidth());
         //   System.out.println("Column width "+column_width);
            
            
            
            int len = this.getText().length();
           
            
            int rows = (len/(this.getWidth()/column_width))-1;
       //     System.out.println("Calculated rows is "+rows);
            
            real_row_counter = rows-1;
            
            //if(real_row_counter==-1)
             //   real_row_counter=1;
            
            initialized=true;
            
            if(rows<4)
                return;
                //rows=4;
                //return;
            
            setRows(rows);
            this.setPreferredSize(new Dimension(this.getWidth()-this.getInsets().left-this.getInsets().right, this.getHeight()+getRowHeight()));
          
            
            
        }
    }
    /**
    public void setText(String text)
    {
        // this needs to keep track of new lines
        
        
        // bit of a hack to deal with expanding text boxes - apologies..
     
        super.setText(text);
        
        
      //  this.setCaretPosition(this.getCaretPosition());
       // this.getCaret().setVisible(true);
        
       // this.getCaret().setVisible(false);
        
        //this.getCaret().setMagicCaretPosition();
      //  System.out.println("Set text "+text);
        
        
        
        
      //  System.out.println("set text caret update "+this.getCaretPosition());
     //   System.out.println("set text Magic caret update "+this.getCaret().getMagicCaretPosition().y);
        
        
        
        
        
        
        
        
        
        
        
       if(getLineCount()>=getRows())
        {
            this.setRows(getLineCount());
            this.setPreferredSize(new Dimension(this.getWidth(), this.getHeight()+getRowHeight()));
        } 
        
        int max_caret_pos = getCaretPos();
        
        int pos = 0;
        
        int row_counter;
        while(pos<max_caret_pos)
        {
            setCaret(pos);
            this.getCaret().set
            
            int y_axis = 
            
        }
        
        setCaretPosition(pos)
        while()
        System.out.println("LINes "+getLineCount());
        System.out.println("Rows "+getRows());
        System.out.println("Columns "+getColumns());
        
        System.out.println("Column width "+this.getColumnWidth());
        System.out.println("Text length is "+text.length());
        
        System.out.println("WIdht is "+this.getWidth());
      
        
        
    }
 **/
    
    
}
