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.event.ActionEvent;
23  import java.awt.event.KeyEvent;
24  import java.io.File;
25  
26  import javax.swing.AbstractAction;
27  import javax.swing.Action;
28  import javax.swing.JMenu;
29  import javax.swing.JPopupMenu;
30  import javax.swing.JScrollPane;
31  import javax.swing.KeyStroke;
32  import javax.swing.tree.TreePath;
33  
34  import org.xnap.gui.action.*;
35  import org.xnap.gui.shortcut.*;
36  import org.xnap.gui.component.XNapMenuItem;
37  import org.xnap.gui.event.DoubleClickListener;
38  import org.xnap.gui.event.PopupListener;
39  import org.xnap.gui.menu.OpenFileWithMenu;
40  import org.xnap.gui.menu.PeerMenu;
41  import org.xnap.gui.menu.TransferMenu;
42  import org.xnap.gui.table.FilteredTransferTableModel;
43  import org.xnap.gui.table.JTreeTable;
44  import org.xnap.gui.table.TransferTableModel;
45  import org.xnap.gui.table.TreeTableNode;
46  import org.xnap.gui.tree.TransferTreeCellRenderer;
47  import org.xnap.gui.util.GUIHelper;
48  import org.xnap.peer.Peer;
49  import org.xnap.transfer.AbstractTransferManager;
50  import org.xnap.transfer.Transfer;
51  import org.xnap.transfer.action.AbstractDeleteAction;
52  import org.xnap.util.Preferences;
53  
54  public class TransferManagerPanel extends JScrollPane 
55      implements FileProvider, PeerProvider, TransferProvider
56  {
57      
58      //--- Data field(s) ---
59  
60      private JTreeTable jtt;
61      private TransferTableModel ttm;
62      private TransferMenu jmPopup;
63  	private AbstractTransferManager manager; 
64  
65  	private Action openFileAction = new OpenFileAction(this);
66  
67      //--- Constructor(s) ---
68  
69      public TransferManagerPanel(AbstractTransferManager manager, String table,
70  								boolean filtered) 
71      {
72          initialize(manager, table, filtered);
73      }
74  
75      //--- Method(s) ---
76  
77      private void initialize(AbstractTransferManager manager, String table,
78  							boolean filtered) 
79      {
80  		this.manager = manager;
81  
82  		// popup
83  		JMenu jmPopup = new TransferMenu(this);
84  		jmPopup.add(new XNapMenuItem(openFileAction));
85  		jmPopup.add(new OpenFileWithMenu(this));
86  		jmPopup.addSeparator();
87  		jmPopup.add(new PeerMenu(this));
88  
89  
90  		ttm = (filtered) 
91  			? new FilteredTransferTableModel(manager)
92  			: new TransferTableModel(manager, true);
93  		jtt = ttm.createTreeTable(Preferences.getInstance(), table);
94  		jtt.getTree().setCellRenderer
95  			(new TransferTreeCellRenderer(ttm, !filtered));
96  		DoubleClickListener.install(jtt, new DoubleClickAction());
97  		GUIHelper.bindKey(jtt, KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0),
98  						  new DeleteAction(), false);
99  
100 		// shortcuts
101 		ShortcutManager.getInstance().add(openFileAction, jtt);
102 
103 		// add existing transfers
104 		Transfer[] transfers = manager.getTransfers();
105 		for (int i = 0; i < transfers.length; i++) {
106 			ttm.add(transfers[i]);
107 		}
108 
109 		PopupListener pl = new PopupListener(jmPopup.getPopupMenu());
110 		jtt.addMouseListener(pl);
111 
112 		setViewportView(jtt);
113 //  		if (filtered) {
114 //  			setBorder(GUIHelper.createEmptyBorder());
115 //  			jtt.getTableHeader().setBorder(GUIHelper.createEtchedBorder());
116 //  		}
117     }
118 
119     /***
120      * Implements FileProvider.
121      */
122     public File[] getFiles()
123     {
124 		return TransferPanel.getFiles(this);
125     }
126 
127 	public AbstractTransferManager getManager()
128 	{
129 		return manager;
130 	}
131 
132     /***
133      * Implements PeerProvider.
134      */
135     public Peer[] getPeers()
136     {
137 		return TransferPanel.getPeers(this);
138     }
139 
140     public JPopupMenu getPopup()
141     {
142 		return jmPopup.getPopupMenu();
143     }
144 
145     public TransferTableModel getTransferTableModel()
146     {
147 		return ttm;
148     }
149 
150     public JTreeTable getTreeTable()
151     {
152 		return jtt;
153     }
154 
155     /***
156      * Returns the currently selected transfer.
157      */
158     public Transfer getSelectedTransfer()
159     {
160 		TreePath path = jtt.getTree().getSelectionPath();
161 		if (path != null) {
162 			TreeTableNode node 
163 				= (TreeTableNode)path.getLastPathComponent();
164 			return (Transfer)node.getData();
165 		}
166 		return null;
167     }
168 
169     /***
170      * Returns the currently selected transfers.
171      *
172      * @return null, if nothing is selected
173      */
174     public Transfer[] getTransfers()
175     {
176 		TreePath[] paths = jtt.getTree().getSelectionPaths();
177 		if (paths != null) {
178 			Transfer[] results = new Transfer[paths.length];
179 			for (int i = 0; i < results.length; i++) {
180 				TreeTableNode node 
181 					= (TreeTableNode)paths[i].getLastPathComponent();
182 				results[i] = (Transfer)node.getData();
183 			}
184 			return results;
185 		}
186 		return null;
187     }
188 
189 	private class DeleteAction extends AbstractAction
190 	{
191 
192         public void actionPerformed(ActionEvent event) 
193 		{
194 			Transfer[] transfers = getTransfers();
195 			if (transfers != null) {
196 				for (int i = 0; i < transfers.length; i++) {
197 					ActionHelper.perform(transfers[i].getActions(), event,
198 										 AbstractDeleteAction.class);
199 				}
200 			}
201 		}
202 
203 	}
204 
205 	private class DoubleClickAction extends AbstractAction
206 	{
207 
208         public void actionPerformed(ActionEvent event) 
209 		{
210 			Transfer transfer = getSelectedTransfer();
211 			if (transfer != null) {
212 				if (transfer.isDone()) {
213 					openFileAction.actionPerformed(event);
214 				}
215 				else {
216 					Action[] actions = transfer.getActions();
217 					if (actions != null && actions.length > 0
218 						&& actions[0].isEnabled()) {
219 						actions[0].actionPerformed(event);
220 					}
221 				}
222 			}
223 		}
224 
225 	}
226 
227 }