KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > tools > swing > SortingComboBoxModel


1 package org.apache.ojb.tools.swing;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 /**
20  *
21  * @author <a HREF="mailto:bfl@florianbruckner.com">Florian Bruckner</a>
22  * @version $Id: SortingComboBoxModel.java,v 1.1.2.1 2005/12/21 22:33:29 tomdz Exp $
23  */

24 public class SortingComboBoxModel extends javax.swing.AbstractListModel JavaDoc
25   implements javax.swing.MutableComboBoxModel JavaDoc
26 {
27
28   java.util.List JavaDoc aList;
29   Object JavaDoc selectedItem;
30
31   /** Creates a new instance of SortingComboBoxModel */
32   public SortingComboBoxModel ()
33   {
34     super();
35   }
36
37   public SortingComboBoxModel(java.util.List JavaDoc l)
38   {
39     super();
40     aList = l;
41     this.setSelectedItem(getElementAt(0));
42   }
43
44   public Object JavaDoc getElementAt (int param)
45   {
46 // System.out.println("getElementAt(" + param + ")=" + aList.get(param));
47
return aList.get(param);
48   }
49
50   public int getSize ()
51   {
52 // System.out.println("getSize()=" + aList.size());
53
return aList.size();
54   }
55
56
57   public void addElement (Object JavaDoc obj)
58   {
59 // System.out.println("addElement(" + obj + ")");
60
aList.add(obj);
61     java.util.Collections.sort(aList);
62     this.fireContentsChanged(this, 0, aList.size());
63   }
64
65   public Object JavaDoc getSelectedItem()
66   {
67 // System.out.println("getSelectedItem: " + selectedItem);
68
return this.selectedItem;
69   }
70
71   public void insertElementAt (Object JavaDoc obj, int param)
72   {
73 // System.out.println("insertElement(" + obj + ", " + param + ")");
74
aList.add(param, obj);
75    java.util.Collections.sort(aList);
76    this.fireContentsChanged(this, -1, aList.size());
77   }
78
79   public void removeElement (Object JavaDoc obj)
80   {
81     aList.remove(obj);
82   }
83
84   public void removeElementAt (int param)
85   {
86     aList.remove(param);
87   }
88
89   public void setSelectedItem (Object JavaDoc obj)
90   {
91     if ( (selectedItem != null && !selectedItem.equals(obj))
92         || (selectedItem == null && obj != null))
93     {
94       selectedItem = obj;
95       fireContentsChanged(this, -1, -1);
96     }
97 // System.out.println("setSelectedItem: " + selectedItem);
98
}
99
100   public int getIndexOf(Object JavaDoc o)
101   {
102 // System.out.println("getIndexOf(" + o + ") = " + aList.indexOf(o));
103
return aList.indexOf(o);
104   }
105 }
106
Popular Tags