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