001    /*
002     * $Id: MapComboBoxModel.java 3374 2009-07-01 14:36:43Z 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     */
021    package org.jdesktop.swingx.combobox;
022    
023    import java.awt.event.ActionEvent;
024    import java.util.ArrayList;
025    import java.util.HashMap;
026    import java.util.List;
027    import java.util.Map;
028    import 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     */
044    public 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 HashMap<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        public int getSize() {
090            return map_data.size();
091        }
092        
093        /**
094         * {@inheritDoc}
095         */
096        public void actionPerformed(ActionEvent evt) {
097            //kgs - this code might not be performant with large maps
098            if(evt.getActionCommand().equals(UPDATE)) {
099                //add new keys
100                Set<K> keys = map_data.keySet();
101                keys.removeAll(data);
102                data.addAll(keys);
103                
104                //remove dead keys
105                List<K> copy = new ArrayList<K>(data);
106                keys = map_data.keySet();
107                copy.removeAll(keys);
108                data.removeAll(copy);
109                
110                fireContentsChanged(this, 0, getSize() - 1);
111            }
112        }
113    
114        /**
115         * Selects an item from the model and returns that map value.
116         * 
117         * @param selectedItem
118         *                the item to select
119         * @return the value for the selected item
120         */
121        public V getValue(Object selectedItem) {
122            return map_data.get(selectedItem);
123        }
124        
125        /**
126         * Selects an item from the model and returns that map value.
127         * 
128         * @param selectedItem
129         *                selects the item at the specified index in this model
130         * @return the value for the item at the selected index
131         */
132        public V getValue(int selectedItem) {
133            return getValue(getElementAt(selectedItem));
134        }
135    }