Source code: org/modama/tools/AbstractList_ListModel.java
1 /**
2 * Created by IntelliJ IDEA.
3 * User: Administrator
4 * Date: Feb 2, 2003
5 * Time: 1:39:48 PM
6 * To change this template use Options | File Templates.
7 */
8 package org.modama.tools;
9
10 import javax.swing.*;
11 import java.util.*;
12
13 /**
14 * implements a <code>java.util.List</code> that can directly be used as a listmodel <br>
15 * since multiinheritance is not possible, there are 2 possibilitys:<br>
16 * implementing the List interface, and inherit from AbstractListModel <br>
17 * or implementing the ListModel interface and inheritating from AbstractList<br>
18 * i took the first method because some of the List methods have to changed anyhow, to
19 * let them fire changeevents
20 */
21 public class AbstractList_ListModel extends AbstractListModel implements List {
22 /**
23 * the collection
24 */
25 AbstractList list = null;
26
27 public AbstractList_ListModel(AbstractList list) {
28 this.list = list;
29 }
30
31 //
32 // ListModel
33 //
34
35 public int getSize() {
36 return list.size();
37 }
38
39 public Object getElementAt(int index) {
40 return list.get( index );
41 }
42
43 //
44 // Collection functions
45 //
46 public int size() {
47 return list.size();
48 }
49
50 public boolean isEmpty() {
51 return list.isEmpty();
52 }
53
54 public boolean contains(Object o) {
55 return list.contains(o);
56 }
57
58 public Iterator iterator() {
59 return list.iterator();
60 }
61
62 public Object[] toArray() {
63 return list.toArray();
64 }
65
66 public Object[] toArray(Object a[]) {
67 return list.toArray( a );
68 }
69
70 public boolean add(Object o) {
71 boolean result = list.add( o );
72 if( result ) fireIntervalAdded( this, list.size() - 1, list.size() - 1 );
73 return result;
74 }
75
76 public boolean remove(Object o) {
77 boolean result = list.remove( o );
78 if( result ) fireIntervalRemoved( this, 0, list.size() == 0 ? 0 : list.size() - 1 );
79 return result;
80 }
81
82 public boolean containsAll(Collection c) {
83 return list.containsAll( c );
84 }
85
86 public boolean addAll(Collection c) {
87 boolean result = list.addAll( c );
88 if( result ) fireIntervalAdded( this, list.size() - c.size(), list.size() - 1 );
89 return result;
90 }
91
92 public boolean removeAll(Collection c) {
93 boolean result = list.removeAll( c );
94 if( result ) fireIntervalRemoved( this, 0, list.size() - 1 );
95 return result;
96 }
97
98 public boolean retainAll(Collection c) {
99 return list.retainAll( c );
100 }
101
102 public void clear() {
103 int size = list.size();
104 list.clear();
105 if( size == 0 ) size = 1;
106 fireIntervalRemoved( this, 0, size - 1 );
107 }
108
109 //
110 // List functions
111 //
112 public boolean addAll(int index, Collection c) {
113 boolean result = list.addAll( index, c );
114 if( result ) fireIntervalAdded( this, index , index + c.size() - 1 );
115 return result;
116 }
117
118 public Object get(int index) {
119 return list.get( index );
120 }
121
122 public Object set(int index, Object element) {
123 Object o = list.set( index, element );
124 fireContentsChanged( this, index, index );
125 return o;
126 }
127
128 public void add(int index, Object element) {
129 list.add( index, element );
130 fireIntervalAdded( this, index, index );
131 }
132
133 public Object remove(int index) {
134 Object o = list.remove(index );
135 fireIntervalRemoved( this, index, index );
136 return o;
137 }
138
139 public int indexOf(Object o) {
140 return list.indexOf(o);
141 }
142
143 public int lastIndexOf(Object o) {
144 return list.lastIndexOf(o);
145 }
146
147 public ListIterator listIterator() {
148 return list.listIterator();
149 }
150
151 public ListIterator listIterator(int index) {
152 return list.listIterator( index );
153 }
154
155 public List subList(int fromIndex, int toIndex) {
156 return list.subList( fromIndex, toIndex );
157 }
158 }