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.search;
21  
22  import java.io.File;
23  import java.util.Iterator;
24  import java.util.StringTokenizer;
25  
26  import javax.swing.Action;
27  import javax.swing.Icon;
28  
29  import org.xnap.gui.FileProvider;
30  import org.xnap.gui.action.OpenFileAction;
31  import org.xnap.gui.util.IconHelper;
32  import org.xnap.io.MetaInfoFile;
33  import org.xnap.peer.Peer;
34  import org.xnap.plugin.Plugin;
35  
36  /***
37   * This class stores a local search result.
38   */
39  public class MetaInfoFileSearchResult implements SearchResult, FileProvider {
40  
41      // --- Constants(s) ---
42  
43      public static Icon ICON = IconHelper.getIcon("gohome.png", 16, false);
44  
45      //--- Data Field(s) ---
46  
47      private MetaInfoFile file;
48  
49      //--- Constructor(s) ---
50  
51      public MetaInfoFileSearchResult(MetaInfoFile file)
52      {
53  		this.file = file;
54      }
55  
56      // --- Method(s) ---
57  
58      public boolean canGroup(SearchResult result)
59      {
60  		return false;
61      }
62  
63      public SearchResultContainer createContainer()
64      {
65  		return null;
66      }
67  
68      public Object get(String key)
69      {
70  		return file.get(key);
71      }
72  
73      public Action[] getActions()
74      {
75  		return new Action[] { new OpenFileAction(this) };
76      }
77  
78      public int getAvailability()
79      {
80  		return 0;
81      }
82  
83      public File[] getFiles()
84      {
85  		return new File[] { file };
86      }
87  
88      public String getFilename()
89      {
90  		return file.getAbsolutePath();
91      }
92  
93      public long getFilesize()
94      {
95  		return file.length();
96      }
97  
98  	public Object getHash()
99  	{
100 		return null;
101 	}
102 
103     /***
104      * Returns null;
105      */
106     public Icon getIcon()
107     {
108 		return ICON;
109     }
110 
111     public String[] getPath()
112     {
113 		StringTokenizer t = new StringTokenizer(getFilename(), File.separator);
114 		if (t.countTokens() > 1) {
115 			String[] path = new String[t.countTokens() - 1];
116 			for (int i = 0; i < path.length; i++) {
117 				path[i] = t.nextToken();
118 			}
119 
120 			return path;
121 		}
122 		else {
123 			return null;
124 		}
125     }
126 
127     public Plugin getPlugin()
128     {
129 		return null;
130     }
131 
132 	public int getSourcesCount()
133 	{
134 		return 1;
135 	}
136 
137     /***
138      * Returns the name of the file.
139      */
140     public String getShortFilename() 
141     {
142 		return file.getName();
143     }
144 
145     public Peer getPeer()
146     {
147 		return null;
148     }
149 
150     /***
151      * Returns the keys of the file.
152      *
153      * @see MetaInfoFile#keys()
154      */
155     public Iterator keys()
156     {
157 		return file.keys();
158     }
159 
160 }