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.transfer;
21  
22  import java.util.Iterator;
23  import java.util.Vector;
24  
25  import org.xnap.event.ListListener;
26  import org.xnap.event.ListSupport;
27  
28  /***
29   * Provides a default implementation for downloads.
30   */
31  public abstract class AbstractDownload extends AbstractTransfer 
32      implements Download, TransferContainer {
33  
34      //--- Constant(s) ---
35  
36      //--- Data field(s) ---
37  
38      private ListSupport listeners = new ListSupport(this);
39      private Vector children = new Vector();
40      
41      //--- Constructor(s) ---
42  
43      public AbstractDownload()
44      {
45      }
46  
47      //--- Method(s) ---
48  
49      public void addListListener(ListListener listener)
50      {
51  		listeners.addListListener(listener); 
52      }
53  
54      public void add(Download d)
55      {
56  		children.add(d);
57  		listeners.fireItemAdded(d);
58      }
59  
60  	public void clearDone()
61  	{
62  		synchronized (children) {
63  			for (int i = children.size() - 1; i >= 0; i--) {
64  				Download d = (Download)children.get(i);
65  				if (d.isDone()) {
66  					remove(d);
67  					d.cleared();
68  				}
69  			}
70  		}
71  	}
72  
73      public int getChildCount()
74  	{
75  		return children.size();
76  	}
77  
78      public Transfer[] getChildren()
79      {
80  		return (Transfer[])children.toArray(new Transfer[0]);
81      }
82  
83  	public Iterator iterator()
84  	{
85  		return children.iterator();
86  	}
87  
88      public void removeListListener(ListListener listener)
89      {
90  		listeners.removeListListener(listener);
91      }
92  
93      public void remove(Download d)
94      {
95  		children.remove(d);
96  		listeners.fireItemRemoved(d);
97      }
98  
99  }
100