source: josm/src/org/openstreetmap/josm/gui/dialogs/UserListDialog.java@ 242

Last change on this file since 242 was 242, checked in by imi, 17 years ago
  • fixed many java warnings
  • changed eclipse settings to use JUnit library instead of (deprecated) environment variable
File size: 2.9 KB
Line 
1package org.openstreetmap.josm.gui.dialogs;
2
3import static org.openstreetmap.josm.tools.I18n.tr;
4
5import java.awt.BorderLayout;
6import java.awt.event.KeyEvent;
7import java.util.Arrays;
8import java.util.Collection;
9import java.util.Comparator;
10import java.util.HashMap;
11
12import javax.swing.JScrollPane;
13import javax.swing.JTable;
14import javax.swing.ListSelectionModel;
15import javax.swing.table.DefaultTableModel;
16
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.data.SelectionChangedListener;
19import org.openstreetmap.josm.data.osm.OsmPrimitive;
20import 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 */
28public 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 private JTable userTable = new JTable(data);
42
43 public UserListDialog() {
44 super(tr("Authors"), "userlist", tr("Open a list of people working on the selected objects."), KeyEvent.VK_A, 150);
45
46 data.setColumnIdentifiers(new String[]{tr("Author"),tr("# Objects"),"%"});
47 userTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
48 add(new JScrollPane(userTable), BorderLayout.CENTER);
49 selectionChanged(Main.ds.getSelected());
50 }
51
52 @Override public void setVisible(boolean b) {
53 if (b) {
54 Main.ds.listeners.add(this);
55 selectionChanged(Main.ds.getSelected());
56 } else {
57 Main.ds.listeners.remove(this);
58 }
59 super.setVisible(b);
60 }
61
62 /**
63 * Called when the selection in the dataset changed.
64 * @param newSelection The new selection array.
65 */
66 public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
67
68 class UserCount {
69 User user;
70 int count;
71 UserCount(User user, int count) { this.user=user; this.count=count; }
72 }
73
74 if (data == null)
75 return; // selection changed may be received in base class constructor before init
76
77 data.setRowCount(0);
78
79 HashMap<User,UserCount> counters = new HashMap<User,UserCount>();
80 int all = 0;
81 for (OsmPrimitive p : newSelection) {
82 if (p.user != null) {
83 UserCount uc = counters.get(p.user);
84 if (uc == null)
85 counters.put(p.user, uc = new UserCount(p.user, 0));
86 uc.count++;
87 all++;
88 }
89 }
90 UserCount[] ucArr = new UserCount[counters.size()];
91 counters.values().toArray(ucArr);
92 Arrays.sort(ucArr, new Comparator<UserCount>() {
93 public int compare(UserCount a, UserCount b) {
94 return (a.count<b.count) ? 1 : (a.count>b.count) ? -1 : 0;
95 }
96 });
97
98 for (UserCount uc : ucArr) {
99 data.addRow(new Object[] { uc.user.name, uc.count, uc.count * 100 / all });
100 }
101 }
102
103}
Note: See TracBrowser for help on using the repository browser.