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.ButtonGroup;
26  import javax.swing.DefaultComboBoxModel;
27  import javax.swing.JCheckBox;
28  import javax.swing.JComboBox;
29  import javax.swing.JPanel;
30  import javax.swing.JRadioButton;
31  import javax.swing.JTextField;
32  
33  import org.xnap.XNap;
34  import org.xnap.gui.action.EnableAction;
35  import org.xnap.gui.component.DefaultDialog;
36  import org.xnap.gui.component.ValidatedTextField;
37  import org.xnap.gui.component.XNapCheckBox;
38  import org.xnap.gui.util.GUIHelper;
39  import org.xnap.gui.util.GridBagHelper;
40  import org.xnap.plugin.opennap.OpenNapPlugin;
41  import org.xnap.plugin.opennap.net.OpenNapServer;
42  import org.xnap.util.*;
43  import org.xnap.util.prefs.PortRangeValidator;
44  
45  /***
46   * Provides a dialog for editing properties of a {@link OpenNapServer} object.
47   */
48  public class OpenNapServerEditorDialog extends DefaultDialog {
49      
50      //--- Data field(s) ---
51  
52      private OpenNapServer server;
53  
54      private JTextField jtHost;
55      private ValidatedTextField jtPort;
56      private JComboBox jcbNetwork;
57      private JTextField jtNetwork;
58      private JCheckBox jcbCustomizeUser;
59      private JTextField jtNick;
60      private JTextField jtPassword;
61      private JTextField jtEmail;
62      private JCheckBox jcbOpenNapigator;
63      private JCheckBox jcbAutoConnect;
64      private JRadioButton jrbTypeDefault;
65      private JRadioButton jrbTypeRedirector;
66      private JTextField jtAutoJoin;
67      
68      /***
69       * If set to true <code>server</code> will be added to 
70       * {@link OpenNapServerManager} if okay is pressed.
71       */
72      private boolean addToConnector;
73  
74      //--- Constructor(s) ---
75     
76      /***
77       *
78       * @param addToConnector <code>server</code> will be added to 
79       *                       {@link OpenNapServerManager} if okay is pressed
80       */
81      public OpenNapServerEditorDialog(OpenNapServer server, boolean addToConnector)
82      {
83  		this.server = server;
84  		this.addToConnector = addToConnector;
85  
86  		initialize();
87      }
88  
89      public OpenNapServerEditorDialog(OpenNapServer server)
90      {
91  		this(server, false);
92      }
93  
94      /***
95       * Creates a dialog with a new {@link OpenNapServer} object that is added
96       * to {@link OpenNapServerManager} if okay is pressed.
97       *
98       * @see #OpenNapServerEditorDialog(OpenNapServer, boolean)
99       */
100     public OpenNapServerEditorDialog()
101     {
102 		this(new OpenNapServer(), true);
103     }
104 
105     // --- Methods ---
106 
107     private void initialize()
108     {
109         setTitle(XNap.tr("Server"));
110 
111 		JPanel jp = getMainPanel();
112 		jp.setLayout(new GridBagLayout());
113 
114 		// host
115 		GridBagHelper.addLabel(jp, XNap.tr("Host"));
116 		jtHost = new JTextField(server.getHost(), 20);
117 		GridBagHelper.add(jp, jtHost);
118 
119 		// port
120 		GridBagHelper.addLabel(jp, XNap.tr("Port"));
121 		jtPort = new ValidatedTextField
122 			(server.getPort() + "", 5, ValidatedTextField.NUMBERS_INT);
123 		GridBagHelper.add(jp, jtPort, false);
124 		jtPort.setMinimumSize(jtPort.getPreferredSize());
125 	
126 		// network
127 		GridBagHelper.addLabel(jp, XNap.tr("Network"));
128 		jcbNetwork = new JComboBox();
129 		jcbNetwork.setEditable(true);
130 		jtNetwork = (JTextField)jcbNetwork.getEditor().getEditorComponent();
131 		jtNetwork.setText(server.getNetworkName());
132 		GridBagHelper.add(jp, jcbNetwork);
133 
134 		// type
135 		JPanel jpType = new JPanel(new GridBagLayout());
136 		jpType.setBorder(GUIHelper.createDefaultBorder(XNap.tr("Type")));
137 		GridBagHelper.add(jp, jpType);
138 	
139 		ButtonGroup bgType = new ButtonGroup();
140 
141 		jrbTypeDefault 
142 			= new JRadioButton(XNap.tr("Default"), !server.isRedirector());
143 		bgType.add(jrbTypeDefault);
144 		GridBagHelper.addComponent(jpType, jrbTypeDefault);
145 
146 		jrbTypeRedirector
147 			= new JRadioButton(XNap.tr("Redirector"), server.isRedirector());
148 		bgType.add(jrbTypeRedirector);
149 		GridBagHelper.add(jpType, jrbTypeRedirector);
150 
151 		// user
152 		JPanel jpUser = new JPanel(new GridBagLayout());
153 		jpUser.setBorder(GUIHelper.createDefaultBorder(XNap.tr("Login")));
154 
155 		boolean custom = server.isLoginCustomized();
156 		jtNick = new JTextField(server.getNick(), 20);
157 		jtPassword = new JTextField(server.getPassword(), 20);
158 		jtEmail = new JTextField(server.getEmail(), 20);
159 
160 		Component[] c = new Component[] { jtNick, jtPassword, jtEmail };
161 		EnableAction ac 
162 			= new EnableAction(XNap.tr("Customize", 1, 1), c, custom);
163 		jcbCustomizeUser = new XNapCheckBox(ac);
164 		GridBagHelper.add(jpUser, jcbCustomizeUser);
165 
166 		GridBagHelper.addLabel(jpUser, XNap.tr("Nickname"));
167 		GridBagHelper.add(jpUser, jtNick);
168 
169 		GridBagHelper.addLabel(jpUser, XNap.tr("Password"));
170 		GridBagHelper.add(jpUser, jtPassword);
171 
172 		GridBagHelper.addLabel(jpUser, XNap.tr("Email"));
173 		GridBagHelper.add(jpUser, jtEmail);
174 
175 		GridBagHelper.add(jp, jpUser);
176 
177 		// auto join
178 		GridBagHelper.addLabel(jp, XNap.tr("Auto Join"));
179 		jtAutoJoin = new JTextField(server.getAutoJoinChannels(), 20);
180 		GridBagHelper.add(jp, jtAutoJoin);
181 
182 		// napigator
183 		jcbOpenNapigator = new JCheckBox(XNap.tr("Napigator Server"));
184 		jcbOpenNapigator.setSelected(server.isTemporary());
185 		GridBagHelper.add(jp, jcbOpenNapigator);
186 
187 		jcbAutoConnect = new JCheckBox(XNap.tr("Connect Automatically"));
188 		jcbAutoConnect.setSelected(server.getAutoConnect());
189 		GridBagHelper.add(jp, jcbAutoConnect);
190 
191 		pack();
192     }
193 
194     public boolean apply()
195     {
196 		// validate input
197 
198 		if (jtHost.getText().trim().length() == 0) {
199 			throw new IllegalArgumentException("Please provide a hostname.");
200 		}
201 	
202 		int port = jtPort.getIntValue();
203 		PortRangeValidator.DEFAULT.check(port);
204 	
205 		// set properties
206 
207 		server.setHost(jtHost.getText());
208 		server.setPort(port);
209 
210 		server.setRedirector(jrbTypeRedirector.isSelected());
211 
212 		boolean custom = jcbCustomizeUser.isSelected();
213 		server.setNick(custom ? jtNick.getText() : null);
214 		server.setPassword(custom ? jtPassword.getText() : null);
215 		server.setEmail(custom ? jtEmail.getText() : null);
216 
217 		server.setTemporary(jcbOpenNapigator.isSelected());
218 		
219 		boolean oldAutoConnectState = server.getAutoConnect();
220 		server.setAutoConnect(jcbAutoConnect.isSelected());
221 
222 		server.setAutoJoinChannels(jtAutoJoin.getText());
223 
224 		if (addToConnector) {
225 			server.setNetworkName(jtNetwork.getText());
226 			OpenNapPlugin.getServerManager().add
227 				(server, server.getAutoConnect());
228 			addToConnector = false;
229 		}
230 		else {
231 			OpenNapPlugin.getServerManager().changeNetwork
232 				(server, jtNetwork.getText());
233 			if (server.getAutoConnect() && !oldAutoConnectState) {
234 				try {
235 					server.connect();
236 				}
237 				catch (IllegalOperationException e) {
238 				}
239 			}
240 		}
241 		return true;
242     }
243 
244     public void setNetworks(String[] networks)
245     {
246 		// save current input
247 		String network = jtNetwork.getText();
248 
249 		DefaultComboBoxModel dcbm = new DefaultComboBoxModel(networks);
250 		jcbNetwork.setModel(dcbm);
251 
252 		// restore
253 		jtNetwork.setText(network);
254     }
255 
256 }