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.jtella;
21  
22  import javax.swing.Action;
23  
24  import org.xnap.peer.Peer;
25  import org.xnap.plugin.Plugin;
26  import org.xnap.search.AbstractSearchResult;
27  import org.xnap.search.SearchResult;
28  import org.xnap.search.SearchResultContainer;
29  
30  import com.kenmccrary.jtella.SearchReplyMessage;
31  
32  /***
33   * This class is the default implementation for search results.
34   */
35  public class JTellaSearchResult extends AbstractSearchResult {
36  
37      // --- Constants(s) ---
38  
39      //--- Data Field(s) ---
40  
41      private SearchReplyMessage message;
42  
43      /***
44       * The index of the file record in {@link #message}.
45       */
46      private int fileIndex;
47  
48      private JTellaServant peer;
49      private String filename;
50  
51      //--- Constructor(s) ---
52  
53      public JTellaSearchResult(SearchReplyMessage message, int fileIndex,
54  							  JTellaServant peer)
55      {
56  		this.message = message;
57  		this.fileIndex = fileIndex;
58  		this.peer = peer;
59  
60  		// shorten filename by one character
61  		this.filename = getFileRecord().getName();
62  		this.filename = filename.substring(0, filename.length() - 1);
63      }
64  
65      // --- Method(s) ---
66  
67      public boolean canGroup(SearchResult result)
68      {
69  		if (result instanceof JTellaSearchResult) {
70  			return result.getFilesize() == getFilesize();
71  		}
72  
73  		return false;
74      }
75  
76      public SearchResultContainer createContainer()
77      {
78  		return new JTellaSearchResultContainer();
79      }
80  
81      public Action[] getActions()
82      {
83  		return new Action[] { new JTellaDownloadAction(this) };
84      }
85  
86      public int getAvailability()
87      {
88  		return message.getDownloadSpeed() + 1;
89      }
90  
91      public String getFilename()
92      {
93  		return filename;
94      }
95  
96      public SearchReplyMessage.FileRecord getFileRecord()
97      {
98  		return message.getFileRecord(fileIndex);
99      }
100 
101     /***
102      * Returns the filesize.
103      */
104     public long getFilesize()
105     {
106 		return getFileRecord().getSize();
107     }
108 
109 	public Object getHash()
110 	{
111 		return new Long(getFilesize());
112 	}
113 
114     /***
115      * Returns the jtella plugin.
116      */
117     public Plugin getPlugin()
118     {
119 		return JTellaPlugin.getInstance();
120     }
121 
122     /***
123      * Returns the peer that offer this result for download.
124      */
125     public Peer getPeer()
126     {
127 		return peer;
128     }
129 
130     public SearchReplyMessage getReply()
131     {
132 		return message;
133     }
134 
135 }