View Javadoc

1   /*
2    * $Id: SuggestionComboBoxModel.java 178 2010-10-31 18:01:20Z roland $
3    * Copyright (C) 2003 Roland Krueger
4    * Created on Oct 15, 2003
5    * 
6    * Author: Roland Krueger (www.rolandkrueger.info)
7    *
8    * This file is part of RoKlib.
9    *
10   * This library is free software; you can redistribute it and/or
11   * modify it under the terms of the GNU Lesser General Public License
12   * as published by the Free Software Foundation; either version 2.1 of
13   * the License, or (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful, but
16   * WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18   * Lesser General Public License for more details.
19   *
20   * You should have received a copy of the GNU Lesser General Public
21   * License along with this library; if not, write to the Free Software
22   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23   * USA
24   */
25  package info.rolandkrueger.roklib.ui.swing.rapidsuggest;
26  
27  import info.rolandkrueger.roklib.util.TernarySearchTreeMap;
28  
29  import java.util.Collection;
30  import java.util.Iterator;
31  import java.util.Map;
32  
33  import javax.swing.DefaultComboBoxModel;
34  
35  /*
36   * TODO: - case sensitive/case insensitive konfigurierbar machen. Dabei
37   * contains()- Methode ueberarbeiten!
38   */
39  
40  /**
41   * @author Roland Krueger (rkrueger@rumms.uni-mannheim.de)
42   * @version CVS $Id: SuggestionComboBoxModel.java 128 2010-03-05 06:45:32Z
43   *          roland $
44   */
45  public class SuggestionComboBoxModel extends DefaultComboBoxModel
46  {
47    private TernarySearchTreeMap tstMap;
48    private int selectedItemIndex = - 1;
49  
50    public SuggestionComboBoxModel (Collection items)
51    {
52      tstMap = new TernarySearchTreeMap ();
53      for (Iterator it = items.iterator (); it.hasNext ();)
54      {
55        Object e = it.next ();
56        tstMap.put (e.toString ().toLowerCase (), e);
57      }
58    }
59  
60    public SuggestionComboBoxModel (Object[] items)
61    {
62      tstMap = new TernarySearchTreeMap ();
63      for (int i = 0; i < items.length; i++)
64      {
65        tstMap.put (items[i].toString ().toLowerCase (), items[i]);
66      }
67    }
68  
69    public SuggestionComboBoxModel ()
70    {
71      tstMap = new TernarySearchTreeMap ();
72    }
73  
74    public SuggestionComboBoxModel (TernarySearchTreeMap map)
75    {
76      tstMap = new TernarySearchTreeMap ();
77      Map.Entry entry;
78      for (Iterator it = map.entrySet ().iterator (); it.hasNext ();)
79      {
80        entry = (Map.Entry) it.next ();
81        tstMap.put (entry.getKey ().toString ().toLowerCase (), entry.getValue ());
82      }
83    }
84  
85    public String getSuggestion (String prefix)
86    {
87      // System.out.println("getSuggestion");
88      String match;
89      Iterator it = tstMap.getPrefixSubtreeIterator (prefix.toLowerCase ());
90      if ((it != null) && (it.hasNext ()))
91        match = ((Map.Entry) it.next ()).getValue ().toString ();
92      else
93        match = null;
94      setSelectedItem (match);
95      return match;
96      // if (prefix.equals("")) return null;
97      // SortedSet tailSet = treeSet.tailSet(prefix.toUpperCase());
98      // //System.out.println(tailSet);
99      // if (tailSet.isEmpty()) return null;
100     // String first = tailSet.first().toString();
101     // for (Iterator it = tailSet.iterator(); it.hasNext();)
102     // {
103     // String element = (String) it.next();
104     // if (element.toLowerCase().startsWith(prefix.toLowerCase()))
105     // {
106     // setSelectedItem(element);
107     // return element;
108     // }
109     // }
110     // return null;
111     // for (Iterator it = treeSet.iterator(); it.hasNext();)
112     // {
113     // String element = (String) it.next();
114     // if (element.toLowerCase().startsWith(prefix.toLowerCase()))
115     // {
116     // setSelectedItem(element);
117     // return element;
118     // }
119     // }
120     // return null;
121   }
122 
123   public boolean contains (Object o, boolean caseSensitive)
124   {
125     if (caseSensitive)
126       return tstMap.containsKey (o);
127     else
128     {
129       Map.Entry entry;
130       for (Iterator it = tstMap.entrySet ().iterator (); it.hasNext ();)
131       {
132         entry = (Map.Entry) it.next ();
133         if (o.toString ().equalsIgnoreCase (entry.getKey ().toString ())) return true;
134       }
135     }
136     return false;
137   }
138 
139   public void setSelectedItem (Object anObject)
140   {
141     if (anObject == null)
142     {
143       selectedItemIndex = - 1;
144       return;
145     }
146     selectedItemIndex = tstMap.indexOf (anObject.toString ().toLowerCase ());
147   }
148 
149   public Object getSelectedItem ()
150   {
151     if (selectedItemIndex == - 1)
152       return null;
153     else
154       return getElementAt (selectedItemIndex);
155   }
156 
157   /*
158    * (non-Javadoc)
159    * 
160    * @see javax.swing.MutableComboBoxModel#removeElementAt(int)
161    */
162   public void removeElementAt (int index)
163   {
164     // TODO Auto-generated method stub
165 
166   }
167 
168   /*
169    * (non-Javadoc)
170    * 
171    * @see javax.swing.MutableComboBoxModel#addElement(java.lang.Object)
172    */
173   public void addElement (Object obj)
174   {
175     tstMap.put (obj.toString ().toLowerCase (), obj);
176   }
177 
178   /*
179    * (non-Javadoc)
180    * 
181    * @see javax.swing.MutableComboBoxModel#removeElement(java.lang.Object)
182    */
183   public void removeElement (Object obj)
184   {
185     tstMap.remove (obj);
186   }
187 
188   /*
189    * (non-Javadoc)
190    * 
191    * @see javax.swing.MutableComboBoxModel#insertElementAt(java.lang.Object,
192    * int)
193    */
194   public void insertElementAt (Object obj, int index)
195   {
196     addElement (obj);
197   }
198 
199   /*
200    * (non-Javadoc)
201    * 
202    * @see javax.swing.ListModel#getSize()
203    */
204   public int getSize ()
205   {
206     return tstMap.size ();
207   }
208 
209   public Object getElementAt (int index)
210   {
211     // Iterator it = tstMap.iterator();
212     // for (int i = 0; i < index; i++)
213     // {
214     // it.next();
215     // }
216     // return it.next();
217     return tstMap.getValueAt (index);
218   }
219 
220   // private int indexOf(Object anObject)
221   // {
222   // int i = 0;
223   // for (Iterator it = tstMap.iterator(); it.hasNext();)
224   // {
225   // if (anObject.toString().equals((String) it.next())) return i;
226   // i++;
227   // }
228   // return -1;
229   // }
230 }