View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.event;
21  
22  import java.util.List;
23  import java.util.Vector;
24  
25  /***
26   * This is a utility class that can be used by data containers to notify
27   * <code>ListEvent</code> listeners.
28   */
29  public class ListSupport { 
30  
31      //--- Data field(s) ---
32  
33      private Object source;
34      private List listeners = new Vector();
35  
36      //--- Constructor(s) ---
37      
38      /***
39       * Constructs a ListSupport object.
40       *
41       * @param source the object to be given as the source for any events
42       */
43      public ListSupport(Object source)
44      {
45  		this.source = source;
46      }
47  
48      //--- Method(s) ---
49  
50      public void addListListener(ListListener listener) 
51      {
52          listeners.add(listener);
53      }
54  
55      public void removeListListener(ListListener listener) 
56      {
57          listeners.remove(listener);
58      }
59  
60      public void fireItemAdded(Object item)
61      {
62          Object[] l = listeners.toArray();
63  
64  		if (l != null) {
65  			ListEvent event = new ListEvent(source, ListEvent.ITEM_ADDED, item);
66  
67  			for (int i = l.length - 1; i >= 0; i--) {
68  				((ListListener)l[i]).itemAdded(event);
69  			}
70  		}
71      }
72  
73      public void fireItemRemoved(Object item)
74      {
75          Object[] l = listeners.toArray();
76  
77  		if (l != null) {
78  			ListEvent event 
79  				= new ListEvent(source, ListEvent.ITEM_REMOVED, item);
80  
81  			for (int i = l.length - 1; i >= 0; i--) {
82  				((ListListener)l[i]).itemRemoved(event);
83  			}
84  		}
85      }
86  
87  }