/*
 * 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 com.k_int.AdminApp.widgets.RelatedComboBox;
import javax.swing.JComboBox;
import org.apache.commons.beanutils.BeanUtils;
import java.util.logging.*;

/**
 * Title:       RelatedComboModel
 * @version     $Id: RelatedComboModel.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 		Jul 27, 2004 10:01:05 AM
 * 
 * History:		
 * 				$Log: RelatedComboModel.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 RelatedComboModel implements RecordModelListener
{
    private static Logger cat = Logger.getLogger("com.k_int.AdminApp.gui.RecordView.RelatedComboModel");
       private String access_path;
       private RelatedComboBox source;
       private JComboBox target;
       private Object record = null;
       private AdminControllerComponent controller = null;



    public RelatedComboModel(String access_path, RelatedComboBox source, JComboBox target)
       {
         this.access_path = access_path;
         this.source = source;
         this.target = target;
       }
    /* (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.finest("JCombo component model set value "+value+", editable "+editable);
          if ( value != null )
            source.importValue(value);
    
            source.setEnabled(editable);
            target.setEnabled(editable);
  
        }
        catch ( Exception e )
        {
          e.printStackTrace();
        }

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

         if ( text != null )
         {
           try
           {
             //cat.finest("set "+access_path+" to "+text);
             BeanUtils.setProperty(record, access_path, text);
           }
           catch ( Exception e )
           {
             e.printStackTrace();
           }
         }
         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.finest("JComboModel:: setEditable "+is_editable);
       source.setEnabled(is_editable);
       target.setEnabled(is_editable);
     }

}
