1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui.component;
21
22 import java.util.StringTokenizer;
23
24 import javax.swing.JLabel;
25
26 import org.xnap.XNap;
27 import org.xnap.gui.util.GUIHelper;
28 import org.xnap.util.StringHelper;
29
30 /***
31 *
32 */
33 public class HostList extends AbstractListPanel {
34
35
36
37 private ValidatedTextField jtfHost;
38 private ValidatedTextField jtfPort;
39
40
41
42 public HostList(int visibleRows, String[] hosts)
43 {
44 super(visibleRows);
45
46 getInputBox().add(new JLabel(XNap.tr("Host", 0, 1)), 0);
47 jtfHost = new ValidatedTextField("", 20, StringHelper.HOST);
48 GUIHelper.bindEnterKey(jtfHost, getAddAction());
49 getInputBox().add(jtfHost, 1);
50
51 getInputBox().add(new JLabel(XNap.tr("Port", 1)), 2);
52 jtfPort = new ValidatedTextField("", 5, StringHelper.NUMBERS_INT);
53 GUIHelper.bindEnterKey(jtfPort, getAddAction());
54 getInputBox().add(jtfPort, 3);
55
56 if (hosts != null) {
57 addItems(hosts);
58 }
59 }
60
61
62
63 /***
64 * Returns the current host.
65 */
66 public String getHost()
67 {
68 return jtfHost.getText();
69 }
70
71 public int getPort()
72 {
73 return jtfPort.getIntValue();
74 }
75
76 /***
77 * Returns the current host:port as a string.
78 *
79 * @return null, if no valid host is set
80 */
81 protected Object getInput()
82 {
83 String host = getHost();
84 int port = getPort();
85
86 return (host.length() > 0 && port != 0) ? host + ":" + port : null;
87 }
88
89 protected void setInput(Object item)
90 {
91 if (item != null) {
92 StringTokenizer t = new StringTokenizer(item.toString(), ":");
93 jtfHost.setText(t.nextToken());
94 jtfPort.setText(t.nextToken());
95 }
96 else {
97 jtfHost.setText("");
98 jtfPort.setText("");
99 }
100 }
101
102 }