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.gui;
21  
22  import java.awt.GridBagLayout;
23  
24  import javax.swing.DefaultComboBoxModel;
25  import javax.swing.JComboBox;
26  import javax.swing.JComponent;
27  import javax.swing.JPanel;
28  
29  import org.xnap.XNap;
30  import org.xnap.gui.SearchOptionsPanel;
31  import org.xnap.gui.component.RangeBox;
32  import org.xnap.gui.util.GUIHelper;
33  import org.xnap.gui.util.GridBagHelper;
34  import org.xnap.plugin.opennap.net.OpenNapServer;
35  import org.xnap.search.SearchFilter;
36  
37  /***
38   * 
39   */
40  public class OpenNapSearchOptionsPanel extends JPanel 
41  	implements SearchOptionsPanel {
42  
43      // --- Data Field(s) ---
44  
45  	private RangeBox rbBitrate;
46  	private DefaultComboBoxModel cbmServers;
47  	private JComboBox jcbServers;
48  
49      // --- Constructor(s) ---
50  
51      public OpenNapSearchOptionsPanel() 
52      {
53  		setLayout(new GridBagLayout());
54  		setBorder(GUIHelper.createDefaultBorder(XNap.tr("OpenNap")));
55  		
56  		GridBagHelper.addLabel(this, XNap.tr("Server"));
57  		cbmServers = new DefaultComboBoxModel();
58  		cbmServers.addElement(XNap.tr("All"));
59  		jcbServers = new JComboBox(cbmServers);
60  		GridBagHelper.add(this, jcbServers);
61  		
62  		GridBagHelper.addLabel(this, XNap.tr("Bitrate"));
63  		rbBitrate = new RangeBox();
64  		GridBagHelper.add(this, rbBitrate);
65      }
66  
67      // --- Method(s) ---
68  
69  	/***
70  	 *  @see xnap.gui.SearchOptionsPanel#getComponent()
71  	 */
72  	public JComponent getComponent() 
73  	{
74  		return this;
75  	}
76  
77  	/***
78  	 *  @see xnap.gui.SearchOptionsPanel#setOptions(xnap.search.SearchFilter)
79  	 */
80  	public void setOptions(SearchFilter filter) 
81  	{
82  		if (cbmServers.getSelectedItem() instanceof OpenNapServer) {
83  			filter.put("nap.server", cbmServers.getSelectedItem());
84  		}
85  
86  		filter.put("nap.minBitrate", 
87  				   (rbBitrate.getMinValue() != -1)
88  				   ? new Long(rbBitrate.getMinValue())
89  					   : null);
90  		filter.put("nap.maxBitrate", 
91  				   (rbBitrate.getMaxValue() != -1) 
92  				   ? new Long(rbBitrate.getMaxValue())
93  					   : null);
94  
95  	}
96  
97  	/***
98  	 *  @see xnap.gui.SearchOptionsPanel#setSearchFilter(xnap.search.SearchFilter)
99  	 */
100 	public void setSearchFilter(SearchFilter filter) 
101 	{
102 		Integer min = (Integer)filter.get("nap.minBitrate");
103 		Integer max = (Integer)filter.get("nap.minBitrate");
104 		rbBitrate.setValues((min != null) ? min.intValue() : -1,
105 					  	(max != null) ? max.intValue() : -1);
106 			
107 		OpenNapServer s = (OpenNapServer)filter.get("nap.server");
108 		if (s != null) {
109 			jcbServers.setSelectedItem(s);
110 		}
111 		else {
112 			// select "All"
113 			jcbServers.setSelectedIndex(0);
114 		}
115 	}
116 
117 }