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.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      //--- Data field(s) ---
36  
37      private ValidatedTextField jtfHost;
38      private ValidatedTextField jtfPort;
39  
40      //--- Constructor(s) ---
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      // --- Method(s) ---
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 }