001/* 002 * $Id: ListAdaptor.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 javax.swing.JList; 024import javax.swing.event.ListSelectionListener; 025import javax.swing.text.JTextComponent; 026 027/** 028 * An implementation of the AbstractAutoCompleteAdaptor that is suitable for a 029 * JList in conjunction with a JTextComponent. 030 * 031 * @author Thomas Bierhance 032 */ 033public class ListAdaptor extends AbstractAutoCompleteAdaptor implements ListSelectionListener { 034 035 /** the list containing the items */ 036 JList list; 037 /** the text component that is used for automatic completion*/ 038 JTextComponent textComponent; 039 /** the converter used to transform items to strings */ 040 ObjectToStringConverter stringConverter; 041 042 /** 043 * Creates a new JListAdaptor for the given list and text component. 044 * @param list the list that contains the items that are used for automatic 045 * completion 046 * @param textComponent the text component that will be used automatic 047 * completion 048 */ 049 public ListAdaptor(JList list, JTextComponent textComponent) { 050 this(list, textComponent, ObjectToStringConverter.DEFAULT_IMPLEMENTATION); 051 } 052 053 /** 054 * Creates a new JListAdaptor for the given list and text component. 055 * @param list the list that contains the items that are used for automatic 056 * completion 057 * @param textComponent the text component that will be used automatic 058 * completion 059 * @param stringConverter the converter used to transform items to strings 060 */ 061 public ListAdaptor(JList list, JTextComponent textComponent, ObjectToStringConverter stringConverter) { 062 this.list = list; 063 this.textComponent = textComponent; 064 this.stringConverter = stringConverter; 065 // when a new item is selected set and mark the text 066 list.addListSelectionListener(this); 067 } 068 069 /** 070 * Implementation side effect - do not invoke. 071 * @param listSelectionEvent - 072 */ 073 // ListSelectionListener (listening to list) 074 @Override 075 public void valueChanged(javax.swing.event.ListSelectionEvent listSelectionEvent) { 076 // set the text to the currently selected item 077 getTextComponent().setText(stringConverter.getPreferredStringForItem(list.getSelectedValue())); 078 // mark the entire text 079 markEntireText(); 080 } 081 082 @Override 083 public Object getSelectedItem() { 084 return list.getSelectedValue(); 085 } 086 087 @Override 088 public int getItemCount() { 089 return list.getModel().getSize(); 090 } 091 092 @Override 093 public Object getItem(int index) { 094 return list.getModel().getElementAt(index); 095 } 096 097 @Override 098 public void setSelectedItem(Object item) { 099 list.setSelectedValue(item, true); 100 } 101 102 @Override 103 public JTextComponent getTextComponent() { 104 return textComponent; 105 } 106}