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

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.util.LinkedHashMap;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.util.Vector;
import java.util.Iterator;
import java.util.logging.*;

/**
 * Title:       RelatedComboBox
 * @version     $Id: RelatedComboBox.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 26, 2004 8:24:32 PM
 * 
 * History:		
 * 				$Log: RelatedComboBox.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 RelatedComboBox extends JComboBox implements ItemListener
{
    private static Logger cat = Logger.getLogger("com.k_int.AdminApp.widgets.RelatedComboBox");
    private LinkedHashMap array_map = new LinkedHashMap();
    private Vector keys = new Vector();
    private JComboBox target=null;
   // public int length;
    private boolean importing;
    
    public void addMappings(LinkedHashMap array_map)
    {
        this.array_map = array_map;
        keys = new Vector(array_map.keySet());
        cat.fine("Keys are "+keys);
        initializeSourceAndTarget();
    }
    
    
    public void setTargetComboBox(JComboBox target)
    {
        target.addItemListener(this);
        this.addItemListener(this);
        this.target = target;       
        initializeSourceAndTarget();        
    }
    
    private void initializeSourceAndTarget()
    {
        if(target==null || keys.size()==0)
            return;
            
        cat.fine("Initializing source and target");
        this.setModel(new DefaultComboBoxModel(keys));
        Object[] target_list = (Object []) array_map.get(this.getSelectedItem());
        target.setModel(new DefaultComboBoxModel(target_list));        
    }
    
    
    public void itemStateChanged(ItemEvent e)
    {
        cat.fine("Item changed "+e.getStateChange());
        if(e.getStateChange()!=1)
        {;}
        else if(e.getSource()==this && importing==false)
        {
            cat.finest("Source");
            cat.finest("New value is "+this.getSelectedItem());
            target.setModel(new DefaultComboBoxModel((Object[]) array_map.get(this.getSelectedItem())));
        }        
        else if (e.getSource()==target)
            cat.finest("target");
    }
    
    
    public void importValue(Object item)
    {
        cat.fine("Set selected item "+item.toString());
        importing=true;
        Iterator i = array_map.keySet().iterator();
        
        while(i.hasNext())
        {           
            String key = (String) i.next();
            Object[] object_array = (Object []) array_map.get(key);
            boolean matches=false;
           
            for(int j=0;j<object_array.length;j++)
            {
                
                Object array_item = object_array[j];
                
                if(array_item.toString().equals(item.toString()))
                {
                    cat.finest("Matched "+item.toString());
                    matches=true;
                    target.setModel(new DefaultComboBoxModel(object_array)); 
                    target.setSelectedItem(array_item);
                    cat.finest("Selected item is "+target.getSelectedItem());
                    this.setSelectedItem(key);
                    break;
                }
                if(matches)
                    break;
                    
            }
        }
        importing=false;
        
    }
}
