package oracle.forms.fd;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.text.BadLocationException;
import oracle.forms.handler.IHandler;
import oracle.forms.properties.ID;
import oracle.forms.ui.CustomEvent;
import oracle.forms.ui.VBean;

/**
   * A TextArea to get more than 64k texts
   * @author Francois Degrelle
   * @version 1.1
   */

  public class TextArea  extends VBean implements FocusListener, KeyListener
  {
    public final static ID ADDTEXT      = ID.registerProperty("ADD_TEXT");  
    public final static ID VALUE        = ID.registerProperty("VALUE");    
    public final static ID SHOW         = ID.registerProperty("SHOW");        
    public final static ID CLEAR        = ID.registerProperty("CLEAR");
    public final static ID GETTEXT      = ID.registerProperty("GET_TEXT");    
    public final static ID GETLENGTH    = ID.registerProperty("GET_LENGTH");        
    public final static ID pLostFocus   = ID.registerProperty("BEAN_QUITTED");  
    
    private   IHandler  m_handler;  
    private   int iStart      = 0 ;
    private   int iChunk      = 8192 ;
    private   StringBuffer sb = new StringBuffer(); 
    protected JTextPane     jtp = new JTextPane();
    
    public TextArea()
    {
       super();
        try
        {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
          SwingUtilities.updateComponentTreeUI(this);
        }
        catch (Exception ex)
        {
          ex.printStackTrace();
        }        
       JScrollPane ps = new JScrollPane(jtp);
       ps.setBorder(null);
       add(ps);
       ps.setVisible(true);
    }

      
    public void init(IHandler handler)
    {
      m_handler = handler;
      super.init(handler);
      addFocusListener(this);
      jtp.addFocusListener(this);
      jtp.addKeyListener(this);
    }      
    
    
    public boolean setProperty(ID property, Object value)
    {
      // 
      // add text to the TextArea
      //
      if (property == ADDTEXT)
      {
        sb.append(value.toString()) ;
        printMemory();
        return true;
      }
      //
      // display the whole text
      //
      else if(property == SHOW)
      { 
        jtp.setText(sb.toString());
        jtp.setCaretPosition(0);
        sb = new StringBuffer(); 
        System.gc();
        printMemory();
        return true ;
      }
      //
      // clear the TextArea
      //
      else if(property == CLEAR) {
        jtp.setText("");
        return true ;
      }
      else
      {
        return super.setProperty(property, value);
      }
    }
    
    /*-----------------------------------*
     * Get the result string from Forms  *
     *-----------------------------------*/
    public Object getProperty(ID pId)
    {
      //
      // returns the text length
      //
      if (pId == GETLENGTH)
      {
        return "" + jtp.getText().length();
      }
      //
      // returns the chunks
      //
      else if (pId == GETTEXT) {
          String s = "" ;
          int iLen = jtp.getText().length() ;
          while (iStart < iLen)
          {
            try{
              if(iStart+iChunk <= iLen) s = jtp.getText(iStart,iChunk);
              else s = jtp.getText(iStart,iLen-iStart);
              iStart += iChunk ;
              return s ;
            }
            catch (BadLocationException ble) { ble.printStackTrace(); return ""; }
          }
          iStart = 0 ;
          return "" ;
      }
      else
      {
        return super.getProperty(pId);
      }
    } // getProperty()
   
   
     /*--------------------------*
      * handle the focus events  *
      *--------------------------*/
     public void focusGained(FocusEvent e)
      {
          if (e.getComponent() == this)
          {
              // put the focus on the component
              jtp.requestFocus();
          }
     
          try
          {
              m_handler.setProperty(FOCUS_EVENT, e);
          }
          catch ( Exception ex )
          {
            ;
          }
      }
     
     public void focusLost(FocusEvent e)
      {     
        CustomEvent ce = new CustomEvent(m_handler, pLostFocus);
        dispatchCustomEvent(ce);        
      }

     /*--------------------------*
      * Handle the Key listener  *
      *--------------------------*/
     public void keyPressed(KeyEvent e)
     {
         /*
         ** Allows TAB key to exit the item
         ** and continue the standard Forms navigation
         */
         if ( (e.getKeyCode() == KeyEvent.VK_TAB) )
         {
             try
             {
                 m_handler.setProperty(KEY_EVENT, e);
             }
             catch ( Exception ex )
             {
             }
         }
     }
     
     
     public void keyTyped(KeyEvent e)
     {
     }
     
     public void keyReleased(KeyEvent e)
     {
     }
   
    
    // utility to output the memory available
    private void printMemory() {
        System.out.println("Java memory in use = " 
        + (Runtime.getRuntime().totalMemory() 
        - Runtime.getRuntime().freeMemory()));
    }
    
  }