AbstractComponentAdaptor.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:4k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: AbstractComponentAdaptor.java,v 1.3 2005/10/10 18:01:34 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx.autocomplete;
  22. import javax.swing.text.JTextComponent;
  23. /**
  24.  * This is the interface that binds the mechanism for automtic completion to
  25.  * a data model, a selection model (e.g. those used by JList, JComboBox and JTable)
  26.  * and the JTextComponent for which the automatic completion should happen.
  27.  * It is used to search and select a matching item and to mark the completed text
  28.  * inside the JTextComponent. Using this interface the mechanism for automatic
  29.  * completion is independent from the underlying data and selection model.
  30.  *
  31.  * @see ComboBoxAdaptor
  32.  * @see ListAdaptor
  33.  *
  34.  * @author Thomas Bierhance
  35.  */
  36. public abstract class AbstractComponentAdaptor {
  37.     /**
  38.      * Returns the currently selected item.
  39.      * @return the selected item
  40.      */
  41.     public abstract Object getSelectedItem();
  42.     
  43.     /**
  44.      * Sets the selected item.
  45.      * @param item the item that is to be selected
  46.      */
  47.     public abstract void setSelectedItem(Object item);
  48.     /**
  49.      * Returns the number of items in the list.
  50.      * @return the number of items in the list
  51.      */
  52.     public abstract int getItemCount();
  53.     
  54.     /**
  55.      * Returns the item at a given index. It is supposed that <code>0&lt;=index&lt;<b>getItemCount()</b></code>.
  56.      * @param index the index of the item that is to be returned
  57.      * @return the item at the given <code>index</code>
  58.      */
  59.     public abstract Object getItem(int index);
  60.     
  61.     /**
  62.      * Returns true if the list contains the currently selected item.
  63.      * @return true if the list contains the currently selected item.
  64.      */
  65.     public boolean listContainsSelectedItem() {
  66.         Object selectedItem = getSelectedItem();
  67.         for (int i=0,n=getItemCount(); i<n; i++) {
  68.             if (getItem(i)==selectedItem) return true;
  69.         }
  70.         return false;
  71.     }
  72.     
  73.     /**
  74.      * Returns the text component that is being used for the automatic completion.
  75.      * @return the text component being used for the automatic completion
  76.      */
  77.     public abstract JTextComponent getTextComponent();
  78.     
  79.     /**
  80.      * Marks/selects the entire text that is displayed inside the text component.
  81.      */
  82.     public void markEntireText() {
  83.         markText(0);
  84.     }
  85.     
  86.     /**
  87.      * Marks/selects the text that is displayed inside the text component starting from the
  88.      * character with index <code>start</code>.
  89.      * @param start index of the first character that should be marked
  90.      */
  91.     public void markText(int start) {
  92.         getTextComponent().setCaretPosition(getTextComponent().getText().length());
  93.         getTextComponent().moveCaretPosition(start);
  94.     }
  95. }