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.Hashtable;
23  import java.util.Iterator;
24  import java.util.LinkedList;
25  import java.util.List;
26  
27  import javax.swing.Action;
28  
29  public class SearchContainer implements Search, SearchHandler
30  {
31  
32      //--- Constant(s) ---
33  
34      //--- Data field(s) ---
35  
36  	private SearchHandler handler;
37      private SearchFilter filter;
38      private List searches = new LinkedList();
39  	private Hashtable statusBySearch = new Hashtable();
40  
41      //--- Constructor(s) ---
42  
43      public SearchContainer(SearchFilter filter)
44      {
45  		this.filter = filter;
46      }
47  
48      //--- Method(s) ---
49  
50      public synchronized void add(Search search)
51      {
52  		searches.add(search);
53      }
54  
55  	/***
56  	 * Returns <code>null</code>.
57  	 */
58  	public Action[] getActions()
59  	{
60  		return null;
61  	}
62  
63      public SearchFilter getFilter()
64      {
65  		return filter;
66      }
67  
68      public String getName()
69      {
70  		return (filter != null) ? filter.toString() : "";
71      }
72      
73      public synchronized String getStatus()
74      {
75  		StringBuffer sb = new StringBuffer();
76  		for (Iterator i = statusBySearch.values().iterator(); i.hasNext();) {
77  			String s = (String)i.next();
78  			if (s.length() > 0) {
79  				sb.append(s);
80  				if (i.hasNext()) {
81  					sb.append(", ");
82  				}
83  			}
84  		}
85  		return sb.toString();
86      }
87  
88      public synchronized boolean isDone()
89      {
90  		for (Iterator i = searches.iterator(); i.hasNext();) {
91  			if (!((Search)i.next()).isDone()) {
92  				return false;
93  			}
94  		}	
95  		return true;
96      }
97  
98      /***
99       * Invoked when <code>result</code> is received.
100      */
101     public void resultReceived(SearchResult result)
102 	{
103 		handler.resultReceived(result);
104 	}
105 
106     /***
107      * Invoked when the state of <code>search</code> changes.
108      */
109     public void stateChanged(Search search)
110 	{
111 		statusBySearch.put(search, search.getStatus());
112 		handler.stateChanged(this);
113 	}
114 
115     public boolean showTree()
116     {
117 		return false;
118     }
119 
120     public synchronized void start(SearchHandler handler)
121     {
122 		this.handler = handler;
123 
124 		for (Iterator i = searches.iterator(); i.hasNext();) {
125 			Search search = (Search)i.next();
126 			search.start(this);
127 			statusBySearch.put(search, search.getStatus());
128 		}	
129     }
130 
131     public synchronized void stop()
132     {
133 		for (Iterator i = searches.iterator(); i.hasNext();) {
134 			((Search)i.next()).stop();
135 		}
136     }
137 
138 }