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.opennap.net;
21  
22  import javax.swing.Action;
23  import javax.swing.Icon;
24  
25  import java.awt.event.ActionEvent;
26  
27  import org.xnap.XNap;
28  import org.xnap.gui.action.AbstractDownloadAction;
29  import org.xnap.peer.Peer;
30  import org.xnap.plugin.Plugin;
31  import org.xnap.plugin.opennap.OpenNapPlugin;
32  import org.xnap.plugin.opennap.user.OpenNapUser;
33  import org.xnap.search.AbstractSearchResult;
34  import org.xnap.search.SearchFilter;
35  import org.xnap.search.SearchResult;
36  import org.xnap.search.SearchResultContainer;
37  
38  /***
39   * Immutable.
40   */
41  public class OpenNapSearchResult extends AbstractSearchResult {
42  
43      //--- Constant(s) ---
44  
45      //--- Data field(s) ---
46  
47      private String filename;
48      private long filesize;
49      private OpenNapUser user;
50  	private OpenNapSearchResult child;
51      private SearchFilter filter;
52  
53      //--- Constructor(s) ---
54  
55      public OpenNapSearchResult(SearchFilter filter, long filesize, 
56  							   int bitrate, int frequency, int length, 
57  							   OpenNapUser user, String filename, String md5) 
58      {
59  		super("///");
60  
61  		this.filter = filter;
62  
63  		this.filesize = filesize;
64  		this.filename = filename;
65  		this.user = user;
66  
67  		put(XNap.tr("Bitrate"), new Integer(bitrate));
68  		put(XNap.tr("Frequency"), new Integer(frequency));
69  		put(XNap.tr("Length"), new Integer(length));
70  		// put(XNap.tr("MD5"), md5);
71      }
72  
73      
74      //--- Method(s) ---
75  
76      public boolean canGroup(SearchResult result)
77      {
78  		if (result instanceof OpenNapSearchResult) {
79  			return result.getFilesize() == getFilesize();
80  		}
81  		return false;
82      }
83  
84      public SearchResultContainer createContainer()
85      {
86  		return new OpenNapSearchResultContainer();
87      }
88  
89      public boolean equals(Object o)
90      {
91  		if (o instanceof OpenNapSearchResult) {
92  			OpenNapSearchResult sr = (OpenNapSearchResult)o;
93  			return ((getPeer().equals(sr.getPeer()))
94  					&& (getFilename().equals(sr.getFilename())));
95  		}
96  		return false;
97  	}
98  
99      public Action[] getActions()
100     {
101 		return new Action[] { new DownloadAction(), };
102     }
103 
104     public int getAvailability()
105     {
106 		return getPeer().getLinkSpeed() + 1;
107     }
108 
109     public String getFilename()
110     {
111 		return filename;
112     }
113 
114     /***
115      * Returns the filesize.
116      */
117     public long getFilesize()
118     {
119 		return filesize;
120     }
121 
122 	public SearchFilter getFilter()
123 	{
124 		return filter;
125 	}
126 
127 	public Object getHash()
128 	{
129 		return new Long(getFilesize());
130 	}
131 
132     public Icon getIcon()
133     {
134 		return OpenNapPlugin.ICON_16;
135     }
136 
137 	public OpenNapUser getOpenNapUser()
138 	{
139 		return user;
140 	}
141 
142     /***
143      * Returns the OpenNap plugin.
144      */
145     public Plugin getPlugin()
146     {
147 		return OpenNapPlugin.getInstance();
148     }
149 
150     /***
151      * Returns the peer that offer this result for download.
152      */
153     public Peer getPeer()
154     {
155 		return user;
156     }
157 
158 	/***
159 	 * Sets <code>result</code> as a child of this result.
160 	 */
161 	public void setChild(OpenNapSearchResult result)
162 	{
163 		if (child != null) {
164 			child.setChild(result);
165 		}
166 	}
167 
168 
169 	//--- Inner Class(es) ---
170 	
171 	private class DownloadAction extends AbstractDownloadAction {
172 
173 		public void actionPerformed(ActionEvent e) 
174 		{
175 			if (isNotInLibrary(getShortFilename())) {
176 				OpenNapSearchResult[] results
177 					= new OpenNapSearchResult[] { OpenNapSearchResult.this };
178 				OpenNapPlugin.getTransferManager().download(results);
179 			}
180 		}
181 
182 	}
183 
184 }