View Javadoc
1 /***
2 * Copyright (c) 2003 held jointly by the individual authors.
3 *
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; with out even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17 *
18 * > http://www.gnu.org/copyleft/lesser.html
19 * > http://www.opensource.org/licenses/lgpl-license.php
20 */
21 package net.mlw.gfw.ext.basic.client.component;
22
23 import javax.swing.ButtonGroup;
24 import javax.swing.JButton;
25 import javax.swing.JDialog;
26 import javax.swing.JFrame;
27 import javax.swing.JLabel;
28 import javax.swing.JPanel;
29 import javax.swing.JRadioButton;
30 import javax.swing.JTextField;
31 import javax.swing.SwingConstants;
32 import javax.swing.border.TitledBorder;
33 import javax.swing.event.CaretEvent;
34 import javax.swing.event.CaretListener;
35
36 import net.mlw.gfw.Constants;
37
38 /***
39 *
40 * @author mwilson
41 */
42 public class ServerOptionsPanel extends JPanel
43 {
44 /*** Name of the remote Server. **/
45 private final JTextField rname = new JTextField(Constants.DEFAULT_HOST);
46 /*** The remote Server port. **/
47 private final JTextField rport = new JTextField(String.valueOf(Constants.DEFAULT_PORT));
48
49 /*** The local Server port. **/
50 private final JTextField lport = new JTextField(String.valueOf(Constants.DEFAULT_PORT));
51
52 /*** The username. **/
53 private final JTextField username = new JTextField();
54
55 private final JButton ok = new JButton(Constants.RESOURCE_BUNDLE.getString("button.ok"));
56 private final JButton quit = new JButton(Constants.RESOURCE_BUNDLE.getString("button.quit"));
57
58 private int mode = Constants.SERVER_NONE;
59
60 /*** Creates new form ServerOptionsPanel */
61 public ServerOptionsPanel()
62 {
63 initComponents();
64 }
65
66 private void initComponents()
67 {
68 java.awt.GridBagConstraints gridBagConstraints;
69
70 final ButtonGroup buttonGroup1 = new ButtonGroup();
71 final JPanel jPanel1 = new JPanel();
72 final JPanel jPanel2 = new JPanel();
73 final JLabel jLabel2 = new JLabel();
74 final JPanel jPanel3 = new JPanel();
75 final JLabel jLabel3 = new JLabel();
76 final JLabel jLabel4 = new JLabel();
77 final JRadioButton none = new JRadioButton();
78 final JRadioButton local = new JRadioButton();
79 final JRadioButton remote = new JRadioButton();
80 final JLabel jLabel1 = new JLabel();
81 final JPanel jPanel4 = new JPanel();
82
83 setLayout(new java.awt.GridBagLayout());
84
85 jPanel1.setBorder(new TitledBorder(Constants.RESOURCE_BUNDLE.getString("border.server.none")));
86 gridBagConstraints = new java.awt.GridBagConstraints();
87 gridBagConstraints.gridx = 1;
88 gridBagConstraints.gridy = 2;
89 gridBagConstraints.gridwidth = 2;
90 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
91 gridBagConstraints.ipadx = 2;
92 gridBagConstraints.ipady = 2;
93 gridBagConstraints.weightx = 0.1;
94 gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
95 add(jPanel1, gridBagConstraints);
96
97 jPanel2.setLayout(new java.awt.GridBagLayout());
98
99 jPanel2.setBorder(new TitledBorder(Constants.RESOURCE_BUNDLE.getString("border.server.local")));
100 jLabel2.setHorizontalAlignment(SwingConstants.RIGHT);
101 jLabel2.setText(Constants.RESOURCE_BUNDLE.getString("server.port"));
102 gridBagConstraints = new java.awt.GridBagConstraints();
103 gridBagConstraints.gridx = 0;
104 gridBagConstraints.gridy = 0;
105 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
106 gridBagConstraints.ipadx = 6;
107 gridBagConstraints.ipady = 6;
108 gridBagConstraints.weightx = 0.25;
109 jPanel2.add(jLabel2, gridBagConstraints);
110
111 lport.setColumns(10);
112 lport.setEditable(false);
113 gridBagConstraints = new java.awt.GridBagConstraints();
114 gridBagConstraints.gridx = 1;
115 gridBagConstraints.gridy = 0;
116 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
117 gridBagConstraints.ipadx = 6;
118 gridBagConstraints.ipady = 6;
119 gridBagConstraints.weightx = 0.75;
120 jPanel2.add(lport, gridBagConstraints);
121
122 gridBagConstraints = new java.awt.GridBagConstraints();
123 gridBagConstraints.gridx = 1;
124 gridBagConstraints.gridy = 3;
125 gridBagConstraints.gridwidth = 2;
126 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
127 gridBagConstraints.ipadx = 2;
128 gridBagConstraints.ipady = 2;
129 gridBagConstraints.weightx = 0.1;
130 gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
131 add(jPanel2, gridBagConstraints);
132
133 jPanel3.setLayout(new java.awt.GridBagLayout());
134
135 jPanel3.setBorder(new TitledBorder(Constants.RESOURCE_BUNDLE.getString("border.server.remote")));
136 jLabel3.setHorizontalAlignment(SwingConstants.RIGHT);
137 jLabel3.setText(Constants.RESOURCE_BUNDLE.getString("server.ip"));
138 gridBagConstraints = new java.awt.GridBagConstraints();
139 gridBagConstraints.ipadx = 6;
140 gridBagConstraints.ipady = 6;
141 gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
142 gridBagConstraints.weightx = 0.25;
143 jPanel3.add(jLabel3, gridBagConstraints);
144
145 rname.setColumns(10);
146 rname.setEditable(false);
147 gridBagConstraints = new java.awt.GridBagConstraints();
148 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
149 gridBagConstraints.ipadx = 6;
150 gridBagConstraints.ipady = 6;
151 gridBagConstraints.weightx = 0.75;
152 jPanel3.add(rname, gridBagConstraints);
153
154 jLabel4.setHorizontalAlignment(SwingConstants.RIGHT);
155 jLabel4.setText(Constants.RESOURCE_BUNDLE.getString("server.port"));
156 gridBagConstraints = new java.awt.GridBagConstraints();
157 gridBagConstraints.gridx = 0;
158 gridBagConstraints.gridy = 1;
159 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
160 gridBagConstraints.ipadx = 6;
161 gridBagConstraints.ipady = 6;
162 gridBagConstraints.weightx = 0.25;
163 jPanel3.add(jLabel4, gridBagConstraints);
164
165 rport.setColumns(10);
166 rport.setEditable(false);
167 gridBagConstraints = new java.awt.GridBagConstraints();
168 gridBagConstraints.gridx = 1;
169 gridBagConstraints.gridy = 1;
170 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
171 gridBagConstraints.ipadx = 6;
172 gridBagConstraints.ipady = 6;
173 gridBagConstraints.weightx = 0.75;
174 jPanel3.add(rport, gridBagConstraints);
175
176 gridBagConstraints = new java.awt.GridBagConstraints();
177 gridBagConstraints.gridx = 1;
178 gridBagConstraints.gridy = 4;
179 gridBagConstraints.gridwidth = 2;
180 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
181 gridBagConstraints.ipadx = 2;
182 gridBagConstraints.ipady = 2;
183 gridBagConstraints.weightx = 0.1;
184 gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
185 add(jPanel3, gridBagConstraints);
186
187 none.setSelected(true);
188 buttonGroup1.add(none);
189 none.addActionListener(new java.awt.event.ActionListener()
190 {
191 public void actionPerformed(java.awt.event.ActionEvent evt)
192 {
193 lport.setEditable(false);
194 rport.setEditable(false);
195 rname.setEditable(false);
196 mode = Constants.SERVER_NONE;
197 }
198 });
199
200 gridBagConstraints = new java.awt.GridBagConstraints();
201 gridBagConstraints.gridx = 0;
202 gridBagConstraints.gridy = 2;
203 gridBagConstraints.ipadx = 2;
204 gridBagConstraints.ipady = 2;
205 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
206 gridBagConstraints.weightx = 0.1;
207 gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
208 add(none, gridBagConstraints);
209
210 buttonGroup1.add(local);
211 local.addActionListener(new java.awt.event.ActionListener()
212 {
213 public void actionPerformed(java.awt.event.ActionEvent evt)
214 {
215 lport.setEditable(true);
216 rport.setEditable(false);
217 rname.setEditable(false);
218 mode = Constants.SERVER_LOCAL;
219 }
220 });
221
222 gridBagConstraints = new java.awt.GridBagConstraints();
223 gridBagConstraints.gridx = 0;
224 gridBagConstraints.gridy = 3;
225 gridBagConstraints.ipadx = 2;
226 gridBagConstraints.ipady = 2;
227 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
228 gridBagConstraints.weightx = 0.1;
229 gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
230 add(local, gridBagConstraints);
231
232 buttonGroup1.add(remote);
233 remote.addActionListener(new java.awt.event.ActionListener()
234 {
235 public void actionPerformed(java.awt.event.ActionEvent evt)
236 {
237 lport.setEditable(false);
238 rport.setEditable(true);
239 rname.setEditable(true);
240 mode = Constants.SERVER_REMOTE;
241 }
242 });
243
244 gridBagConstraints = new java.awt.GridBagConstraints();
245 gridBagConstraints.gridx = 0;
246 gridBagConstraints.gridy = 4;
247 gridBagConstraints.ipadx = 2;
248 gridBagConstraints.ipady = 2;
249 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
250 gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
251 add(remote, gridBagConstraints);
252
253 jLabel1.setText(Constants.RESOURCE_BUNDLE.getString("server.user"));
254 gridBagConstraints = new java.awt.GridBagConstraints();
255 gridBagConstraints.gridx = 0;
256 gridBagConstraints.gridy = 0;
257 gridBagConstraints.gridwidth = 2;
258 gridBagConstraints.ipadx = 2;
259 gridBagConstraints.ipady = 2;
260 gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
261 gridBagConstraints.weightx = 0.1;
262 gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
263 add(jLabel1, gridBagConstraints);
264
265 gridBagConstraints = new java.awt.GridBagConstraints();
266 gridBagConstraints.gridx = 2;
267 gridBagConstraints.gridy = 0;
268 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
269 gridBagConstraints.ipadx = 2;
270 gridBagConstraints.ipady = 2;
271 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
272 gridBagConstraints.weightx = 0.5;
273 gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
274 add(username, gridBagConstraints);
275
276 username.addCaretListener(new CaretListener()
277 {
278 public void caretUpdate(CaretEvent e)
279 {
280 ok.setEnabled(username.getText().length() > 0);
281 }
282 });
283
284 try
285 {
286 username.setText(System.getProperty("user.name"));
287 }
288 catch (Exception e)
289 {
290 ok.setEnabled(false);
291 }
292
293 jPanel4.add(ok);
294 jPanel4.add(quit);
295
296 gridBagConstraints = new java.awt.GridBagConstraints();
297 gridBagConstraints.gridx = 1;
298 gridBagConstraints.gridy = 5;
299 gridBagConstraints.gridwidth = 2;
300 gridBagConstraints.ipadx = 2;
301 gridBagConstraints.ipady = 2;
302 gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
303 gridBagConstraints.weightx = 0.1;
304 gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
305 add(jPanel4, gridBagConstraints);
306
307 } //GEN-END:initComponents
308
309 public static ServerOptionsPanel prompt(JFrame owner)
310 {
311 final ServerOptionsPanel options = new ServerOptionsPanel();
312
313 final JDialog dialog = new JDialog(owner, "New Game", true);
314 options.ok.addActionListener(new java.awt.event.ActionListener()
315 {
316 public void actionPerformed(java.awt.event.ActionEvent evt)
317 {
318 dialog.setVisible(false);
319 }
320 });
321
322 dialog.getContentPane().add(options);
323 dialog.pack();
324 dialog.setLocationRelativeTo(owner);
325 dialog.setVisible(true);
326
327 return options;
328 }
329
330 /***
331 * @return Returns the mode.
332 */
333 public int getMode()
334 {
335 return mode;
336 }
337
338 public int getPort()
339 {
340 try
341 {
342 return Integer.parseInt(mode == Constants.SERVER_LOCAL ? lport.getText() : rport.getText());
343 }
344 catch (NumberFormatException e)
345 {
346 return Constants.DEFAULT_PORT;
347 }
348 }
349
350 public String getHost()
351 {
352 return rname.getText();
353 }
354
355 public String getUserName()
356 {
357 return username.getText();
358 }
359
360 }
This page was automatically generated by Maven