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.plugin.azureus;
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  import java.text.NumberFormat;
26  import java.util.ArrayList;
27  
28  import javax.swing.SwingUtilities;
29  
30  import org.gudy.azureus2.core3.tracker.host.TRHostListener;
31  import org.gudy.azureus2.core3.tracker.host.TRHostTorrent;
32  import org.xnap.XNap;
33  import org.xnap.gui.table.AbstractColumnTableModel;
34  import org.xnap.gui.table.Column;
35  import org.xnap.gui.table.FilesizeCellRenderer;
36  
37  /***
38   * Table model for {@link } objects.
39   */
40  public class AzureusTrackerTableModel extends AbstractColumnTableModel
41  	implements TRHostListener
42  {
43  
44      //--- Data field(s) ---
45  
46      private ArrayList data = new ArrayList();
47  
48      //--- Constructor(s) ---
49  
50      public AzureusTrackerTableModel() 
51      {
52  		Column columns[] = new Column[] {
53  			new Column("name", XNap.tr("Name"), String.class),
54  			new Column("tracker", XNap.tr("Tracker"), String.class),
55  			new Column("status", XNap.tr("Status"), String.class),
56  			new Column("seeds", XNap.tr("Seeds"), Integer.class),
57  			new Column("peers", XNap.tr("Peers"), Integer.class),
58  			new Column("announces", XNap.tr("Announces"), Integer.class),
59  			new Column("scrapes", XNap.tr("Scrapes"), Integer.class),
60  			new Column("completed", XNap.tr("Completed"), Integer.class),
61  			new Column("downloaded", XNap.tr("Downloaded"), Long.class, 
62  					   new FilesizeCellRenderer()),
63  			new Column("uploaded", XNap.tr("Uploaded"), Long.class,
64  					   new FilesizeCellRenderer()),
65  		};
66  		addColumns(columns);
67      }
68  
69      //--- Method(s) ---
70  
71      public int getRowCount() 
72      {
73          return data.size();
74      }
75  
76      public Object get(int i, int j) 
77      {
78  		TRHostTorrent t = (TRHostTorrent)data.get(i);
79  		
80  //		TRHostPeer[]	peers = torrent.getPeers();
81  //		
82  //		int		peer_count	= 0;
83  //		int		seed_count	= 0;
84  //		
85  //		long	uploaded	= 0;
86  //		long	downloaded	= 0;
87  //		long	left		= 0;
88  //		
89  //		for (int i=0;i<peers.length;i++){
90  //			
91  //			TRHostPeer	peer = peers[i];
92  //			
93  //			if ( peer.isSeed()){
94  //				
95  //				seed_count++;
96  //			}else{
97  //				
98  //				peer_count++;
99  //			}
100 //			
101 //			uploaded 	+= peer.getUploaded();
102 //			downloaded	+= peer.getDownloaded();
103 //			left		+= peer.getAmountLeft();
104 //		}
105 		
106         switch (j) {
107 		case 0:
108 			return new String(t.getTorrent().getName());
109 		case 1:
110 			return t.getTorrent().getAnnounceURL().toString();
111 		case 2:
112 			return getStatus(t.getStatus());
113 		case 3:
114 			return new Integer(0);
115 		case 4:
116 			return new Integer(0);
117 		case 5:
118 			return new Integer(t.getAnnounceCount());
119 		case 6: 
120  			return new Integer(t.getScrapeCount());
121 		case 7:
122 			return new Integer(t.getCompletedCount());
123 		case 8:
124 			return new Long(t.getTotalBytesIn());
125 		case 9:
126 			return new Long(t.getTotalBytesOut());
127 		default:
128 			return null;
129         }
130     }
131 
132     public String getStatus(int status)
133     {
134     	switch (status) {
135     	case TRHostTorrent.TS_FAILED:
136     		return XNap.tr("Failed");
137     	case TRHostTorrent.TS_PUBLISHED:
138     		return XNap.tr("Published");
139     	case TRHostTorrent.TS_STARTED:
140     		return XNap.tr("Started");
141     	case TRHostTorrent.TS_STOPPED:
142     		return XNap.tr("Stopped");
143     	default:
144     		return XNap.tr("Unknown status {0}", status);
145     	}
146     }
147     
148 	/***
149 	 *  @see org.gudy.azureus2.core3.tracker.host.TRHostListener#torrentAdded(org.gudy.azureus2.core3.tracker.host.TRHostTorrent)
150 	 */
151 	public void torrentAdded(final TRHostTorrent t)
152 	{
153 		Runnable runner = new Runnable()
154 		{
155 			public void run()
156 			{
157 				data.add(t);
158 				fireTableRowsInserted(data.size() - 1, data.size() - 1);
159 			}
160 		};
161 		SwingUtilities.invokeLater(runner);
162 	}
163 
164 	/***
165 	 *  @see org.gudy.azureus2.core3.tracker.host.TRHostListener#torrentChanged(org.gudy.azureus2.core3.tracker.host.TRHostTorrent)
166 	 */
167 	public void torrentChanged(final TRHostTorrent t)
168 	{
169 		Runnable runner = new Runnable()
170 		{
171 			public void run()
172 			{
173 				int i = data.indexOf(t);
174 				if (i != -1) {
175 					data.remove(i);
176 					fireTableRowsUpdated(i, i);
177 				}
178 			}
179 		};
180 		SwingUtilities.invokeLater(runner);
181 	}
182 
183 	/***
184 	 *  @see org.gudy.azureus2.core3.tracker.host.TRHostListener#torrentRemoved(org.gudy.azureus2.core3.tracker.host.TRHostTorrent)
185 	 */
186 	public void torrentRemoved(final TRHostTorrent t)
187 	{
188 		Runnable runner = new Runnable()
189 		{
190 			public void run()
191 			{
192 				int i = data.indexOf(t);
193 				if (i != -1) {
194 					data.remove(i);
195 					fireTableRowsDeleted(i, i);
196 				}
197 			}
198 		};
199 		SwingUtilities.invokeLater(runner);
200 	}
201 
202 	/***
203 	 *  @see org.gudy.azureus2.core3.tracker.host.TRHostListener#handleExternalRequest(java.lang.String, java.lang.String, java.lang.String, java.io.InputStream, java.io.OutputStream)
204 	 */
205 	public boolean handleExternalRequest(String client_address, String url, String header, InputStream is, OutputStream os) throws IOException
206 	{
207 		return false;
208 	}
209 
210 }