01: package org.apache.ojb.tools.swing;
02:
03: /* Copyright 2002-2005 The Apache Software Foundation
04: *
05: * Licensed under the Apache License, Version 2.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * 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: * @author <a href="mailto:bfl@florianbruckner.com">Florian Bruckner</a>
21: * @version $Id: SortingComboBoxModel.java,v 1.1.2.1 2005/12/21 22:33:29 tomdz Exp $
22: */
23: public class SortingComboBoxModel extends javax.swing.AbstractListModel
24: implements javax.swing.MutableComboBoxModel {
25:
26: java.util.List aList;
27: Object selectedItem;
28:
29: /** Creates a new instance of SortingComboBoxModel */
30: public SortingComboBoxModel() {
31: super ();
32: }
33:
34: public SortingComboBoxModel(java.util.List l) {
35: super ();
36: aList = l;
37: this .setSelectedItem(getElementAt(0));
38: }
39:
40: public Object getElementAt(int param) {
41: // System.out.println("getElementAt(" + param + ")=" + aList.get(param));
42: return aList.get(param);
43: }
44:
45: public int getSize() {
46: // System.out.println("getSize()=" + aList.size());
47: return aList.size();
48: }
49:
50: public void addElement(Object obj) {
51: // System.out.println("addElement(" + obj + ")");
52: aList.add(obj);
53: java.util.Collections.sort(aList);
54: this .fireContentsChanged(this , 0, aList.size());
55: }
56:
57: public Object getSelectedItem() {
58: // System.out.println("getSelectedItem: " + selectedItem);
59: return this .selectedItem;
60: }
61:
62: public void insertElementAt(Object obj, int param) {
63: // System.out.println("insertElement(" + obj + ", " + param + ")");
64: aList.add(param, obj);
65: java.util.Collections.sort(aList);
66: this .fireContentsChanged(this , -1, aList.size());
67: }
68:
69: public void removeElement(Object obj) {
70: aList.remove(obj);
71: }
72:
73: public void removeElementAt(int param) {
74: aList.remove(param);
75: }
76:
77: public void setSelectedItem(Object obj) {
78: if ((selectedItem != null && !selectedItem.equals(obj))
79: || (selectedItem == null && obj != null)) {
80: selectedItem = obj;
81: fireContentsChanged(this , -1, -1);
82: }
83: // System.out.println("setSelectedItem: " + selectedItem);
84: }
85:
86: public int getIndexOf(Object o) {
87: // System.out.println("getIndexOf(" + o + ") = " + aList.indexOf(o));
88: return aList.indexOf(o);
89: }
90: }
|