01
02 /**
03 * Title: Advanced Network Client Sample<p>
04 * Description: <p>
05 * Copyright: Copyright (C) 2009 Alexey Veremenko<p>
06 * Company: <p>
07 * @author Alexey Veremenko
08 * @version 1.0
09 */
10 package networking.client;
11
12 import networking.protocol.*;
13
14 import java.awt.*;
15 import java.util.*;
16
17 /**
18 * ListEx extends List to allow associating user objects with List items
19 */
20 public class ListEx extends java.awt.List
21 {
22 /**
23 * Mark for the leading client
24 */
25 private static final String s_mark = " (Leader)";
26
27 /**
28 * Construct new ListEx object
29 * @param rows number of visible rows
30 */
31 public ListEx(int rows)
32 {
33 super(rows, true);
34 }
35
36 /**
37 * Refill clients list
38 * @param msg message
39 */
40 public void fill(Message msg)
41 {
42 // Save selection
43 ArrayList selected = getSelectedClients();
44 removeAll();
45
46 // Sort items
47 java.util.List list = msg.getList();
48 Client leader = msg.getClient();
49 Collections.sort(list);
50
51 // Add items
52 for (int i = 0; i < list.size(); i++)
53 {
54 Client c = (Client)list.get(i);
55 add(c == leader ? c.getName() + s_mark : c.getName());
56 // Restore selection
57 if (selected.contains(c))
58 select(i);
59 }
60 }
61
62 /**
63 * Get a list of selected clients
64 * @return list of Client objects
65 */
66 public ArrayList getSelectedClients()
67 {
68 String[] names = getSelectedItems();
69 ArrayList list = new ArrayList();
70
71 for (int i = 0; i < names.length; i++)
72 {
73 String name = names[i];
74 // Check mark
75 if (name.endsWith(s_mark))
76 {
77 // Remove mark from name
78 name = name.substring(0, name.length() - s_mark.length());
79 }
80 list.add(new Client(name));
81 }
82
83 return list;
84 }
85 }
|