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