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 java.awt.event.ActionEvent;
23  import java.util.HashSet;
24  import java.util.LinkedList;
25  
26  import javax.swing.AbstractAction;
27  import javax.swing.Action;
28  
29  import org.xnap.XNap;
30  import org.xnap.gui.StatusBar;
31  import org.xnap.gui.util.IconHelper;
32  import org.xnap.plugin.opennap.user.OpenNapUser;
33  import org.xnap.search.Search;
34  import org.xnap.search.SearchFilter;
35  import org.xnap.search.SearchHandler;
36  import org.xnap.search.SearchResult;
37  
38  /***
39   * Implements an OpenNap search on set of servers.
40   */
41  public class OpenNapMetaBrowse implements SearchHandler, Search {
42  
43      // --- Constants(s) ---
44  
45      //--- Data Field(s) ---
46      
47      private SearchHandler handler;
48  	private boolean done;
49  
50      private OpenNapUser user;
51  	private LinkedList servers;
52  
53  	private OpenNapBrowseInterface currentBrowse;
54  	private boolean fallbackToBrowse;
55  
56  	private Action acTryOther = new TryNextServerAction();
57      
58      //--- Constructor(s) ---
59  
60      public OpenNapMetaBrowse(OpenNapUser user)
61      {
62  		this.user = user;
63  
64  		// request client info
65  		user.update();
66      }
67  
68      // --- Method(s) ---
69  
70  	/***
71  	 * Returns <code>null</code>.
72  	 */
73  	public Action[] getActions()
74  	{
75  		return new Action[] { 
76  			acTryOther,
77  		};
78  	}
79  
80      /***
81       * Returns <code>null</code>.
82       */
83      public SearchFilter getFilter()
84      {
85  		return null;
86      }
87  
88      /***
89       * Returns the name of the user that is browsed.
90       */
91      public String getName()
92      {
93  		return user.getName();
94      }
95  
96      /***
97       * Returns <code>true</code>.
98       */
99      public boolean showTree()
100     {
101 		return true;
102     }
103 
104     /***
105      * Returns a string that describes the current status. 
106      */
107     public String getStatus()
108     {
109 		return (currentBrowse != null) ? currentBrowse.getStatus() : "";
110     }
111 
112     /***
113      * Returns true, if this search can not be canceled.
114      */
115     public boolean isDone()
116     {
117 		return done;
118     }
119 
120     public void resultReceived(SearchResult result)
121 	{
122 		handler.resultReceived(result);
123 	}
124 
125     /***
126      * Invoked when the state of <code>search</code> changes.
127      */
128     public void stateChanged(Search search)
129 	{
130 		if (currentBrowse.isDone()) {
131 			if (currentBrowse.isFailed() && fallbackToBrowse) {
132 				// try standard browse as a fallback
133 				currentBrowse = new OpenNapBrowse(user);
134 				fallbackToBrowse = false;
135 				currentBrowse.start(this);
136 			}
137 			else {
138 				done = true;
139 				acTryOther.setEnabled(true);
140 			}
141 		}
142 		handler.stateChanged(this);
143 	}
144 
145     /***
146      * Starts this search.
147      *
148      * @param handler the object that handles the results
149      */
150     public void start(SearchHandler handler)
151     {
152 		this.handler = handler;
153 
154 		done = false;
155 		acTryOther.setEnabled(false);
156 
157 		if (user.canDirectBrowse()) {
158 			currentBrowse = new OpenNapDirectBrowse(user);
159 			fallbackToBrowse = true;
160 		}
161 		else {
162 			currentBrowse = new OpenNapBrowse(user);
163 			fallbackToBrowse = true;
164 		}
165 		currentBrowse.start(this);
166     }
167 
168     /***
169      * Cancels this search.
170      */
171     public void stop()
172     {
173 		currentBrowse.stop();
174 		done = true;
175     }
176 
177     // --- Inner Class(es) ---
178 
179 	public class TryNextServerAction extends AbstractAction
180 	{
181 		private HashSet tried = new HashSet();
182 		
183 		public TryNextServerAction()
184 		{
185 			putValue(Action.NAME, XNap.tr("Try Again"));
186 			putValue(Action.SHORT_DESCRIPTION, 
187 					 XNap.tr("Try to browse peer on another server."));
188 			putValue(IconHelper.XNAP_ICON, "filefind.png");
189 
190 			setEnabled(false);
191 		}
192 
193 		public void actionPerformed(ActionEvent event) 
194 		{
195 			OpenNapServer[] servers = user.getParent().getServers();
196 			for (int i = 0; i < servers.length; i++) {
197 				if (servers[i].isConnected() && !tried.contains(servers[i])) {
198 					user = servers[i].getUser(user.getName());
199 					OpenNapMetaBrowse.this.start(handler);
200 					OpenNapMetaBrowse.this.stateChanged(OpenNapMetaBrowse.this);
201 					return;
202 				}
203 			}
204 
205 			StatusBar.setText(XNap.tr("Already tried all servers!"));
206 
207 			// maybe the user has connected on a different server
208 			user.getParent().lookup();
209 		}		
210 
211 	}
212 
213 }
214