01: /*
02: * DriverListModel.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.gui.profiles;
13:
14: import java.util.ArrayList;
15: import java.util.Collections;
16: import java.util.Comparator;
17: import java.util.List;
18:
19: import javax.swing.ListModel;
20: import javax.swing.event.ListDataListener;
21:
22: import workbench.db.DbDriver;
23: import workbench.db.DbDriver;
24:
25: /**
26: *
27: * @author support@sql-workbench.net
28: */
29: class DriverListModel implements ListModel {
30: private ArrayList<DbDriver> drivers;
31:
32: public DriverListModel(List<DbDriver> aDriverList) {
33: this .drivers = new ArrayList<DbDriver>(aDriverList.size());
34: this .drivers.addAll(0, aDriverList);
35: Comparator<DbDriver> comp = new Comparator<DbDriver>() {
36: public int compare(DbDriver o1, DbDriver o2) {
37: if (o1 == null && o2 == null)
38: return 0;
39: if (o1 == null)
40: return -1;
41: if (o2 == null)
42: return 1;
43: return o1.getName().compareTo(o2.getName());
44: }
45: };
46: Collections.sort(this .drivers, comp);
47: }
48:
49: /** Adds a listener to the list that's notified each time a change
50: * to the data model occurs.
51: * @param l the <code>ListDataListener</code> to be added
52: *
53: */
54: public void addListDataListener(ListDataListener l) {
55: }
56:
57: /** Returns the value at the specified index.
58: * @param index the requested index
59: * @return the value at <code>index</code>
60: *
61: */
62: public Object getElementAt(int index) {
63: return this .getDriver(index).getName();
64: }
65:
66: public DbDriver getDriver(int index) {
67: return this .drivers.get(index);
68: }
69:
70: /**
71: * Returns the length of the list.
72: * @return the length of the list
73: *
74: */
75: public int getSize() {
76: return this .drivers.size();
77: }
78:
79: public void removeListDataListener(ListDataListener l) {
80: }
81:
82: public void addDriver(DbDriver aDriver) {
83: this .drivers.add(this .drivers.size(), aDriver);
84: }
85:
86: public void deleteDriver(int index) {
87: this .drivers.remove(index);
88: }
89:
90: public void putDriver(int index, DbDriver aDriver) {
91: this .drivers.set(index, aDriver);
92: index = 1;
93: }
94:
95: public List<DbDriver> getValues() {
96: return this.drivers;
97: }
98: }
|