1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui.menu;
21
22 import java.util.Arrays;
23 import java.util.HashSet;
24
25 import javax.swing.Action;
26 import javax.swing.JMenuItem;
27 import javax.swing.JPopupMenu;
28
29 import org.xnap.XNap;
30 import org.xnap.gui.PeerProvider;
31 import org.xnap.gui.action.ActionHelper;
32 import org.xnap.gui.component.XNapMenuItem;
33 import org.xnap.gui.util.IconHelper;
34 import org.xnap.peer.Peer;
35 import org.xnap.util.LinkType;
36
37 public class PeerMenu extends AbstractDynamicMenu
38 {
39
40
41
42
43
44 private PeerProvider pp;
45
46
47
48 public PeerMenu(PeerProvider pp)
49 {
50 super(XNap.tr("Peers"));
51
52 this.pp = pp;
53
54 setIcon(IconHelper.getMenuIcon("users.png"));
55 setToolTipText(XNap.tr("Opens a menu with peer related actions."));
56
57
58 addDummy();
59 }
60
61
62
63 /***
64 * @return the number of actions that were added; -1, if no peers
65 * were passed
66 */
67 public static int addActions(AbstractDynamicMenu menu, Peer[] peers)
68 {
69 if (peers == null || peers.length == 0) {
70 return -1;
71 }
72 else if (peers.length == 1) {
73 Action[] actions = peers[0].getActions();
74 if (actions != null && actions.length > 0) {
75 for (int i = 0; i < actions.length; i++) {
76 menu.addTemporary(new XNapMenuItem(actions[i]));
77 }
78 addInfo(menu, peers[0]);
79 return 1;
80 }
81 else {
82 return 0;
83 }
84 }
85 else {
86
87 HashSet set = new HashSet(Arrays.asList(peers));
88 peers = (Peer[])set.toArray(new Peer[0]);
89
90 int count = ActionHelper.addCommonActions
91 (menu, peers, new PeerActionProvider(), menu.getItemCount());
92 menu.addTemporary(new JPopupMenu.Separator());
93 menu.addTemporary(XNap.tr("{0} peers", new Integer(peers.length)));
94 return count;
95 }
96 }
97
98 protected void addDummy()
99 {
100 addTemporary(new JMenuItem("(" + XNap.tr("No Peers Selected") + ")"));
101 }
102
103 public static void addInfo(AbstractDynamicMenu menu, Peer peer)
104 {
105 if (peer.getClientInfo() != null) {
106 menu.addTemporary(new JPopupMenu.Separator());
107 menu.addTemporary
108 (XNap.tr("Client") + ": " + peer.getClientInfo());
109 if (peer.getFileCount() != -1) {
110 menu.addTemporary
111 (XNap.tr("File Count") + ": " + peer.getFileCount());
112 }
113 menu.addTemporary(XNap.tr("Link") + ": "
114 + LinkType.getType(peer.getLinkSpeed()));
115 }
116 }
117
118 protected void willBecomeVisible()
119 {
120 removeAllTemporaries();
121
122 int count = addActions(this, pp.getPeers());
123 if (count == -1) {
124 addDummy();
125 }
126 else if (count == 0) {
127 addTemporary
128 (new JMenuItem("(" + XNap.tr("No Actions Supported") + ")"));
129 }
130 }
131
132 /***
133 * @see ActionHelper#getCommonActions(Object[], ActionHelper.ActionExtractor)
134 */
135 private static class PeerActionProvider
136 implements ActionHelper.ActionExtractor
137 {
138
139 public Action[] getActions(Object o)
140 {
141 return ((Peer)o).getActions();
142 }
143
144 }
145
146 }