1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.gui.dialogs;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.BorderLayout;
|
---|
7 | import java.awt.event.KeyEvent;
|
---|
8 | import java.awt.event.MouseEvent;
|
---|
9 | import java.awt.event.MouseListener;
|
---|
10 | import java.util.Arrays;
|
---|
11 | import java.util.Collection;
|
---|
12 | import java.util.Comparator;
|
---|
13 | import java.util.HashMap;
|
---|
14 | import java.util.LinkedList;
|
---|
15 |
|
---|
16 | import javax.swing.JScrollPane;
|
---|
17 | import javax.swing.JTable;
|
---|
18 | import javax.swing.ListSelectionModel;
|
---|
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.DataSet;
|
---|
24 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
25 | import org.openstreetmap.josm.data.osm.User;
|
---|
26 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Displays a dialog with all users who have last edited something in the
|
---|
30 | * selection area, along with the number of objects.
|
---|
31 | *
|
---|
32 | * @author Frederik Ramm <frederik@remote.org>
|
---|
33 | */
|
---|
34 | public class UserListDialog extends ToggleDialog implements SelectionChangedListener, MouseListener{
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * The display list.
|
---|
38 | */
|
---|
39 | private final DefaultTableModel data = new DefaultTableModel() {
|
---|
40 | @Override public boolean isCellEditable(int row, int column) {
|
---|
41 | return false;
|
---|
42 | }
|
---|
43 | @Override public Class<?> getColumnClass(int columnIndex) {
|
---|
44 | return columnIndex == 0 ? String.class : Integer.class;
|
---|
45 | }
|
---|
46 | };
|
---|
47 |
|
---|
48 | private JTable userTable = new JTable(data);
|
---|
49 |
|
---|
50 | private static User anonymousUser = User.get("(anonymous users)");
|
---|
51 |
|
---|
52 | public UserListDialog() {
|
---|
53 | super(tr("Authors"), "userlist", tr("Open a list of people working on the selected objects."),
|
---|
54 | Shortcut.registerShortcut("subwindow:authors", tr("Toggle: {0}", tr("Authors")), KeyEvent.VK_A, Shortcut.GROUP_LAYER, Shortcut.SHIFT_DEFAULT), 150);
|
---|
55 |
|
---|
56 | data.setColumnIdentifiers(new String[]{tr("Author"),tr("# Objects"),"%"});
|
---|
57 | userTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
---|
58 | add(new JScrollPane(userTable), BorderLayout.CENTER);
|
---|
59 | selectionChanged(Main.ds.getSelected());
|
---|
60 | userTable.addMouseListener(this);
|
---|
61 | DataSet.selListeners.add(this);
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Override public void setVisible(boolean b) {
|
---|
65 | super.setVisible(b);
|
---|
66 | if (b)
|
---|
67 | selectionChanged(Main.ds.getSelected());
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Called when the selection in the dataset changed.
|
---|
72 | * @param newSelection The new selection array.
|
---|
73 | */
|
---|
74 | public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
|
---|
75 | if (!isVisible())
|
---|
76 | return;
|
---|
77 |
|
---|
78 | class UserCount {
|
---|
79 | User user;
|
---|
80 | int count;
|
---|
81 | UserCount(User user, int count) { this.user=user; this.count=count; }
|
---|
82 | }
|
---|
83 |
|
---|
84 | if (data == null)
|
---|
85 | return; // selection changed may be received in base class constructor before init
|
---|
86 |
|
---|
87 | data.setRowCount(0);
|
---|
88 |
|
---|
89 | HashMap<User,UserCount> counters = new HashMap<User,UserCount>();
|
---|
90 | int all = 0;
|
---|
91 | for (OsmPrimitive p : newSelection) {
|
---|
92 | User u = p.user;
|
---|
93 | if (u == null) u = anonymousUser;
|
---|
94 | UserCount uc = counters.get(u);
|
---|
95 | if (uc == null)
|
---|
96 | counters.put(u, uc = new UserCount(u, 0));
|
---|
97 | uc.count++;
|
---|
98 | all++;
|
---|
99 | }
|
---|
100 | UserCount[] ucArr = new UserCount[counters.size()];
|
---|
101 | counters.values().toArray(ucArr);
|
---|
102 | Arrays.sort(ucArr, new Comparator<UserCount>() {
|
---|
103 | public int compare(UserCount a, UserCount b) {
|
---|
104 | return (a.count<b.count) ? 1 : (a.count>b.count) ? -1 : 0;
|
---|
105 | }
|
---|
106 | });
|
---|
107 |
|
---|
108 | for (UserCount uc : ucArr) {
|
---|
109 | data.addRow(new Object[] { uc.user.name, uc.count, uc.count * 100 / all });
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | public void mouseClicked(MouseEvent e) {
|
---|
114 | if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount()==2) {
|
---|
115 | int index = userTable.getSelectedRow();
|
---|
116 | String userName = (String) data.getValueAt(index, 0);
|
---|
117 | if (userName==null)
|
---|
118 | return;
|
---|
119 | Collection<OsmPrimitive> selected = Main.ds.getSelected();
|
---|
120 | Collection<OsmPrimitive> byUser = new LinkedList<OsmPrimitive>();
|
---|
121 | for (OsmPrimitive p : selected) {
|
---|
122 | if (p.user!= null && userName.equals(p.user.name))
|
---|
123 | byUser.add(p);
|
---|
124 | }
|
---|
125 | Main.ds.setSelected(byUser);
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | public void mouseEntered(MouseEvent e) {
|
---|
130 | }
|
---|
131 |
|
---|
132 | public void mouseExited(MouseEvent e) {
|
---|
133 | }
|
---|
134 |
|
---|
135 | public void mousePressed(MouseEvent e) {
|
---|
136 | }
|
---|
137 |
|
---|
138 | public void mouseReleased(MouseEvent e) {
|
---|
139 | }
|
---|
140 |
|
---|
141 | }
|
---|