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.Component;
23  import java.awt.GridBagLayout;
24  
25  import javax.swing.JLabel;
26  import javax.swing.JPanel;
27  
28  import org.xnap.XNap;
29  import org.xnap.gui.AbstractSettingsPanel;
30  import org.xnap.gui.action.EnableAction;
31  import org.xnap.gui.component.PortPanel;
32  import org.xnap.gui.component.ValidatedTextField;
33  import org.xnap.gui.component.XNapCheckBox;
34  import org.xnap.gui.util.GUIHelper;
35  import org.xnap.gui.util.GridBagHelper;
36  import org.xnap.plugin.opennap.OpenNapPlugin;
37  import org.xnap.plugin.opennap.util.OpenNapPreferences;
38  
39  public class OpenNapPreferencesPanel extends AbstractSettingsPanel {
40  
41      //--- Data Field(s) ----
42  
43      private OpenNapPreferences napPrefs = OpenNapPlugin.getPreferences();
44  
45      private PortPanel jpPort;
46  
47      private EnableAction useAutoConnectorAction;
48      private ValidatedTextField jteMaxAutoconnectServers;
49  	private EnableAction autoResumeDownloadsAction;
50  	private ValidatedTextField autoResumeConnectCountTextField;
51  	private EnableAction limitSearchServersAction;
52  	private ValidatedTextField jteMaxSearchServers;
53      
54      //--- Constructor(s) ---
55  
56      public OpenNapPreferencesPanel()
57      {
58  		setLayout(new GridBagLayout());
59  
60  		// local port
61  		JPanel jpConnection = new JPanel(new GridBagLayout());
62  		jpConnection.setBorder
63  			(GUIHelper.createDefaultBorder(XNap.tr("Local Port")));
64  		GridBagHelper.add(this, jpConnection);
65  
66  		GridBagHelper.add(jpConnection, XNap.tr("Enable if you can receive incoming connections and set a local port."));
67  		jpPort = new PortPanel(!napPrefs.getFirewalled(),
68  							   napPrefs.getLocalPortRange(), 20);
69  
70  		GridBagHelper.add(jpConnection, jpPort, false);
71  
72  		// startup
73  		JPanel jpStartup = new JPanel(new GridBagLayout());
74  		jpStartup.setBorder(GUIHelper.createDefaultBorder
75  							(XNap.tr("Options")));
76  		GridBagHelper.add(this, jpStartup);
77  
78  		jteMaxAutoconnectServers = new ValidatedTextField
79  			(napPrefs.getMaxAutoconnectServers() + "", 5,
80  			 ValidatedTextField.NUMBERS_INT);
81  		useAutoConnectorAction 
82  			= new EnableAction
83  				(XNap.tr("On startup automatically try to connect to"),
84  				 new Component[] {},
85  				 napPrefs.getUseAutoconnector());
86  		GridBagHelper.addComponent(jpStartup, new XNapCheckBox(useAutoConnectorAction));
87  		GridBagHelper.addComponent(jpStartup, jteMaxAutoconnectServers);
88  		GridBagHelper.addLabel(jpStartup, XNap.tr("servers"));
89  		// XXX: workaroung to ensure left alignment
90  		GridBagHelper.add(jpStartup, new JLabel());
91  
92  		autoResumeConnectCountTextField = new ValidatedTextField
93  			(napPrefs.getAutoResumeConnectedCount() + "", 5,
94  			 ValidatedTextField.NUMBERS_INT);
95  		autoResumeDownloadsAction 
96  			= new EnableAction
97  				(XNap.tr("Resume downloads when connected to"),
98  				 new Component[] { autoResumeConnectCountTextField }, 
99  				 napPrefs.getAutoResumeDownloads());
100 		GridBagHelper.addComponent
101 			(jpStartup, new XNapCheckBox(autoResumeDownloadsAction));
102 		GridBagHelper.addComponent
103 			(jpStartup, autoResumeConnectCountTextField);
104 		GridBagHelper.addLabel(jpStartup, XNap.tr("servers"));
105 		// XXX: workaroung to ensure left alignment
106 		GridBagHelper.add(jpStartup, new JLabel());
107 			 
108 		// max search servers
109 		jteMaxSearchServers = new ValidatedTextField
110 			(napPrefs.getMaxSearchServers() + "", 5,
111 			 ValidatedTextField.NUMBERS_INT);
112 		limitSearchServersAction 
113 			= new EnableAction
114 				(XNap.tr("Limit number of servers to search on"),
115 						new Component[] { jteMaxSearchServers }, 
116 						napPrefs.getLimitSearchServers());
117 		GridBagHelper.addComponent
118 			(jpStartup, new XNapCheckBox(limitSearchServersAction));
119 		GridBagHelper.addComponent
120 			(jpStartup, jteMaxSearchServers);
121 		GridBagHelper.addLabel(jpStartup, XNap.tr("servers"));
122 		// XXX: workaroung to ensure left alignment
123 		GridBagHelper.add(jpStartup, new JLabel());
124 
125 		GridBagHelper.addVerticalSpacer(this);
126 		GUIHelper.setMnemonics(this);
127     }
128 
129     //--- Method(s) ---
130 
131     public void apply()
132     {
133 		napPrefs.setFirewalled(!jpPort.isSelected());
134 		napPrefs.setLocalPortRange(jpPort.getPort());
135 
136 		napPrefs.setUseAutoconnector(useAutoConnectorAction.isSelected());
137 		napPrefs.setMaxAutoconnectServers
138 		(jteMaxAutoconnectServers.getIntValue());
139 		napPrefs.setAutoResumeDownloads(autoResumeDownloadsAction.isSelected());
140 		napPrefs.setAutoResumeConnectedCount
141 			(autoResumeConnectCountTextField.getIntValue());
142 		napPrefs.setLimitSearchServers(limitSearchServersAction.isSelected());
143 		napPrefs.setMaxSearchServers(jteMaxSearchServers.getIntValue());
144     }
145 
146     public String getTitle()
147     {
148 		return "OpenNap";
149     }
150 
151 
152 }