KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > core > util > ObjectList


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.tasklist.core.util;
21
22 import java.util.AbstractList JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25 import javax.swing.event.EventListenerList JavaDoc;
26
27 /**
28  * A list of objects that allows listening for changes.
29  *
30  * @author tl
31  */

32 public class ObjectList<E> extends AbstractList JavaDoc<E> {
33     private EventListenerList JavaDoc listeners = null;
34     
35     /** internal representation */
36     private List JavaDoc<E> objects = null;
37     
38     /**
39      * Creates a new instance of ObjectList with an ArrayList as underlying
40      * implementation class.
41      */

42     public ObjectList() {
43     }
44     
45     /**
46      * Creates a new instance of ObjectList.
47      *
48      * @param objects observed list
49      */

50     public ObjectList(List JavaDoc<E> objects) {
51         this.objects = objects;
52     }
53     
54     public int size() {
55         int sz;
56         if (objects == null)
57             sz = 0;
58         else
59             sz = objects.size();
60         return sz;
61     }
62     
63     public E set(int index, E obj) {
64         if (objects == null)
65             objects = new ArrayList JavaDoc<E>();
66         
67         E old = objects.set(index, obj);
68         if (hasListeners()) {
69             fireEvent(new ObjectListEvent(this, ObjectListEvent.EVENT_REMOVED,
70                 new int[] {index}, new Object JavaDoc[]{old}));
71             fireEvent(new ObjectListEvent(this, ObjectListEvent.EVENT_ADDED,
72                 new int[] {index}, new Object JavaDoc[]{obj}));
73         }
74         return old;
75     }
76     
77     /**
78      * Adds a listener to this list
79      *
80      * @param listener a listener
81      */

82     public void addListener(ObjectListListener listener) {
83         if (listeners == null)
84             listeners = new EventListenerList JavaDoc();
85         listeners.add(ObjectListListener.class, listener);
86     }
87     
88     /**
89      * Removes a listener
90      *
91      * @param listener a listener
92      */

93     public void removeListener(ObjectListListener listener) {
94         if (listeners != null)
95             listeners.remove(ObjectListListener.class, listener);
96         if (listeners.getListenerCount() == 0)
97             this.listeners = null;
98     }
99     
100     /**
101      * Are there any listeners?
102      *
103      * @return true if the number of listeners > 0
104      */

105     protected boolean hasListeners() {
106         return listeners != null && listeners.getListenerCount() != 0;
107     }
108     
109     /**
110      * Fires an event
111      *
112      * @param e an event
113      */

114     protected void fireEvent(ObjectListEvent e) {
115         // Guaranteed to return a non-null array
116
Object JavaDoc[] l = listeners.getListenerList();
117         
118         // Process the listeners last to first, notifying
119
// those that are interested in this event
120
for (int i = l.length - 2; i >= 0; i -= 2) {
121             // Lazily create the event:
122
((ObjectListListener) l[i+1]).listChanged(e);
123         }
124     }
125     
126     public E get(int index) {
127         if (objects == null)
128             throw new IndexOutOfBoundsException JavaDoc("Empty list"); // NOI18N
129

130         return objects.get(index);
131     }
132     
133     @Override JavaDoc
134     public void add(int index, E element) {
135     if (objects == null)
136             objects = new ArrayList JavaDoc();
137         
138         objects.add(index, element);
139         
140         if (hasListeners() || element instanceof ObjectListListener) {
141             ObjectListEvent e = new ObjectListEvent(this,
142                     ObjectListEvent.EVENT_ADDED, new int[] {index},
143                     new Object JavaDoc[] {element});
144             if (hasListeners())
145                 fireEvent(e);
146             if (element instanceof ObjectListListener)
147                 ((ObjectListListener) element).listChanged(e);
148         }
149     }
150     
151     public E remove(int index) {
152     if (objects == null)
153             throw new IndexOutOfBoundsException JavaDoc("Empty list"); // NOI18N
154
E obj = objects.remove(index);
155         if (hasListeners() || obj instanceof ObjectListListener) {
156             ObjectListEvent e = new ObjectListEvent(this,
157                     ObjectListEvent.EVENT_REMOVED,
158                     new int[] {index}, new Object JavaDoc[] {obj});
159             if (hasListeners())
160                 fireEvent(e);
161             if (obj instanceof ObjectListListener)
162                 ((ObjectListListener) obj).listChanged(e);
163         }
164         if (objects.size() == 0)
165             objects = null;
166         return obj;
167     }
168     
169     /**
170      * Moves an element
171      *
172      * @param index old index of the element
173      * @param newIndex new index of the element
174      */

175     public void move(int index, int newIndex) {
176     if (objects == null)
177             throw new IndexOutOfBoundsException JavaDoc("Empty list"); // NOI18N
178
objects.add(newIndex, objects.remove(index));
179         if (hasListeners()) {
180             ObjectListEvent e = new ObjectListEvent(this,
181                     ObjectListEvent.EVENT_REORDERED, null, null);
182             fireEvent(e);
183         }
184     }
185 }
186
Popular Tags