/**
 * Title:
 * @version:    $Id: TextComponentModel.java,v 1.1 2004/11/15 13:47:50 rob Exp $
 * Copyright:   Copyright (C) 2003 Ian Ibbotson
 * @author:     Ian Ibbotson
 * Company:
 * Description:
 */

package com.k_int.AdminApp.gui.RecordView;

import org.apache.commons.beanutils.BeanUtils;

import javax.swing.text.JTextComponent;
import java.util.logging.*;

import com.k_int.AdminApp.gui.AdminControllerComponent;


public class TextComponentModel implements RecordModelListener
{
    private static Logger cat = Logger.getLogger("com.k_int.AdminApp.gui.RecordView.TextComponentModel");
  private String access_path;
  private JTextComponent control;
  private Object record = null;
  private static final String EMPTY_STRING="";
  private AdminControllerComponent controller = null;

  public TextComponentModel(String access_path, JTextComponent control)
  {
    this.access_path = access_path;
    this.control = control;
  }

  public void recordChanged(Object record, boolean editable)
  {
    this.record = record;

    try
    {
      Object value = BeanUtils.getProperty(record, access_path);
      cat.finest("Text component model set value "+value+", "+access_path);
      if ( value != null )
        control.setText(value.toString());
      else
        control.setText(EMPTY_STRING);

      control.setEditable(editable);
    }
    catch ( Exception e )
    {
      e.printStackTrace();
    }
  }
  
  

  public void setEditable(boolean is_editable)
  {
    cat.finest("setEditable "+is_editable);
    control.setEditable(is_editable);
  }

  public void synchronizeViewToModel()
  {
    String text = control.getText();

    if ( text != null )
    {
      try
      {
        BeanUtils.setProperty(record, access_path, text);
      }
      catch ( Exception e )
      {
        e.printStackTrace();
      }
    }
    else
    {
      
    }
  }

  public void setController(AdminControllerComponent c)
  {
    this.controller = c;
  }

}
