/*
 * 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.gui.RecordView;

import com.k_int.AdminApp.gui.AdminControllerComponent;
import javax.swing.JComboBox;
import org.apache.commons.beanutils.BeanUtils;
import java.util.logging.*;

/**
 * Title:       JComboModel
 * @version     $Id: JComboModel.java,v 1.1 2004/11/15 13:47:50 rob Exp $
 * Copyright:   Copyright (C) 1999 - 2004,Knowledge Integration Ltd.
 * @author      Rob Tice (rob.tice@k-int.com)
 * Company:     Knowledge Integration Ltd.
 * Description: 
 * 
 * Created 		Jun 29, 2004 10:56:06 AM
 * 
 * History:		
 * 				$Log: JComboModel.java,v $
 * 				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 JComboModel implements RecordModelListener
{
    private static Logger cat = Logger.getLogger("com.k_int.AdminApp.gui.RecordView.JComboModel");
    private String access_path;
    private JComboBox control;
    private Object record = null;
    private AdminControllerComponent controller = null;
    
    public JComboModel(String access_path, JComboBox control)
    {
      this.access_path = access_path;
      this.control = control;
    }


    /* (non-Javadoc)
     * @see com.k_int.AdminApp.gui.RecordView.RecordModelListener#recordChanged(java.lang.Object, boolean)
     */
    public void recordChanged(Object record, boolean editable)
    {
        this.record = record;

        try
        {
          Object value = BeanUtils.getProperty(record, access_path);
          cat.fine("JCombo component model set value "+value+", editable "+editable);
          if ( value != null )
            control.setSelectedItem(value);
            
            control.setEnabled(editable);
          
        }
        catch ( Exception e )
        {
            cat.log(Level.SEVERE,"Error changing record",e);
        }

    }

    /* (non-Javadoc)
     * @see com.k_int.AdminApp.gui.RecordView.RecordModelListener#synchronizeViewToModel()
     */
    public void synchronizeViewToModel()
    {
        String text = (String) control.getSelectedItem();

        if ( text != null )
        {
          try
          {
            BeanUtils.setProperty(record, access_path, text);
          }
          catch ( Exception e )
          {
            cat.log(Level.SEVERE,"Error",e);
          }
        }
        else
        {
          cat.fine("TExt is null");
        }

    }

    /* (non-Javadoc)
     * @see com.k_int.AdminApp.gui.RecordView.RecordModelListener#setController(com.k_int.AdminApp.gui.AdminControllerComponent)
     */
    public void setController(AdminControllerComponent c)
    {
        this.controller = c;
    }
   
    public void setEditable(boolean is_editable)
    {
      cat.fine("JComboModel:: setEditable "+is_editable);
      control.setEnabled(is_editable);
    }

}
