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

Last change on this file since 1054 was 1054, checked in by stoecker, 16 years ago

fixed a lot of the shortcut related translations

  • Property svn:eol-style set to native
File size: 4.3 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.gui.dialogs;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.BorderLayout;
7import java.awt.event.KeyEvent;
8import java.awt.event.MouseEvent;
9import java.awt.event.MouseListener;
10import java.util.Arrays;
11import java.util.Collection;
12import java.util.Comparator;
13import java.util.HashMap;
14import java.util.LinkedList;
15
16import javax.swing.JScrollPane;
17import javax.swing.JTable;
18import javax.swing.ListSelectionModel;
19import javax.swing.table.DefaultTableModel;
20
21import org.openstreetmap.josm.Main;
22import org.openstreetmap.josm.data.SelectionChangedListener;
23import org.openstreetmap.josm.data.osm.DataSet;
24import org.openstreetmap.josm.data.osm.OsmPrimitive;
25import org.openstreetmap.josm.data.osm.User;
26import 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 */
34public 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}
Note: See TracBrowser for help on using the repository browser.