/**
 * Title:
 * @version:    $Id: LabelComponentModel.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 org.apache.commons.beanutils.PropertyUtils;
import javax.swing.JLabel;
import com.k_int.AdminApp.gui.AdminControllerComponent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.*;



public class LabelComponentModel implements RecordModelListener
{
    private static Logger cat = Logger.getLogger("com.k_int.AdminApp.gui.RecordView.LabelComponentModel");;
  private String access_path;
  private JLabel control;
  private Object record = null;
  private static final String EMPTY_STRING="";
  private AdminControllerComponent controller = null;
  private SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");

  public LabelComponentModel(String access_path, JLabel control)
  {
    this.access_path = access_path;
    this.control = control;
  }

  public void recordChanged(Object record, boolean editable)
  {
    this.record = record;
 
   	try 
    {
      Object value = PropertyUtils.getProperty(record, access_path);
      cat.fine("Label component model set value "+value);
      if ( value != null )
      {
        if(value instanceof Date)
        {
            control.setText(df.format(value));
        }
        else
            control.setText(value.toString());
      }
      else
        control.setText(EMPTY_STRING);
  	}
  	catch ( Exception e )
  	{
    	cat.log(Level.SEVERE,"ERROR during recod changed",e);
    	throw new IllegalArgumentException("Invocation exception "+e.toString());
   	}
  }

  public void setEditable(boolean is_editable)
  {
    // Label fields are never editable
  }

  public void synchronizeViewToModel()
  {
    // Labels never push their value back into the model
  }

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