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  
24  import org.xnap.XNap;
25  import org.xnap.io.Library;
26  import org.xnap.io.MetaInfoFile;
27  
28  /***
29   * This class searches the local library
30   */
31  public class LibrarySearch extends AbstractSearch  {
32  
33      // --- Constants(s) ---
34  
35      //--- Data Field(s) ---
36  
37  	private SearchHandler handler;
38  	private boolean done;
39  
40      //--- Constructor(s) ---
41  
42      /***
43       * Constructs the search object.
44       *
45       * @param filter the search filter
46       */
47      public LibrarySearch(SearchFilter filter)
48      {
49  		super(filter);
50      }
51  
52      // --- Method(s) ---
53  
54      public String getStatus()
55      {
56  		return (done) 
57  			? XNap.tr("Library search done") 
58  			: XNap.tr("Searching library");
59      }
60  
61      public boolean isDone()
62      {
63  		return done;
64      }
65  
66      public void start(SearchHandler handler)
67      {
68  		this.handler = handler;
69  
70  		done = false;
71  
72  		Thread t = new Thread(new SearchRunner(), "LibrarySearch");
73  		t.start();
74      }
75  
76      public void stop()
77      {
78  		done = true;
79      }
80  
81  	//--- Inner Class(es) ---
82  
83  	public class SearchRunner implements Runnable
84  	{
85  		public void run()
86  		{
87  			File[] files 
88  				= Library.getInstance().search(getFilter().getText());
89  			if (files != null && files.length > 0) {
90  				for (int i = 0; i < files.length && !done; i++) {
91  					MetaInfoFileSearchResult result 
92  						= new MetaInfoFileSearchResult((MetaInfoFile)files[i]);
93  					if (getFilter().matches(result)) {
94  						handler.resultReceived(result);
95  					}
96  				}
97  			}
98  			done = true;
99  			handler.stateChanged(LibrarySearch.this);
100 		}
101 	}
102 
103 }
104