| 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.DataSet;
|
|---|
| 20 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 21 | import org.openstreetmap.josm.data.osm.User;
|
|---|
| 22 |
|
|---|
| 23 | /**
|
|---|
| 24 | * Displays a dialog with all users who have last edited something in the
|
|---|
| 25 | * selection area, along with the number of objects.
|
|---|
| 26 | *
|
|---|
| 27 | * @author Frederik Ramm <frederik@remote.org>
|
|---|
| 28 | */
|
|---|
| 29 | public class UserListDialog extends ToggleDialog implements SelectionChangedListener {
|
|---|
| 30 |
|
|---|
| 31 | /**
|
|---|
| 32 | * The display list.
|
|---|
| 33 | */
|
|---|
| 34 | private final DefaultTableModel data = new DefaultTableModel() {
|
|---|
| 35 | @Override public boolean isCellEditable(int row, int column) {
|
|---|
| 36 | return false;
|
|---|
| 37 | }
|
|---|
| 38 | @Override public Class<?> getColumnClass(int columnIndex) {
|
|---|
| 39 | return columnIndex == 0 ? String.class : Integer.class;
|
|---|
| 40 | }
|
|---|
| 41 | };
|
|---|
| 42 |
|
|---|
| 43 | private JTable userTable = new JTable(data);
|
|---|
| 44 |
|
|---|
| 45 | private static User anonymousUser = User.get("(anonymous users)");
|
|---|
| 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 | DataSet.listeners.add(this);
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | @Override public void setVisible(boolean b) {
|
|---|
| 59 | super.setVisible(b);
|
|---|
| 60 | if (b)
|
|---|
| 61 | selectionChanged(Main.ds.getSelected());
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | /**
|
|---|
| 65 | * Called when the selection in the dataset changed.
|
|---|
| 66 | * @param newSelection The new selection array.
|
|---|
| 67 | */
|
|---|
| 68 | public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
|
|---|
| 69 | if (!isVisible())
|
|---|
| 70 | return;
|
|---|
| 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 | User u = p.user;
|
|---|
| 87 | if (u == null) u = anonymousUser;
|
|---|
| 88 | UserCount uc = counters.get(u);
|
|---|
| 89 | if (uc == null)
|
|---|
| 90 | counters.put(u, uc = new UserCount(u, 0));
|
|---|
| 91 | uc.count++;
|
|---|
| 92 | all++;
|
|---|
| 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 | }
|
|---|