KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > dbschema > jdbcimpl > wizard > SortedListModel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.dbschema.jdbcimpl.wizard;
21
22 import java.util.Comparator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Collections JavaDoc;
27
28 import javax.swing.AbstractListModel JavaDoc;
29
30
31 /**
32  *
33  */

34 public class SortedListModel extends AbstractListModel JavaDoc
35 {
36
37     /**
38      *
39      */

40     public static final Comparator JavaDoc DEFAULT_COMPARATOR = new Comparator JavaDoc()
41     {
42         public int compare(Object JavaDoc o1, Object JavaDoc o2)
43         {
44             if (o1 == null)
45                 return -1;
46
47             if (o2 == null)
48                 return 1;
49
50             return o1.toString().compareTo(o2.toString());
51         }
52
53         public boolean equals(Object JavaDoc obj)
54         {
55             return obj == this;
56         }
57     };
58
59     /**
60      *
61      */

62     private List JavaDoc elements;
63
64     /**
65      *
66      */

67     private Comparator JavaDoc comp = DEFAULT_COMPARATOR;
68
69     ///////////////////////////////////////////////////////////////////////////
70
// construction
71
///////////////////////////////////////////////////////////////////////////
72

73     /**
74      *
75      */

76     public SortedListModel()
77     {
78         elements = new ArrayList JavaDoc();
79     }
80
81     /**
82      *
83      */

84     public SortedListModel(Collection JavaDoc c)
85     {
86         elements = new ArrayList JavaDoc(c);
87         Collections.sort(elements, comp);
88     }
89
90     /**
91      *
92      */

93     public SortedListModel(int initialCapacity)
94     {
95         elements = new ArrayList JavaDoc(initialCapacity);
96     }
97
98     /**
99      *
100      */

101     public int getSize()
102     {
103         return elements.size();
104     }
105
106     /**
107      *
108      */

109     public Object JavaDoc getElementAt(int index)
110     {
111         return elements.get(index);
112     }
113
114     /**
115      * Returns the comparator used to sort the elements of this list model.
116      *
117      * @see #setComparator
118      */

119     public Comparator JavaDoc getComparator()
120     {
121         return comp;
122     }
123
124     /**
125      *
126      */

127     public void setComparator(Comparator JavaDoc newComp)
128     {
129         if (comp == newComp)
130             return;
131
132         comp = newComp;
133         Collections.sort(elements, comp);
134
135         int last = elements.size() - 1;
136
137         if (last >= 0)
138             super.fireContentsChanged(this, 0, last);
139     }
140
141     /**
142      * Returns <code>true</code> if this list model contains no elements.
143      */

144     public boolean isEmpty()
145     {
146         return elements.isEmpty();
147     }
148
149     /**
150      *
151      */

152     public boolean contains(Object JavaDoc o)
153     {
154         return Collections.binarySearch(elements, o, getComparator()) >= 0;
155     }
156
157     /**
158      *
159      */

160     public Object JavaDoc[] toArray()
161     {
162         return elements.toArray();
163     }
164
165     /**
166      *
167      */

168     public Object JavaDoc[] toArray(Object JavaDoc[] a)
169     {
170         return elements.toArray(a);
171     }
172
173     /**
174      *
175      */

176     public int add(Object JavaDoc o)
177     {
178         int index = Collections.binarySearch(elements, o, getComparator());
179         if (index < 0)
180             index = -index - 1;
181
182         elements.add(index, o);
183         fireIntervalAdded(this, index, index);
184
185         return index;
186     }
187
188     /**
189      *
190      */

191     public int indexOf(Object JavaDoc o)
192     {
193         return Collections.binarySearch(elements, o, getComparator());
194     }
195
196     /**
197      *
198      */

199     public int remove(Object JavaDoc o)
200     {
201         int index = Collections.binarySearch(elements, o, getComparator());
202         if (index >= 0)
203         {
204             remove(index);
205         }
206         return index;
207     }
208
209     /**
210      *
211      */

212     public boolean remove(int index)
213     {
214         elements.remove(index);
215         fireIntervalRemoved(this, index, index);
216
217         return true;
218     }
219
220     /**
221      *
222      */

223     public void clear()
224     {
225         int last = elements.size() - 1;
226
227         if (last >= 0)
228         {
229             elements.clear();
230             fireIntervalRemoved(this, 0, last);
231         }
232     }
233
234     /**
235      * Returns a string that displays and identifies this
236      * object's properties.
237      *
238      * @return a String representation of this object
239      */

240     public String JavaDoc toString()
241     {
242         return elements.toString();
243     }
244 }
245
Popular Tags