001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.dbschema.jdbcimpl.wizard;
043:
044: import java.util.Comparator;
045: import java.util.List;
046: import java.util.ArrayList;
047: import java.util.Collection;
048: import java.util.Collections;
049:
050: import javax.swing.AbstractListModel;
051:
052: /**
053: *
054: */
055: public class SortedListModel extends AbstractListModel {
056:
057: /**
058: *
059: */
060: public static final Comparator DEFAULT_COMPARATOR = new Comparator() {
061: public int compare(Object o1, Object o2) {
062: if (o1 == null)
063: return -1;
064:
065: if (o2 == null)
066: return 1;
067:
068: return o1.toString().compareTo(o2.toString());
069: }
070:
071: public boolean equals(Object obj) {
072: return obj == this ;
073: }
074: };
075:
076: /**
077: *
078: */
079: private List elements;
080:
081: /**
082: *
083: */
084: private Comparator comp = DEFAULT_COMPARATOR;
085:
086: ///////////////////////////////////////////////////////////////////////////
087: // construction
088: ///////////////////////////////////////////////////////////////////////////
089:
090: /**
091: *
092: */
093: public SortedListModel() {
094: elements = new ArrayList();
095: }
096:
097: /**
098: *
099: */
100: public SortedListModel(Collection c) {
101: elements = new ArrayList(c);
102: Collections.sort(elements, comp);
103: }
104:
105: /**
106: *
107: */
108: public SortedListModel(int initialCapacity) {
109: elements = new ArrayList(initialCapacity);
110: }
111:
112: /**
113: *
114: */
115: public int getSize() {
116: return elements.size();
117: }
118:
119: /**
120: *
121: */
122: public Object getElementAt(int index) {
123: return elements.get(index);
124: }
125:
126: /**
127: * Returns the comparator used to sort the elements of this list model.
128: *
129: * @see #setComparator
130: */
131: public Comparator getComparator() {
132: return comp;
133: }
134:
135: /**
136: *
137: */
138: public void setComparator(Comparator newComp) {
139: if (comp == newComp)
140: return;
141:
142: comp = newComp;
143: Collections.sort(elements, comp);
144:
145: int last = elements.size() - 1;
146:
147: if (last >= 0)
148: super .fireContentsChanged(this , 0, last);
149: }
150:
151: /**
152: * Returns <code>true</code> if this list model contains no elements.
153: */
154: public boolean isEmpty() {
155: return elements.isEmpty();
156: }
157:
158: /**
159: *
160: */
161: public boolean contains(Object o) {
162: return Collections.binarySearch(elements, o, getComparator()) >= 0;
163: }
164:
165: /**
166: *
167: */
168: public Object[] toArray() {
169: return elements.toArray();
170: }
171:
172: /**
173: *
174: */
175: public Object[] toArray(Object[] a) {
176: return elements.toArray(a);
177: }
178:
179: /**
180: *
181: */
182: public int add(Object o) {
183: int index = Collections.binarySearch(elements, o,
184: getComparator());
185: if (index < 0)
186: index = -index - 1;
187:
188: elements.add(index, o);
189: fireIntervalAdded(this , index, index);
190:
191: return index;
192: }
193:
194: /**
195: *
196: */
197: public int indexOf(Object o) {
198: return Collections.binarySearch(elements, o, getComparator());
199: }
200:
201: /**
202: *
203: */
204: public int remove(Object o) {
205: int index = Collections.binarySearch(elements, o,
206: getComparator());
207: if (index >= 0) {
208: remove(index);
209: }
210: return index;
211: }
212:
213: /**
214: *
215: */
216: public boolean remove(int index) {
217: elements.remove(index);
218: fireIntervalRemoved(this , index, index);
219:
220: return true;
221: }
222:
223: /**
224: *
225: */
226: public void clear() {
227: int last = elements.size() - 1;
228:
229: if (last >= 0) {
230: elements.clear();
231: fireIntervalRemoved(this , 0, last);
232: }
233: }
234:
235: /**
236: * Returns a string that displays and identifies this
237: * object's properties.
238: *
239: * @return a String representation of this object
240: */
241: public String toString() {
242: return elements.toString();
243: }
244: }
|