001    /*
002     * $Id: AbstractAutoCompleteAdaptor.java,v 1.1 2006/03/19 18:28:46 Bierhance Exp $
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 javax.swing.text.JTextComponent;
024    
025    /**
026     * This is the interface that binds the mechanism for automatic completion to
027     * a data model, a selection model (e.g. those used by JList, JComboBox and JTable)
028     * and the JTextComponent for which the automatic completion should happen.
029     * It is used to search and select a matching item and to mark the completed text
030     * inside the JTextComponent. Using this interface the mechanism for automatic
031     * completion is independent from the underlying data and selection model.
032     *
033     * @see ComboBoxAdaptor
034     * @see ListAdaptor
035     *
036     * @author Thomas Bierhance
037     */
038    public abstract class AbstractAutoCompleteAdaptor {
039        /**
040         * Returns the currently selected item.
041         * @return the selected item
042         */
043        public abstract Object getSelectedItem();
044        
045        /**
046         * Sets the selected item.
047         * @param item the item that is to be selected
048         */
049        public abstract void setSelectedItem(Object item);
050    
051        /**
052         * Returns the number of items in the list.
053         * @return the number of items in the list
054         */
055        public abstract int getItemCount();
056        
057        /**
058         * Returns the item at a given index. It is supposed that <code>0&lt;=index&lt;<b>getItemCount()</b></code>.
059         * @param index the index of the item that is to be returned
060         * @return the item at the given <code>index</code>
061         */
062        public abstract Object getItem(int index);
063        
064        /**
065         * Returns true if the list contains the currently selected item.
066         * @return true if the list contains the currently selected item.
067         */
068        public boolean listContainsSelectedItem() {
069            Object selectedItem = getSelectedItem();
070            for (int i=0,n=getItemCount(); i<n; i++) {
071                if (getItem(i)==selectedItem) return true;
072            }
073            return false;
074        }
075        
076        /**
077         * Returns the text component that is being used for the automatic completion.
078         * @return the text component being used for the automatic completion
079         */
080        public abstract JTextComponent getTextComponent();
081        
082        /**
083         * Marks/selects the entire text that is displayed inside the text component.
084         */
085        public void markEntireText() {
086            markText(0);
087        }
088        
089        /**
090         * Marks/selects the text that is displayed inside the text component starting from the
091         * character with index <code>start</code>.
092         * @param start index of the first character that should be marked
093         */
094        public void markText(int start) {
095            getTextComponent().setCaretPosition(getTextComponent().getText().length());
096            getTextComponent().moveCaretPosition(start);
097        }
098    }