1 package net.sourceforge.rconed.demo;
2
3 import java.awt.BorderLayout;
4 import javax.swing.JPanel;
5 import javax.swing.JFrame;
6 import java.awt.Dimension;
7
8 import javax.swing.ButtonGroup;
9 import javax.swing.JLabel;
10 import javax.swing.JTextField;
11 import javax.swing.JButton;
12 import javax.swing.SwingUtilities;
13
14 import java.awt.GridLayout;
15 import javax.swing.JTextArea;
16
17 import net.sourceforge.rconed.BF2Rcon;
18 import net.sourceforge.rconed.CODRcon;
19 import net.sourceforge.rconed.Rcon;
20 import net.sourceforge.rconed.SourceRcon;
21
22 import java.awt.Font;
23 import javax.swing.JScrollPane;
24 import java.awt.GridBagLayout;
25 import javax.swing.JRadioButton;
26 import java.awt.GridBagConstraints;
27 import javax.swing.SwingConstants;
28
29
30
31
32
33
34
35
36
37
38
39 final class Demo extends JFrame {
40
41 private static final long serialVersionUID = 1L;
42 private JPanel jContentPane = null;
43 private JPanel commandPanel = null;
44 private JLabel rconCommandLabel = null;
45 private JTextField commandField = null;
46 private JButton executeButton = null;
47 private JPanel connectionPanel = null;
48 private JLabel hostLabel = null;
49 private JTextField hostField = null;
50 private JLabel portLabel = null;
51 private JTextField portField = null;
52 private JLabel rconPasswordLabel = null;
53 private JTextField rconPasswordField = null;
54 private JTextArea resultTextArea = null;
55 private JScrollPane resultScrollPane = null;
56 private JPanel gameTypePanel = null;
57 private JRadioButton halfLifeButton = null;
58 private JRadioButton sourceButton = null;
59 private JRadioButton bf2Button = null;
60 private JRadioButton codButton = null;
61 private JPanel aboveCommandPanel = null;
62 private ButtonGroup gameTypeGroup = null;
63 private JLabel localPort = null;
64 private JTextField localPortField = null;
65
66
67
68
69
70
71 private JScrollPane getResultScrollPane() {
72 if (resultScrollPane == null) {
73 resultScrollPane = new JScrollPane();
74 resultScrollPane.setPreferredSize(new Dimension(800, 600));
75 resultScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
76 resultScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
77 resultScrollPane.setViewportView(getResultTextArea());
78 }
79 return resultScrollPane;
80 }
81
82
83
84
85
86
87 private JPanel getGameTypePanel() {
88 if (gameTypePanel == null) {
89 GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
90 gridBagConstraints3.gridx = 0;
91 gridBagConstraints3.anchor = GridBagConstraints.WEST;
92 gridBagConstraints3.gridy = 3;
93 GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
94 gridBagConstraints2.gridx = 0;
95 gridBagConstraints2.anchor = GridBagConstraints.WEST;
96 gridBagConstraints2.gridy = 2;
97 GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
98 gridBagConstraints1.gridx = 0;
99 gridBagConstraints1.anchor = GridBagConstraints.WEST;
100 gridBagConstraints1.gridy = 1;
101 GridBagConstraints gridBagConstraints = new GridBagConstraints();
102 gridBagConstraints.gridx = 0;
103 gridBagConstraints.anchor = GridBagConstraints.WEST;
104 gridBagConstraints.gridy = 0;
105 gameTypePanel = new JPanel();
106 gameTypePanel.setLayout(new GridBagLayout());
107 gameTypePanel.add(getHalfLifeButton(), gridBagConstraints);
108 gameTypePanel.add(getSourceButton(), gridBagConstraints1);
109 gameTypePanel.add(getBf2Button(), gridBagConstraints2);
110 gameTypePanel.add(getCodButton(), gridBagConstraints3);
111 getGameTypeGroup();
112 }
113 return gameTypePanel;
114 }
115
116
117
118
119
120
121 private JRadioButton getHalfLifeButton() {
122 if (halfLifeButton == null) {
123 halfLifeButton = new JRadioButton();
124 halfLifeButton.setText("Half Life");
125 halfLifeButton.setHorizontalTextPosition(SwingConstants.TRAILING);
126 halfLifeButton.setSelected(true);
127 halfLifeButton.setHorizontalAlignment(SwingConstants.LEADING);
128 halfLifeButton.addActionListener(new java.awt.event.ActionListener() {
129 public void actionPerformed(java.awt.event.ActionEvent e) {
130 portField.setText("27015");
131 }
132 });
133 }
134 return halfLifeButton;
135 }
136
137
138
139
140
141
142 private JRadioButton getSourceButton() {
143 if (sourceButton == null) {
144 sourceButton = new JRadioButton();
145 sourceButton.setText("Half Life 2 (Source)");
146 sourceButton.addActionListener(new java.awt.event.ActionListener() {
147 public void actionPerformed(java.awt.event.ActionEvent e) {
148 portField.setText("27015");
149 }
150 });
151 }
152 return sourceButton;
153 }
154
155
156
157
158
159
160 private JRadioButton getBf2Button() {
161 if (bf2Button == null) {
162 bf2Button = new JRadioButton();
163 bf2Button.setText("Battlefield 2");
164 bf2Button.addActionListener(new java.awt.event.ActionListener() {
165 public void actionPerformed(java.awt.event.ActionEvent e) {
166 portField.setText("4711");
167 }
168 });
169 }
170 return bf2Button;
171 }
172
173
174
175
176
177
178 private JRadioButton getCodButton() {
179 if (codButton == null) {
180 codButton = new JRadioButton();
181 codButton.setText("Call of Duty");
182 codButton.addActionListener(new java.awt.event.ActionListener() {
183 public void actionPerformed(java.awt.event.ActionEvent e) {
184 portField.setText("28960");
185 }
186 });
187 }
188 return codButton;
189 }
190
191
192
193
194
195
196 private ButtonGroup getGameTypeGroup() {
197 if (gameTypeGroup == null) {
198 gameTypeGroup = new ButtonGroup();
199 gameTypeGroup.add(getHalfLifeButton());
200 gameTypeGroup.add(getSourceButton());
201 gameTypeGroup.add(getBf2Button());
202 gameTypeGroup.add(getCodButton());
203 }
204 return gameTypeGroup;
205 }
206
207
208
209
210
211
212 private JPanel getAboveCommandPanel() {
213 if (aboveCommandPanel == null) {
214 aboveCommandPanel = new JPanel();
215 aboveCommandPanel.setLayout(new BorderLayout());
216 aboveCommandPanel.add(getGameTypePanel(), BorderLayout.EAST);
217 aboveCommandPanel.add(getConnectionPanel(), BorderLayout.CENTER);
218 }
219 return aboveCommandPanel;
220 }
221
222
223
224
225
226
227 private JTextField getLocalPortField() {
228 if (localPortField == null) {
229 localPortField = new JTextField();
230 localPortField.setText("-1");
231 }
232 return localPortField;
233 }
234
235
236
237
238 public static void main(String[] args) {
239 SwingUtilities.invokeLater(new Runnable() {
240 public void run() {
241 Demo thisClass = new Demo();
242 thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
243 thisClass.setVisible(true);
244 }
245 });
246 }
247
248
249
250
251
252
253 private JPanel getCommandPanel() {
254 if (commandPanel == null) {
255 rconCommandLabel = new JLabel();
256 rconCommandLabel.setText("Rcon command");
257 commandPanel = new JPanel();
258 commandPanel.setLayout(new BorderLayout());
259 commandPanel.add(rconCommandLabel, BorderLayout.WEST);
260 commandPanel.add(getCommandField(), BorderLayout.CENTER);
261 commandPanel.add(getExecuteButton(), BorderLayout.EAST);
262 commandPanel.add(getAboveCommandPanel(), BorderLayout.NORTH);
263 }
264 return commandPanel;
265 }
266
267
268
269
270
271
272 private JTextField getCommandField() {
273 if (commandField == null) {
274 commandField = new JTextField();
275 commandField.addActionListener(new java.awt.event.ActionListener() {
276 public void actionPerformed(java.awt.event.ActionEvent e) {
277 execute();
278 }
279 });
280 }
281 return commandField;
282 }
283
284
285
286
287
288
289 private JButton getExecuteButton() {
290 if (executeButton == null) {
291 executeButton = new JButton();
292 executeButton.setText("Execute");
293 executeButton.addActionListener(new java.awt.event.ActionListener() {
294 public void actionPerformed(java.awt.event.ActionEvent e) {
295 execute();
296 }
297 });
298 }
299 return executeButton;
300 }
301
302
303
304
305
306
307 private JPanel getConnectionPanel() {
308 if (connectionPanel == null) {
309 localPort = new JLabel();
310 localPort.setText("Local Port (leave -1 for auto)");
311 rconPasswordLabel = new JLabel();
312 rconPasswordLabel.setText("Rcon Password");
313 portLabel = new JLabel();
314 portLabel.setText("Port");
315 hostLabel = new JLabel();
316 hostLabel.setText("Host");
317 GridLayout gridLayout = new GridLayout();
318 gridLayout.setRows(4);
319 gridLayout.setColumns(2);
320 connectionPanel = new JPanel();
321 connectionPanel.setLayout(gridLayout);
322 connectionPanel.add(localPort, null);
323 connectionPanel.add(getLocalPortField(), null);
324 connectionPanel.add(hostLabel, null);
325 connectionPanel.add(getHostField(), null);
326 connectionPanel.add(portLabel, null);
327 connectionPanel.add(getPortField(), null);
328 connectionPanel.add(rconPasswordLabel, null);
329 connectionPanel.add(getRconPasswordField(), null);
330 }
331 return connectionPanel;
332 }
333
334
335
336
337
338
339 private JTextField getHostField() {
340 if (hostField == null) {
341 hostField = new JTextField();
342 hostField.setText("localhost");
343 }
344 return hostField;
345 }
346
347
348
349
350
351
352 private JTextField getPortField() {
353 if (portField == null) {
354 portField = new JTextField();
355 portField.setText("27015");
356 }
357 return portField;
358 }
359
360
361
362
363
364
365 private JTextField getRconPasswordField() {
366 if (rconPasswordField == null) {
367 rconPasswordField = new JTextField();
368 }
369 return rconPasswordField;
370 }
371
372
373
374
375
376
377 private JTextArea getResultTextArea() {
378 if (resultTextArea == null) {
379 resultTextArea = new JTextArea();
380 resultTextArea.setFont(new Font("Courier New", Font.PLAIN, 12));
381 resultTextArea.setEditable(false);
382 resultTextArea.setPreferredSize(new Dimension(800, 600));
383 }
384 return resultTextArea;
385 }
386
387
388
389
390 private Demo() {
391 super();
392 initialize();
393 }
394
395
396
397
398
399
400 private void initialize() {
401 this.setSize(800, 600);
402 this.setContentPane(getJContentPane());
403 this.setTitle("RconEd Tester");
404 }
405
406
407
408
409
410
411 private JPanel getJContentPane() {
412 if (jContentPane == null) {
413 jContentPane = new JPanel();
414 jContentPane.setLayout(new BorderLayout());
415 jContentPane.add(getCommandPanel(), BorderLayout.NORTH);
416 jContentPane.add(getResultScrollPane(), BorderLayout.CENTER);
417 }
418 return jContentPane;
419 }
420
421
422
423
424
425
426 private void execute() {
427 try {
428
429
430 String stringShow = null;
431 int localPort = -1;
432 if (localPortField.getText().length() > 0) {
433 localPort = Integer.parseInt(localPortField.getText());
434 }
435 String ipStr = hostField.getText();
436 int port = Integer.parseInt(portField.getText());
437 String password = rconPasswordField.getText();
438 String command = commandField.getText();
439
440
441
442 if (halfLifeButton.isSelected()) {
443 if (localPort == -1) {
444 throw new Exception("Half life server requires a local port to be specified");
445 }
446
447 stringShow = Rcon.send(localPort, ipStr, port, password, command);
448 } else if (sourceButton.isSelected()) {
449 if (localPort == -1) {
450
451 stringShow = SourceRcon.send(ipStr, port, password, command);
452 } else {
453
454 stringShow = SourceRcon.send(ipStr, port, password, command, localPort);
455 }
456 } else if (bf2Button.isSelected()) {
457 if (localPort == -1) {
458
459 stringShow = BF2Rcon.send(ipStr, port, password, command);
460 } else {
461
462 stringShow = BF2Rcon.send(ipStr, port, password, command, localPort);
463 }
464 } else if (codButton.isSelected()) {
465 if (localPort == -1) {
466
467 stringShow = CODRcon.send(ipStr, port, password, command);
468 } else {
469
470 stringShow = CODRcon.send(localPort, ipStr, port, password, command);
471 }
472 }
473
474 resultTextArea.setText(stringShow);
475
476 commandField.grabFocus();
477 } catch (Exception e) {
478
479 resultTextArea.setText(e.toString() + "\n" + e.getLocalizedMessage());
480 }
481 }
482
483 }