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.jtella;
21  
22  import java.awt.BorderLayout;
23  import java.awt.GridBagLayout;
24  import java.awt.event.ActionEvent;
25  
26  import javax.swing.AbstractAction;
27  import javax.swing.Action;
28  import javax.swing.JButton;
29  import javax.swing.JPanel;
30  import javax.swing.JScrollPane;
31  import javax.swing.JTable;
32  import javax.swing.JTextField;
33  
34  import org.xnap.XNap;
35  import org.xnap.gui.ActionProvider;
36  import org.xnap.gui.component.ValidatedTextField;
37  import org.xnap.gui.component.XNapButton;
38  import org.xnap.gui.util.GUIHelper;
39  import org.xnap.gui.util.GridBagHelper;
40  import org.xnap.gui.util.IconHelper;
41  import org.xnap.util.StringHelper;
42  
43  import com.kenmccrary.jtella.GNUTellaConnection;
44  
45  public class JTellaPanel extends JPanel implements ActionProvider {
46  
47      // --- Data Field(s) ---
48  
49      private JTellaConnectionTableModel tmConnections;
50      private JTable jtConnections;
51  
52      private JTextField jtfHost;
53      private ValidatedTextField vtfPort;
54  
55      private ConnectAction acConnect = new ConnectAction();
56      private DisconnectAction acDisconnect = new DisconnectAction();
57  
58      // --- Constructor(s) ---
59  
60      public JTellaPanel() 
61      {
62          setLayout(new GridBagLayout());
63  
64  	// connections
65  	JPanel jpConnections = new JPanel(new BorderLayout());
66  	jpConnections.setBorder(GUIHelper.createDefaultBorder(XNap.tr("Connections")));
67  	GridBagHelper.addPanel(this, jpConnections);
68  
69  	tmConnections = new JTellaConnectionTableModel();
70  	jtConnections = tmConnections.createTable
71  	    (JTellaPlugin.getPreferences(), "connection");
72  	jpConnections.add(new JScrollPane(jtConnections), BorderLayout.CENTER);
73  
74  	// add servant Panel
75  	JPanel jpConnect = new JPanel(new GridBagLayout());
76  	GridBagHelper.add(this, jpConnect);
77  
78  	Action acAdd = new AddServantAction();
79  
80  	GridBagHelper.addLabel(jpConnect, XNap.tr("Host"));
81  	jtfHost = new JTextField("localhost", 15);
82  	GUIHelper.bindEnterKey(jtfHost, acAdd);
83  	GridBagHelper.addComponent(jpConnect, jtfHost);
84  
85  	GridBagHelper.addLabel(jpConnect, XNap.tr("Port"));
86  	vtfPort = new ValidatedTextField("6349", 5, StringHelper.NUMBERS_INT);
87  	GUIHelper.bindEnterKey(vtfPort, acAdd);
88  	GridBagHelper.addComponent(jpConnect, vtfPort);
89  
90  	GridBagHelper.addLabel(jpConnect, "");
91  	JButton jbAdd = new XNapButton(acAdd);
92  	GridBagHelper.addComponent(jpConnect, jbAdd);
93  
94  	// button
95  	GridBagHelper.addComponent(this, new XNapButton(acConnect));
96  	GridBagHelper.addComponent(this, new XNapButton(acDisconnect));
97  
98  	GridBagHelper.addVerticalSpacer(this);
99  
100 	// initialize
101 	tmConnections.setConnected(JTellaPlugin.getInstance().isStarted());
102     }
103 
104     // --- Method(s) ---
105 
106     /***
107      * Called by {@link JTellaPlugin} when a connection to the network
108      * is established or lost. Updates the actions.
109      */
110     public void setConnected(boolean connected)
111     {
112 	acConnect.setEnabled(!connected);
113 	acDisconnect.setEnabled(connected);
114 	tmConnections.setConnected(connected);
115     }
116 
117     public Action[] getActions()
118     {
119 	return new Action[] { acConnect, acDisconnect, };
120     }
121 
122     // --- Inner Class(es) ---
123 
124     /***
125      * Connects to a servant.
126      */
127     private class AddServantAction extends AbstractAction {
128 
129         public AddServantAction() 
130 	{
131             putValue(Action.NAME, XNap.tr("Add Servant"));
132             putValue(Action.SHORT_DESCRIPTION, 
133 		     XNap.tr("Connects to host:port."));
134         }
135 
136         public void actionPerformed(ActionEvent event) 
137 	{
138 	    String host = jtfHost.getText();
139 	    int port = vtfPort.getIntValue();
140 
141 	    if (!JTellaPlugin.getInstance().isStarted()) {
142 		JTellaPlugin.getInstance().startConnection();
143 	    }
144 
145 	    GNUTellaConnection c = JTellaPlugin.getInstance().getConnection();
146 	    c.addConnection(host, port);
147         }
148 
149     } 
150 
151     /***
152      * Connects to the network.
153      */
154     private class ConnectAction extends AbstractAction {
155 
156         public ConnectAction() 
157 	{
158             putValue(Action.NAME, XNap.tr("Connect"));
159             putValue(Action.SHORT_DESCRIPTION,
160 		     XNap.tr("Connects to the gnutella network."));
161 	    putValue(IconHelper.XNAP_ICON, "connect_creating.png");
162 
163 	    this.setEnabled(!JTellaPlugin.getInstance().isStarted());
164         }
165 
166         public void actionPerformed(ActionEvent event) 
167 	{
168 	    JTellaPlugin.getInstance().startConnection();
169 	}
170 
171     } 
172 
173     /***
174      * Disconnects from the network.
175      */
176     private class DisconnectAction extends AbstractAction {
177 
178         public DisconnectAction() 
179 	{
180             putValue(Action.NAME, XNap.tr("Disconnect"));
181             putValue(Action.SHORT_DESCRIPTION,
182 		     XNap.tr("Disconnects from the gnutella network."));
183 	    putValue(IconHelper.XNAP_ICON, "connect_no.png");
184 
185 	    this.setEnabled(JTellaPlugin.getInstance().isStarted());
186         }
187 
188         public void actionPerformed(ActionEvent event) 
189 	{
190 	    JTellaPlugin.getInstance().stopConnection();
191         }
192 
193     } 
194 
195 }