/**
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 *
 * Copyright (C)
 * <a href="http://www.inab.org">Spanish National Institute of Bioinformatics (INB)</a>
 * <a href="http://www.bsc.es">Barcelona Supercomputing Center (BSC)</a>
 * <a href="http://inb.bsc.es">Computational Node 6</a>
 */

package org.inb.biomoby.central.gui;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JList;
import org.inb.biomoby.central.MobyCentral;
import org.inb.biomoby.central.model.MobyCentralModel;
import org.inb.biomoby.central.gui.model.RegistryComboBoxModel;

/**
 * JComboBox based on BioMoby MobyCentralModel
 * @author Dmitry Repchevsky
 */

public class RegistryComboBox extends JComboBox implements ActionListener
{
    public RegistryComboBox()
    {
        super(new RegistryComboBoxModel());
        
        setRenderer(new RegistryComboBoxRenderer());
        
        addActionListener(this);
    }
    
    @Override
    public void actionPerformed(ActionEvent e)
    {
        MobyCentral central = (MobyCentral)getSelectedItem();
        
        MobyCentralModel.instance().setSelectedCentral(central);
    }
    
    public static class RegistryComboBoxRenderer extends DefaultListCellRenderer
    {
        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean hasFocus)
        {
            if (value instanceof MobyCentral)
            {
                value = ((MobyCentral)value).getEndpointURL();
            }
            
            return super.getListCellRendererComponent(list, value, index, isSelected, hasFocus);
        }
    }
}
