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;
21  
22  import java.awt.BorderLayout;
23  import java.awt.event.ActionEvent;
24  import java.awt.event.FocusAdapter;
25  import java.awt.event.FocusEvent;
26  import java.beans.PropertyChangeEvent;
27  import java.beans.PropertyChangeListener;
28  import java.io.File;
29  import java.util.LinkedList;
30  
31  import javax.swing.AbstractAction;
32  import javax.swing.Action;
33  import javax.swing.BorderFactory;
34  import javax.swing.JLabel;
35  import javax.swing.JPanel;
36  import javax.swing.JSplitPane;
37  import javax.swing.JTabbedPane;
38  
39  import org.xnap.XNap;
40  import org.xnap.gui.action.MenuAction;
41  import org.xnap.gui.action.PanelAction;
42  import org.xnap.gui.menu.PeerMenu;
43  import org.xnap.gui.util.*;
44  import org.xnap.peer.Peer;
45  import org.xnap.transfer.DownloadManager;
46  import org.xnap.transfer.Transfer;
47  import org.xnap.transfer.UploadManager;
48  import org.xnap.util.Preferences;
49  
50  public class TransferPanel extends JPanel 
51  	implements ActionProvider, PeerProvider, PropertyChangeListener
52  {
53  
54  	//--- Data field(s) ---
55  
56  	private static Preferences prefs = Preferences.getInstance();
57  
58  	private TransferManagerPanel jpDownloads;
59  	private TransferManagerPanel jpUploads;
60  
61  	private JTabbedPane jtp = null;
62  	private JSplitPane jsp = null;
63  
64  	private Action acClear = new ClearAction();
65  	private Action acSettings;
66  	private Action acPeerMenu 
67  		= new MenuAction(new PeerMenu(this), "users.png");
68  
69  	//--- Constructor(s) ---
70  
71  	public TransferPanel() 
72  	{
73  		initialize();
74  	}
75  
76  	//--- Method(s) ---
77  
78  	private void initialize() 
79  	{
80  		setLayout(new BorderLayout());
81  
82  		// settings panel
83  		JPanel jpSettings = new TransferSettingsPanel();
84  		acSettings = new PanelAction(jpSettings, "view_text.png");
85  	
86  		FocusSelectionListener fsl = new FocusSelectionListener();
87  
88  		// download panel
89  		jpDownloads = new TransferManagerPanel
90  			(DownloadManager.getInstance(), "download", false);
91  		jpDownloads.getTreeTable().addFocusListener(fsl);
92  
93  		// download status panel
94  		TransferStatusPanel spDownload = new TransferStatusPanel();
95  		spDownload.setName(XNap.tr("Download"));
96  		spDownload.setIcon(IconHelper.getIcon("down.png", 16, false));
97  		jpDownloads.getTransferTableModel().setListener(spDownload);
98  		StatusBar.getInstance().addComponent(spDownload);
99  		
100 		// upload panel
101 		jpUploads = new TransferManagerPanel
102 			(UploadManager.getInstance(), "upload", false);
103 		jpUploads.getTreeTable().addFocusListener(fsl);
104 
105 		// upload status panel
106 		TransferStatusPanel spUpload = new TransferStatusPanel();
107 		spUpload.setName(XNap.tr("Upload"));
108 		spUpload.setIcon(IconHelper.getIcon("up.png", 16, false));
109 		jpUploads.getTransferTableModel().setListener(spUpload);
110 		StatusBar.getInstance().addComponent(spUpload);
111 		
112 		// transfer panel
113 		updateTransfersPanel();
114 
115 		prefs.addPropertyChangeListener("useTabbedTransferPane", this);
116 	}
117 
118 	public Action[] getActions()
119 	{
120 		return new Action[] { acSettings, null, acPeerMenu, null, acClear };
121 	}
122 
123 	public void propertyChange(PropertyChangeEvent e)
124 	{
125 		updateTransfersPanel();
126 	}
127 
128 	public void savePrefs()
129 	{
130 		if (jsp != null) {
131 			prefs.setTransferDividerLocation(jsp.getDividerLocation());
132 		}
133 	}
134 
135 
136 	/***
137 	 *  @see xnap.gui.PeerProvider#getPeers()
138 	 */
139 	public Peer[] getPeers() {
140 		if (getSelectedPanel() != null) {
141 			return ((PeerProvider)getSelectedPanel()).getPeers();
142 		}
143 		return null;
144 	}
145 
146 	/***
147 	 * Return the active table.
148 	 *
149 	 * @return null, if nothing is selected
150 	 */
151 	private TransferManagerPanel getSelectedPanel() 
152 	{
153 		if (jpDownloads.getTreeTable().getSelectedRowCount() > 0) {
154 			return jpDownloads;
155 		}
156 		else if (jpUploads.getTreeTable().getSelectedRowCount() > 0) {
157 			return jpUploads;
158 		}
159 
160 		return null;
161 	}
162 
163 	/***
164 	 * Toggles transfers panel. 
165 	 */
166 	private void updateTransfersPanel()
167 	{
168 		if (prefs.getUseTabbedTransferPane()) {
169 			// tabbed pane
170 			jtp = new JTabbedPane();
171 			jtp.addTab(XNap.tr("Downloads"), 
172 					   IconHelper.getTabTitleIcon("down.png"), jpDownloads);
173 			jtp.addTab(XNap.tr("Uploads"), 
174 					   IconHelper.getTabTitleIcon("up.png"), jpUploads);
175 			if (jsp != null) {
176 				prefs.setTransferDividerLocation(jsp.getDividerLocation());
177 				remove(jsp);
178 				jsp = null;
179 			}
180 			add(jtp, BorderLayout.CENTER);
181 		}
182 		else {
183 			// split pane
184 			jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
185 			jsp.setOneTouchExpandable(true);
186 			jsp.setResizeWeight(1);
187 			jsp.setDividerLocation(prefs.getTransferDividerLocation());
188 			jsp.setBorder(GUIHelper.createEmptyBorder());
189 
190 			JPanel jpTemp = new JPanel(new BorderLayout());
191 			JLabel jlTemp = new JLabel(XNap.tr("Downloads"));
192 			jlTemp.setIcon(IconHelper.getIcon("down.png",
193 											  prefs.getUseSmallTransferIcons()
194 											  ? 16 : 22, false));
195 			jlTemp.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
196 			jpTemp.add(jlTemp, BorderLayout.NORTH);
197 			jpTemp.add(jpDownloads, BorderLayout.CENTER);
198 			jsp.add(jpTemp, JSplitPane.TOP);
199 
200 			jpTemp = new JPanel(new BorderLayout());
201 			jlTemp = new JLabel(XNap.tr("Uploads"));
202 			jlTemp.setIcon(IconHelper.getIcon("up.png",
203 											  prefs.getUseSmallTransferIcons()
204 											  ? 16 : 22, false));
205 			jlTemp.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
206 			jpTemp.add(jlTemp, BorderLayout.NORTH);
207 			jpTemp.add(jpUploads, BorderLayout.CENTER);
208 			jsp.add(jpTemp, JSplitPane.BOTTOM);
209 
210 			if (jtp != null) {
211 				remove(jtp);
212 				jtp = null;
213 			}
214 			add(jsp, BorderLayout.CENTER);
215 		}
216 	}
217 
218 	/***
219 	 * Returns the File objects in provided by <code>p</code>. Helper method
220 	 * that is used by {@link TransferPanel}.
221 	 */
222 	public static File[] getFiles(TransferProvider p)
223 	{
224 		Transfer[] transfers = p.getTransfers();
225 		if (transfers != null) {
226 			LinkedList files = new LinkedList();
227 			for (int i = 0; i < transfers.length; i++) {
228 				File file = transfers[i].getFile();
229 				if (file != null) {
230 					files.add(file);
231 				}
232 			}
233 			return (File[])files.toArray(new File[0]);
234 		}
235 		return null;
236 	}
237 
238 	/***
239 	 * Returns the Peer objects provided by <code>p</code>. Helper method
240 	 * that is used by {@link TransferPanel}.
241 	 */
242 	public static Peer[] getPeers(TransferProvider p)
243 	{
244 		Transfer[] transfers = p.getTransfers();
245 		if (transfers != null) {
246 			LinkedList peers = new LinkedList();
247 			for (int i = 0; i < transfers.length; i++) {
248 				Peer peer = transfers[i].getPeer();
249 				if (peer != null) {
250 					peers.add(peer);
251 				}
252 			}
253 			return (Peer[])peers.toArray(new Peer[0]);
254 		}
255 		return null;
256 	}
257 
258 	//--- Inner Class(es) ---
259 
260 	/***
261 	 * Makes sure only one table has selected transfers at a time.
262 	 */
263 	private class FocusSelectionListener extends FocusAdapter
264 	{
265 		public void focusGained(FocusEvent e)
266 		{
267 			if (e.getSource() == jpDownloads.getTreeTable()) {
268 				jpUploads.getTreeTable().getSelectionModel().clearSelection();
269 			}
270 			else if (e.getSource() == jpUploads.getTreeTable()) {
271 				jpDownloads.getTreeTable().getSelectionModel().clearSelection();
272 			}
273 		}
274 	}
275 
276     /***
277      * Clears all finished transfers.
278      */
279     private class ClearAction extends AbstractAction 
280 	{
281 
282         public ClearAction() 
283 		{
284             putValue(Action.NAME, XNap.tr("Clear Finished"));
285             putValue(Action.SHORT_DESCRIPTION,
286 					 XNap.tr("Clears all finished transfers."));
287 			putValue(IconHelper.XNAP_ICON, "edittrash.png");
288         }
289 
290         public void actionPerformed(ActionEvent event) 
291 		{
292 			TransferManagerPanel panel = getSelectedPanel();
293 			if (panel == null) {
294 				DownloadManager.getInstance().clearDone();
295 				UploadManager.getInstance().clearDone();
296 			}
297 			else {
298 				panel.getManager().clearDone();
299 			}
300         }
301 
302     }
303 
304 }