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.limewire;
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.JLabel;
30  import javax.swing.JPanel;
31  import javax.swing.JScrollPane;
32  import javax.swing.JTable;
33  import javax.swing.JTextField;
34  
35  import org.xnap.XNap;
36  import org.xnap.gui.ActionProvider;
37  import org.xnap.gui.component.ValidatedTextField;
38  import org.xnap.gui.component.XNapButton;
39  import org.xnap.gui.util.GUIHelper;
40  import org.xnap.gui.util.GridBagHelper;
41  import org.xnap.gui.util.IconHelper;
42  import org.xnap.util.Formatter;
43  import org.xnap.util.StringHelper;
44  
45  import com.limegroup.gnutella.RouterService;
46  
47  public class LimeWirePanel extends JPanel implements ActionProvider {
48  
49      // --- Data Field(s) ---
50  
51      private LimeWireConnectionTableModel tmConnections;
52      private JTable jtConnections;
53  
54  	private JLabel jlStatus;
55  
56      private JTextField jtfHost;
57      private ValidatedTextField vtfPort;
58  
59      private Action acConnect = new ConnectAction();
60      private Action acDisconnect = new DisconnectAction();
61  
62      // --- Constructor(s) ---
63  
64      public LimeWirePanel() 
65      {
66          setLayout(new GridBagLayout());
67  
68  		// status
69  		JPanel jpStatus = new JPanel(new GridBagLayout());
70  		jpStatus.setBorder(GUIHelper.createDefaultBorder(XNap.tr("Status")));
71  		GridBagHelper.add(this, jpStatus);
72  
73  		jlStatus = new JLabel();
74  		GridBagHelper.add(jpStatus, jlStatus);
75  		
76  		GridBagHelper.addComponent
77  			(jpStatus, new XNapButton(new UpdateAction()));
78  
79  		// connections
80  		JPanel jpConnections = new JPanel(new BorderLayout());
81  		jpConnections.setBorder
82  			(GUIHelper.createDefaultBorder(XNap.tr("Connections")));
83  		GridBagHelper.addPanel(this, jpConnections);
84  
85  		tmConnections = new LimeWireConnectionTableModel();
86  		jtConnections = tmConnections.createTable
87  			(LimeWirePlugin.getPreferences(), "connection");
88  		jpConnections.add(new JScrollPane(jtConnections), BorderLayout.CENTER);
89  
90  		// add servant Panel
91  		JPanel jpConnect = new JPanel(new GridBagLayout());
92  		GridBagHelper.add(this, jpConnect);
93  
94  		Action acAdd = new AddServantAction();
95  
96  		GridBagHelper.addLabel(jpConnect, XNap.tr("Host"));
97  		jtfHost = new JTextField("localhost", 15);
98  		GUIHelper.bindEnterKey(jtfHost, acAdd);
99  		GridBagHelper.addComponent(jpConnect, jtfHost);
100 
101 		GridBagHelper.addLabel(jpConnect, XNap.tr("Port"));
102 		vtfPort = new ValidatedTextField("6346", 5, StringHelper.NUMBERS_INT);
103 		GUIHelper.bindEnterKey(vtfPort, acAdd);
104 		GridBagHelper.addComponent(jpConnect, vtfPort);
105 
106 		GridBagHelper.addLabel(jpConnect, "");
107 		JButton jbAdd = new XNapButton(acAdd);
108 		GridBagHelper.addComponent(jpConnect, jbAdd);
109 
110 		// button
111 		GridBagHelper.addComponent(this, new XNapButton(acConnect));
112 		GridBagHelper.addComponent(this, new XNapButton(acDisconnect));
113 
114 		GridBagHelper.addVerticalSpacer(this);
115 
116 		// initialize
117 		updateStatus();
118     }
119 
120     // --- Method(s) ---
121 
122     /***
123      * Called by {@link LimeWirePlugin} when a connection to the network
124      * is established or lost. Updates the actions.
125      */
126     public void setConnected(boolean connected)
127     {
128 		acConnect.setEnabled(!connected);
129 		acDisconnect.setEnabled(connected);
130 		tmConnections.setConnected(connected);
131     }
132 
133     public Action[] getActions()
134     {
135 		return new Action[] { acConnect, acDisconnect, };
136     }
137 
138 	public LimeWireConnectionTableModel getTableModel()
139 	{
140 		return tmConnections;
141 	}
142 
143 	public void updateStatus()
144 	{
145 		StringBuffer sb = new StringBuffer();
146 		sb.append("<html>");
147 		sb.append("Listening on port " + RouterService.getPort() + ", ");
148 		sb.append(RouterService.getNumFiles() + " Files, ");
149 		sb.append(RouterService.getNumHosts() + " Hosts, ");
150 		sb.append(RouterService.getNumSharedFiles() + " Files Shared (" 
151 				  + Formatter.formatSize(RouterService.getSharedFileSize()) 
152 				  + ")");
153 		if (RouterService.getNumPendingShared() > 0) {
154 			sb.append(", " + RouterService.getNumPendingShared() + " pending");
155 		}
156 		jlStatus.setText(sb.toString());
157 	}
158 
159     // --- Inner Class(es) ---
160 
161     /***
162      * Connects to a servant.
163      */
164     private class AddServantAction extends AbstractAction {
165 
166         public AddServantAction() 
167 		{
168             putValue(Action.NAME, XNap.tr("Add Servant"));
169             putValue(Action.SHORT_DESCRIPTION, 
170 					 XNap.tr("Connects to host:port."));
171         }
172 
173         public void actionPerformed(ActionEvent event) 
174 		{
175 			String host = jtfHost.getText();
176 			int port = vtfPort.getIntValue();
177 
178 			RouterService.connectToHostAsynchronously(host, port);
179         }
180 
181     } 
182 
183     /***
184      * Connects to the network.
185      */
186     private class ConnectAction extends AbstractAction 
187 	{
188         public ConnectAction() 
189 		{
190             putValue(Action.NAME, XNap.tr("Connect"));
191             putValue(Action.SHORT_DESCRIPTION,
192 					 XNap.tr("Connects to the gnutella network."));
193 			putValue(IconHelper.XNAP_ICON, "connect_creating.png");
194         }
195 
196         public void actionPerformed(ActionEvent event) 
197 		{
198 			LimeWirePlugin.getInstance().connect();
199 		}
200     } 
201 
202     /***
203      * Disconnects from the network.
204      */
205     private class DisconnectAction extends AbstractAction 
206 	{
207         public DisconnectAction() 
208 		{
209             putValue(Action.NAME, XNap.tr("Disconnect"));
210             putValue(Action.SHORT_DESCRIPTION,
211 					 XNap.tr("Disconnects from the gnutella network."));
212 			putValue(IconHelper.XNAP_ICON, "connect_no.png");
213         }
214 
215         public void actionPerformed(ActionEvent event) 
216 		{
217 			LimeWirePlugin.getInstance().disconnect();
218         }
219     } 
220 
221 	public class UpdateAction extends AbstractAction
222 	{
223 		public UpdateAction()
224 		{
225 			putValue(Action.NAME, XNap.tr("Update"));
226 			putValue(Action.SHORT_DESCRIPTION, 
227 					 XNap.tr("Updates status."));
228 			putValue(IconHelper.XNAP_ICON, "reload.png");
229 		}
230 		
231 		public void actionPerformed(ActionEvent event)
232 		{
233 			updateStatus();
234 		}
235 	}
236 
237 }