001    /*
002     * $Id: TextComponentAdaptor.java 2370 2007-11-02 14:26:47Z 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.autocomplete;
022    
023    import java.util.List;
024    import javax.swing.text.JTextComponent;
025    
026    /**
027     * An implementation of the AbstractAutoCompleteAdaptor that is suitable for a
028     * JTextComponent.
029     * 
030     * @author Thomas Bierhance
031     */
032    public class TextComponentAdaptor extends AbstractAutoCompleteAdaptor {
033        
034        /** a <tt>List</tt> containing the strings to be used for automatic
035         * completion */
036        List<?> items;
037        /** the text component that is used for automatic completion*/
038        JTextComponent textComponent;
039        /** the item that is currently selected */
040        Object selectedItem;
041        
042        /**
043         * Creates a new <tt>TextComponentAdaptor</tt> for the given list and text
044         * component.
045         * 
046         * @param items a <tt>List</tt> that contains the items that are used for
047         * automatic completion
048         * @param textComponent the text component that will be used automatic
049         * completion
050         */
051        public TextComponentAdaptor(JTextComponent textComponent, List<?> items) {
052            this.items = items;
053            this.textComponent = textComponent;
054        }
055        
056        public Object getSelectedItem() {
057            return selectedItem;
058        }
059        
060        public int getItemCount() {
061            return items.size();
062        }
063        
064        public Object getItem(int index) {
065            return items.get(index);
066        }
067        
068        public void setSelectedItem(Object item) {
069            selectedItem = item;
070        }
071        
072        public JTextComponent getTextComponent() {
073            return textComponent;
074        }
075    }