1 | package org.openstreetmap.josm.gui.dialogs;
|
---|
2 |
|
---|
3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
4 |
|
---|
5 | import java.awt.BorderLayout;
|
---|
6 | import java.awt.event.KeyEvent;
|
---|
7 | import java.util.Arrays;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.Comparator;
|
---|
10 | import java.util.HashMap;
|
---|
11 |
|
---|
12 | import javax.swing.JScrollPane;
|
---|
13 | import javax.swing.JTable;
|
---|
14 | import javax.swing.ListSelectionModel;
|
---|
15 | import javax.swing.table.DefaultTableModel;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
19 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
20 | import org.openstreetmap.josm.data.osm.User;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Displays a dialog with all users who have last edited something in the
|
---|
24 | * selection area, along with the number of objects.
|
---|
25 | *
|
---|
26 | * @author Frederik Ramm <frederik@remote.org>
|
---|
27 | */
|
---|
28 | public class UserListDialog extends ToggleDialog implements SelectionChangedListener {
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * The display list.
|
---|
32 | */
|
---|
33 | private final DefaultTableModel data = new DefaultTableModel() {
|
---|
34 | @Override public boolean isCellEditable(int row, int column) {
|
---|
35 | return false;
|
---|
36 | }
|
---|
37 | @Override public Class<?> getColumnClass(int columnIndex) {
|
---|
38 | return columnIndex == 0 ? String.class : Integer.class;
|
---|
39 | }
|
---|
40 | };
|
---|
41 |
|
---|
42 | private JTable userTable = new JTable(data);
|
---|
43 |
|
---|
44 | private static User anonymousUser = User.get("(anonymous users)");
|
---|
45 |
|
---|
46 | public UserListDialog() {
|
---|
47 | super(tr("Authors"), "userlist", tr("Open a list of people working on the selected objects."), KeyEvent.VK_A, 150);
|
---|
48 |
|
---|
49 | data.setColumnIdentifiers(new String[]{tr("Author"),tr("# Objects"),"%"});
|
---|
50 | userTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
---|
51 | add(new JScrollPane(userTable), BorderLayout.CENTER);
|
---|
52 | selectionChanged(Main.ds.getSelected());
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override public void setVisible(boolean b) {
|
---|
56 | if (b) {
|
---|
57 | Main.ds.listeners.add(this);
|
---|
58 | selectionChanged(Main.ds.getSelected());
|
---|
59 | } else {
|
---|
60 | Main.ds.listeners.remove(this);
|
---|
61 | }
|
---|
62 | super.setVisible(b);
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Called when the selection in the dataset changed.
|
---|
67 | * @param newSelection The new selection array.
|
---|
68 | */
|
---|
69 | public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
|
---|
70 |
|
---|
71 | class UserCount {
|
---|
72 | User user;
|
---|
73 | int count;
|
---|
74 | UserCount(User user, int count) { this.user=user; this.count=count; }
|
---|
75 | }
|
---|
76 |
|
---|
77 | if (data == null)
|
---|
78 | return; // selection changed may be received in base class constructor before init
|
---|
79 |
|
---|
80 | data.setRowCount(0);
|
---|
81 |
|
---|
82 | HashMap<User,UserCount> counters = new HashMap<User,UserCount>();
|
---|
83 | int all = 0;
|
---|
84 | for (OsmPrimitive p : newSelection) {
|
---|
85 | User u = p.user;
|
---|
86 | if (u == null) u = anonymousUser;
|
---|
87 | UserCount uc = counters.get(u);
|
---|
88 | if (uc == null)
|
---|
89 | counters.put(u, uc = new UserCount(u, 0));
|
---|
90 | uc.count++;
|
---|
91 | all++;
|
---|
92 | }
|
---|
93 | UserCount[] ucArr = new UserCount[counters.size()];
|
---|
94 | counters.values().toArray(ucArr);
|
---|
95 | Arrays.sort(ucArr, new Comparator<UserCount>() {
|
---|
96 | public int compare(UserCount a, UserCount b) {
|
---|
97 | return (a.count<b.count) ? 1 : (a.count>b.count) ? -1 : 0;
|
---|
98 | }
|
---|
99 | });
|
---|
100 |
|
---|
101 | for (UserCount uc : ucArr) {
|
---|
102 | data.addRow(new Object[] { uc.user.name, uc.count, uc.count * 100 / all });
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | }
|
---|