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.util.LinkedList;
23  import java.util.List;
24  
25  import org.xnap.event.ListEvent;
26  import org.xnap.event.ListListener;
27  import org.xnap.event.ListSupport;
28  import org.xnap.event.StatusListener;
29  import org.xnap.util.*;
30  import org.apache.log4j.Logger;
31  
32  /***
33   * Controls a search object. Groups search results.
34   */
35  public class SearchController implements SearchHandler
36  {
37  
38      //--- Constant(s) ---
39  
40      //--- Data field(s) ---
41  
42      private static Logger logger = Logger.getLogger(SearchController.class);
43  
44      private List data = new LinkedList();
45      private Search search;
46      private SearchFilter filter;
47      private ListListener llResults;
48      private StatusListener slStatus;
49  	private ListSupport lsResults = new ListSupport(this);	
50  
51      //--- Constructor(s) ---
52  
53      public SearchController(Search search, ListListener llResults, 
54  							StatusListener slStatus)
55      {
56  		this.search = search;
57  		this.llResults = llResults;
58  		this.slStatus = slStatus;
59      }
60  
61      //--- Method(s) ---
62  
63  	/***
64  	 * <code>listener</code> is notified when a result is received,
65  	 * regardless of any filters.
66  	 */
67      public void addListListener(ListListener listener)
68      {
69  		lsResults.addListListener(listener);
70      }
71  
72      public SearchFilter getFilter()
73      {
74  		return filter;
75      }
76  
77      public synchronized int getResultCount()
78      {
79  		return data.size();
80      }
81  		
82      /***
83       * Returns the search object that is controlled by this controller.
84       */ 
85      public Search getSearch()
86      {
87  		return search;
88      }
89  
90  	public String getStatus()
91  	{
92  		return search.getStatus();
93  	}
94  
95  	public boolean isDone()
96  	{
97  		return search.isDone();
98  	}
99  
100 	/***
101 	 * @see #addListListener(ListListener)
102 	 */
103     public void removeListListener(ListListener listener)
104     {
105 		lsResults.removeListListener(listener);
106     }
107 
108     public synchronized void resultReceived(SearchResult result)
109     {
110 		data.add(result);
111 		lsResults.fireItemAdded(result);
112 
113 		if (filter == null || filter.matches(result)) {
114 			llResults.itemAdded
115 				(new ListEvent(this, ListEvent.ITEM_ADDED, result));
116 		}
117     }
118 
119     public synchronized void setFilter(SearchFilter fiter)
120     {
121 		// copy data and readd from thread
122 		llResults.itemRemoved(null);
123     }
124     
125     public synchronized void stateChanged(Search search)
126     {
127 		slStatus.setStatus(getStatus());
128     }
129 
130     public void start()
131     {
132 		search.start(this);
133     }
134 
135     /***
136      * Cancels the controlled search.
137      */
138     public void stop()
139     {
140 		try {
141 			search.stop();
142 		}
143 		catch (IllegalOperationException e) {
144 			logger.debug("unexpected state", e);
145 		}
146     }
147 
148 }