001/*
002 * $Id: MapComboBoxModel.java 3751 2010-08-08 04:07:44Z kschaefe $
003 *
004 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005 * Santa Clara, California 95054, U.S.A. All rights reserved.
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 * 
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015 * Lesser General Public License for more details.
016 * 
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this library; if not, write to the Free Software
019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020 */
021package org.jdesktop.swingx.combobox;
022
023import java.awt.event.ActionEvent;
024import java.util.ArrayList;
025import java.util.LinkedHashMap;
026import java.util.List;
027import java.util.Map;
028import java.util.Set;
029
030/**
031 * A {@code ComboBoxModel} for {@code Map}s. The model will always present a {@code Map}
032 * consistently, once it is instantiated. However, unless the {@code Map} is ordered, as a
033 * {@code java.util.TreeMap} is, the model is not guaranteed to present the maps in a consistent
034 * order between instantiations.
035 * 
036 * @author jm158417
037 * @author Karl George Schaefer
038 * 
039 * @param <K>
040 *                the type of keys maintained by the map backing this model
041 * @param <V>
042 *                the type of mapped values
043 */
044public class MapComboBoxModel<K, V> extends ListComboBoxModel<K> {
045
046    /**
047     * The map backing this model.
048     */
049    protected Map<K, V> map_data;
050    
051    /**
052     * Creates an empty model.
053     */
054    public MapComboBoxModel() {
055        this(new LinkedHashMap<K, V>());
056    }
057    
058    /**
059     * Creates a model backed by the specified map.
060     * 
061     * @param map
062     *                the map backing this model
063     */
064    public MapComboBoxModel(Map<K, V> map) {
065        super(buildIndex(map));
066        
067        this.map_data = map;
068    }
069    
070    /**
071     * Builds an index for this model. This method ensures that the map is always presented in a
072     * consistent order.
073     * <p>
074     * This method is called by the constructor and the {@code List} is passed to {@code super}.
075     * 
076     * @param <E>
077     *                the type of keys for the map
078     * @param map
079     *                the map to build an index for
080     * @return a list containing the map keys
081     */
082    private static <E> List<E> buildIndex(Map<E, ?> map) {
083        return new ArrayList<E>(map.keySet());
084    }
085    
086    /**
087     * {@inheritDoc}
088     */
089    @Override
090    public int getSize() {
091        return map_data.size();
092    }
093    
094    /**
095     * {@inheritDoc}
096     */
097    @Override
098    public void actionPerformed(ActionEvent evt) {
099        //kgs - this code might not be performant with large maps
100        if(evt.getActionCommand().equals(UPDATE)) {
101            //add new keys
102            Set<K> keys = map_data.keySet();
103            keys.removeAll(data);
104            data.addAll(keys);
105            
106            //remove dead keys
107            List<K> copy = new ArrayList<K>(data);
108            keys = map_data.keySet();
109            copy.removeAll(keys);
110            data.removeAll(copy);
111            
112            fireContentsChanged(this, 0, getSize() - 1);
113        }
114    }
115
116    /**
117     * Selects an item from the model and returns that map value.
118     * 
119     * @param selectedItem
120     *                the item to select
121     * @return the value for the selected item
122     */
123    public V getValue(Object selectedItem) {
124        return map_data.get(selectedItem);
125    }
126    
127    /**
128     * Selects an item from the model and returns that map value.
129     * 
130     * @param selectedItem
131     *                selects the item at the specified index in this model
132     * @return the value for the item at the selected index
133     */
134    public V getValue(int selectedItem) {
135        return getValue(getElementAt(selectedItem));
136    }
137}