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 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
6 |
|
---|
7 | import java.awt.BorderLayout;
|
---|
8 | import java.awt.Component;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.KeyEvent;
|
---|
11 | import java.awt.event.MouseAdapter;
|
---|
12 | import java.awt.event.MouseEvent;
|
---|
13 | import java.io.UnsupportedEncodingException;
|
---|
14 | import java.net.URLEncoder;
|
---|
15 | import java.text.NumberFormat;
|
---|
16 | import java.util.ArrayList;
|
---|
17 | import java.util.Collection;
|
---|
18 | import java.util.Collections;
|
---|
19 | import java.util.HashMap;
|
---|
20 | import java.util.HashSet;
|
---|
21 | import java.util.Iterator;
|
---|
22 | import java.util.LinkedList;
|
---|
23 | import java.util.List;
|
---|
24 | import java.util.Map;
|
---|
25 | import java.util.Set;
|
---|
26 |
|
---|
27 | import javax.swing.AbstractAction;
|
---|
28 | import javax.swing.ImageIcon;
|
---|
29 | import javax.swing.JLabel;
|
---|
30 | import javax.swing.JOptionPane;
|
---|
31 | import javax.swing.JPanel;
|
---|
32 | import javax.swing.JScrollPane;
|
---|
33 | import javax.swing.JTable;
|
---|
34 | import javax.swing.ListSelectionModel;
|
---|
35 | import javax.swing.event.ListSelectionEvent;
|
---|
36 | import javax.swing.event.ListSelectionListener;
|
---|
37 | import javax.swing.table.DefaultTableCellRenderer;
|
---|
38 | import javax.swing.table.DefaultTableModel;
|
---|
39 | import javax.swing.table.TableColumnModel;
|
---|
40 |
|
---|
41 | import org.openstreetmap.josm.Main;
|
---|
42 | import org.openstreetmap.josm.actions.AbstractInfoAction;
|
---|
43 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
44 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
45 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
46 | import org.openstreetmap.josm.data.osm.User;
|
---|
47 | import org.openstreetmap.josm.gui.MapView;
|
---|
48 | import org.openstreetmap.josm.gui.SideButton;
|
---|
49 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
50 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
51 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
52 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Displays a dialog with all users who have last edited something in the
|
---|
56 | * selection area, along with the number of objects.
|
---|
57 | *
|
---|
58 | */
|
---|
59 | public class UserListDialog extends ToggleDialog implements SelectionChangedListener, MapView.LayerChangeListener {
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * The display list.
|
---|
63 | */
|
---|
64 | private JTable userTable;
|
---|
65 | private UserTableModel model;
|
---|
66 | private SelectUsersPrimitivesAction selectionUsersPrimitivesAction;
|
---|
67 | private ShowUserInfoAction showUserInfoAction;
|
---|
68 | private LoadRelicensingInformationAction loadRelicensingInformationAction;
|
---|
69 |
|
---|
70 | public UserListDialog() {
|
---|
71 | super(tr("Authors"), "userlist", tr("Open a list of people working on the selected objects."),
|
---|
72 | Shortcut.registerShortcut("subwindow:authors", tr("Toggle: {0}", tr("Authors")), KeyEvent.VK_A, Shortcut.GROUP_LAYER, Shortcut.SHIFT_DEFAULT), 150);
|
---|
73 |
|
---|
74 | build();
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Override
|
---|
78 | public void showNotify() {
|
---|
79 | DataSet.addSelectionListener(this);
|
---|
80 | MapView.addLayerChangeListener(this);
|
---|
81 | }
|
---|
82 |
|
---|
83 | @Override
|
---|
84 | public void hideNotify() {
|
---|
85 | MapView.removeLayerChangeListener(this);
|
---|
86 | DataSet.removeSelectionListener(this);
|
---|
87 | }
|
---|
88 |
|
---|
89 | protected JPanel buildButtonRow() {
|
---|
90 | JPanel pnl = getButtonPanel(2);
|
---|
91 |
|
---|
92 | // -- select users primitives action
|
---|
93 | //
|
---|
94 | selectionUsersPrimitivesAction = new SelectUsersPrimitivesAction();
|
---|
95 | userTable.getSelectionModel().addListSelectionListener(selectionUsersPrimitivesAction);
|
---|
96 | pnl.add(new SideButton(selectionUsersPrimitivesAction));
|
---|
97 |
|
---|
98 | // -- info action
|
---|
99 | //
|
---|
100 | showUserInfoAction = new ShowUserInfoAction();
|
---|
101 | userTable.getSelectionModel().addListSelectionListener(showUserInfoAction);
|
---|
102 | pnl.add(new SideButton(showUserInfoAction));
|
---|
103 |
|
---|
104 | // -- load relicensing info action
|
---|
105 | loadRelicensingInformationAction = new LoadRelicensingInformationAction();
|
---|
106 | pnl.add(new SideButton(loadRelicensingInformationAction));
|
---|
107 | return pnl;
|
---|
108 | }
|
---|
109 |
|
---|
110 | protected void build() {
|
---|
111 | JPanel pnl = new JPanel();
|
---|
112 | pnl.setLayout(new BorderLayout());
|
---|
113 | model = new UserTableModel();
|
---|
114 | userTable = new JTable(model);
|
---|
115 | userTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
---|
116 | TableColumnModel columnModel = userTable.getColumnModel();
|
---|
117 | columnModel.getColumn(3).setPreferredWidth(20);
|
---|
118 | columnModel.getColumn(3).setCellRenderer(new DefaultTableCellRenderer() {
|
---|
119 | @Override
|
---|
120 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
121 | final JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
|
---|
122 | label.setIcon((ImageIcon)value);
|
---|
123 | label.setText("");
|
---|
124 | return label;
|
---|
125 | };
|
---|
126 | });
|
---|
127 | pnl.add(new JScrollPane(userTable), BorderLayout.CENTER);
|
---|
128 |
|
---|
129 | // -- the button row
|
---|
130 | pnl.add(buildButtonRow(), BorderLayout.SOUTH);
|
---|
131 | userTable.addMouseListener(new DoubleClickAdapter());
|
---|
132 | add(pnl, BorderLayout.CENTER);
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Called when the selection in the dataset changed.
|
---|
137 | * @param newSelection The new selection array.
|
---|
138 | */
|
---|
139 | public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
|
---|
140 | refresh(newSelection);
|
---|
141 | }
|
---|
142 |
|
---|
143 | public void activeLayerChange(Layer oldLayer, Layer newLayer) {
|
---|
144 | if (newLayer instanceof OsmDataLayer) {
|
---|
145 | refresh(((OsmDataLayer) newLayer).data.getSelected());
|
---|
146 | } else {
|
---|
147 | refresh(null);
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | public void layerAdded(Layer newLayer) {
|
---|
152 | // do nothing
|
---|
153 | }
|
---|
154 |
|
---|
155 | public void layerRemoved(Layer oldLayer) {
|
---|
156 | // do nothing
|
---|
157 | }
|
---|
158 |
|
---|
159 | public void refresh(Collection<? extends OsmPrimitive> fromPrimitives) {
|
---|
160 | model.populate(fromPrimitives);
|
---|
161 | if(model.getRowCount() != 0) {
|
---|
162 | setTitle(trn("{0} Author", "{0} Authors", model.getRowCount() , model.getRowCount()));
|
---|
163 | } else {
|
---|
164 | setTitle(tr("Authors"));
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | @Override
|
---|
169 | public void showDialog() {
|
---|
170 | super.showDialog();
|
---|
171 | User.initRelicensingInformation();
|
---|
172 | Layer layer = Main.main.getActiveLayer();
|
---|
173 | if (layer instanceof OsmDataLayer) {
|
---|
174 | refresh(((OsmDataLayer)layer).data.getSelected());
|
---|
175 | }
|
---|
176 |
|
---|
177 | }
|
---|
178 |
|
---|
179 | class SelectUsersPrimitivesAction extends AbstractAction implements ListSelectionListener{
|
---|
180 | public SelectUsersPrimitivesAction() {
|
---|
181 | putValue(NAME, tr("Select"));
|
---|
182 | putValue(SHORT_DESCRIPTION, tr("Select objects submitted by this user"));
|
---|
183 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "select"));
|
---|
184 | updateEnabledState();
|
---|
185 | }
|
---|
186 |
|
---|
187 | public void select() {
|
---|
188 | int indexes[] = userTable.getSelectedRows();
|
---|
189 | if (indexes == null || indexes.length == 0) return;
|
---|
190 | model.selectPrimitivesOwnedBy(userTable.getSelectedRows());
|
---|
191 | }
|
---|
192 |
|
---|
193 | public void actionPerformed(ActionEvent e) {
|
---|
194 | select();
|
---|
195 | }
|
---|
196 |
|
---|
197 | protected void updateEnabledState() {
|
---|
198 | setEnabled(userTable != null && userTable.getSelectedRowCount() > 0);
|
---|
199 | }
|
---|
200 |
|
---|
201 | public void valueChanged(ListSelectionEvent e) {
|
---|
202 | updateEnabledState();
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | /*
|
---|
207 | * Action for launching the info page of a user
|
---|
208 | */
|
---|
209 | class ShowUserInfoAction extends AbstractInfoAction implements ListSelectionListener {
|
---|
210 |
|
---|
211 | public ShowUserInfoAction() {
|
---|
212 | super(false);
|
---|
213 | putValue(NAME, tr("Show info"));
|
---|
214 | putValue(SHORT_DESCRIPTION, tr("Launches a browser with information about the user"));
|
---|
215 | putValue(SMALL_ICON, ImageProvider.get("about"));
|
---|
216 | updateEnabledState();
|
---|
217 | }
|
---|
218 |
|
---|
219 | @Override
|
---|
220 | public void actionPerformed(ActionEvent e) {
|
---|
221 | int rows[] = userTable.getSelectedRows();
|
---|
222 | if (rows == null || rows.length == 0) return;
|
---|
223 | List<User> users = model.getSelectedUsers(rows);
|
---|
224 | if (users.isEmpty()) return;
|
---|
225 | if (users.size() > 10) {
|
---|
226 | System.out.println(tr("Warning: only launching info browsers for the first {0} of {1} selected users", 10, users.size()));
|
---|
227 | }
|
---|
228 | int num = Math.min(10, users.size());
|
---|
229 | Iterator<User> it = users.iterator();
|
---|
230 | while(it.hasNext() && num > 0) {
|
---|
231 | String url = createInfoUrl(it.next());
|
---|
232 | if (url == null) {
|
---|
233 | break;
|
---|
234 | }
|
---|
235 | launchBrowser(url);
|
---|
236 | num--;
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | @Override
|
---|
241 | protected String createInfoUrl(Object infoObject) {
|
---|
242 | User user = (User)infoObject;
|
---|
243 | try {
|
---|
244 | return getBaseUserUrl() + "/" + URLEncoder.encode(user.getName(), "UTF-8").replaceAll("\\+", "%20");
|
---|
245 | } catch(UnsupportedEncodingException e) {
|
---|
246 | e.printStackTrace();
|
---|
247 | JOptionPane.showMessageDialog(
|
---|
248 | Main.parent,
|
---|
249 | tr("<html>Failed to create an URL because the encoding ''{0}''<br>"
|
---|
250 | + "was missing on this system.</html>", "UTF-8"),
|
---|
251 | tr("Missing encoding"),
|
---|
252 | JOptionPane.ERROR_MESSAGE
|
---|
253 | );
|
---|
254 | return null;
|
---|
255 | }
|
---|
256 | }
|
---|
257 |
|
---|
258 | @Override
|
---|
259 | protected void updateEnabledState() {
|
---|
260 | setEnabled(userTable != null && userTable.getSelectedRowCount() > 0);
|
---|
261 | }
|
---|
262 |
|
---|
263 | public void valueChanged(ListSelectionEvent e) {
|
---|
264 | updateEnabledState();
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 | /*
|
---|
269 | */
|
---|
270 | class LoadRelicensingInformationAction extends AbstractAction {
|
---|
271 |
|
---|
272 | public LoadRelicensingInformationAction() {
|
---|
273 | super();
|
---|
274 | putValue(NAME, tr("Load CT"));
|
---|
275 | putValue(SHORT_DESCRIPTION, tr("Loads information about relicensing status from the server. Users having agreed to the new contributor terms will show a green check mark."));
|
---|
276 | putValue(SMALL_ICON, ImageProvider.get("about"));
|
---|
277 | }
|
---|
278 |
|
---|
279 | @Override
|
---|
280 | public void actionPerformed(ActionEvent e) {
|
---|
281 | User.loadRelicensingInformation(true);
|
---|
282 | Layer layer = Main.main.getActiveLayer();
|
---|
283 | if (layer instanceof OsmDataLayer) {
|
---|
284 | refresh(((OsmDataLayer)layer).data.getSelected());
|
---|
285 | }
|
---|
286 | setEnabled(false);
|
---|
287 | }
|
---|
288 | }
|
---|
289 |
|
---|
290 | class DoubleClickAdapter extends MouseAdapter {
|
---|
291 | @Override
|
---|
292 | public void mouseClicked(MouseEvent e) {
|
---|
293 | if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount()==2) {
|
---|
294 | selectionUsersPrimitivesAction.select();
|
---|
295 | }
|
---|
296 | }
|
---|
297 | }
|
---|
298 |
|
---|
299 | /**
|
---|
300 | * Action for selecting the primitives contributed by the currently selected
|
---|
301 | * users.
|
---|
302 | *
|
---|
303 | */
|
---|
304 | private static class UserInfo implements Comparable<UserInfo> {
|
---|
305 | public User user;
|
---|
306 | public int count;
|
---|
307 | public double percent;
|
---|
308 | UserInfo(User user, int count, double percent) {
|
---|
309 | this.user=user;
|
---|
310 | this.count=count;
|
---|
311 | this.percent = percent;
|
---|
312 | }
|
---|
313 | public int compareTo(UserInfo o) {
|
---|
314 | if (count < o.count) return 1;
|
---|
315 | if (count > o.count) return -1;
|
---|
316 | if (user== null || user.getName() == null) return 1;
|
---|
317 | if (o.user == null || o.user.getName() == null) return -1;
|
---|
318 | return user.getName().compareTo(o.user.getName());
|
---|
319 | }
|
---|
320 |
|
---|
321 | public String getName() {
|
---|
322 | if (user == null)
|
---|
323 | return tr("<new object>");
|
---|
324 | return user.getName();
|
---|
325 | }
|
---|
326 |
|
---|
327 | public int getRelicensingStatus() {
|
---|
328 | if (user == null)
|
---|
329 | return User.STATUS_UNKNOWN;
|
---|
330 | return user.getRelicensingStatus();
|
---|
331 | }
|
---|
332 | }
|
---|
333 |
|
---|
334 | /**
|
---|
335 | * The table model for the users
|
---|
336 | *
|
---|
337 | */
|
---|
338 | static class UserTableModel extends DefaultTableModel {
|
---|
339 | private ArrayList<UserInfo> data;
|
---|
340 | private ImageIcon greenCheckmark;
|
---|
341 | private ImageIcon greyCheckmark;
|
---|
342 | private ImageIcon redX;
|
---|
343 |
|
---|
344 | public UserTableModel() {
|
---|
345 | setColumnIdentifiers(new String[]{tr("Author"),tr("# Objects"),"%", tr("CT")});
|
---|
346 | data = new ArrayList<UserInfo>();
|
---|
347 | greenCheckmark = ImageProvider.get("misc", "green_check.png");
|
---|
348 | greyCheckmark = ImageProvider.get("misc", "grey_check.png");
|
---|
349 | redX = ImageProvider.get("misc", "red_x.png");
|
---|
350 | }
|
---|
351 |
|
---|
352 | protected Map<User, Integer> computeStatistics(Collection<? extends OsmPrimitive> primitives) {
|
---|
353 | HashMap<User, Integer> ret = new HashMap<User, Integer>();
|
---|
354 | if (primitives == null || primitives.isEmpty()) return ret;
|
---|
355 | for (OsmPrimitive primitive: primitives) {
|
---|
356 | if (ret.containsKey(primitive.getUser())) {
|
---|
357 | ret.put(primitive.getUser(), ret.get(primitive.getUser()) + 1);
|
---|
358 | } else {
|
---|
359 | ret.put(primitive.getUser(), 1);
|
---|
360 | }
|
---|
361 | }
|
---|
362 | return ret;
|
---|
363 | }
|
---|
364 |
|
---|
365 | public void populate(Collection<? extends OsmPrimitive> primitives) {
|
---|
366 | Map<User,Integer> statistics = computeStatistics(primitives);
|
---|
367 | data.clear();
|
---|
368 | if (primitives != null) {
|
---|
369 | for (Map.Entry<User, Integer> entry: statistics.entrySet()) {
|
---|
370 | data.add(new UserInfo(entry.getKey(), entry.getValue(), (double)entry.getValue() / (double)primitives.size()));
|
---|
371 | }
|
---|
372 | }
|
---|
373 | Collections.sort(data);
|
---|
374 | fireTableDataChanged();
|
---|
375 | }
|
---|
376 |
|
---|
377 | @Override
|
---|
378 | public int getRowCount() {
|
---|
379 | if (data == null) return 0;
|
---|
380 | return data.size();
|
---|
381 | }
|
---|
382 |
|
---|
383 | @Override
|
---|
384 | public Object getValueAt(int row, int column) {
|
---|
385 | UserInfo info = data.get(row);
|
---|
386 | switch(column) {
|
---|
387 | case 0: /* author */ return info.getName() == null ? "" : info.getName();
|
---|
388 | case 1: /* count */ return info.count;
|
---|
389 | case 2: /* percent */ return NumberFormat.getPercentInstance().format(info.percent);
|
---|
390 | case 3: /* relicensing status */
|
---|
391 | switch(info.getRelicensingStatus()) {
|
---|
392 | case User.STATUS_AGREED: return greenCheckmark;
|
---|
393 | case User.STATUS_AUTO_AGREED: return greyCheckmark;
|
---|
394 | case User.STATUS_NOT_AGREED: return redX;
|
---|
395 | default: return null;
|
---|
396 | }
|
---|
397 | }
|
---|
398 | return null;
|
---|
399 | }
|
---|
400 |
|
---|
401 | @Override
|
---|
402 | public boolean isCellEditable(int row, int column) {
|
---|
403 | return false;
|
---|
404 | }
|
---|
405 |
|
---|
406 | public void selectPrimitivesOwnedBy(int [] rows) {
|
---|
407 | Set<User> users= new HashSet<User>();
|
---|
408 | for (int index: rows) {
|
---|
409 | users.add(data.get(index).user);
|
---|
410 | }
|
---|
411 | Collection<OsmPrimitive> selected = Main.main.getCurrentDataSet().getSelected();
|
---|
412 | Collection<OsmPrimitive> byUser = new LinkedList<OsmPrimitive>();
|
---|
413 | for (OsmPrimitive p : selected) {
|
---|
414 | if (users.contains(p.getUser())) {
|
---|
415 | byUser.add(p);
|
---|
416 | }
|
---|
417 | }
|
---|
418 | Main.main.getCurrentDataSet().setSelected(byUser);
|
---|
419 | }
|
---|
420 |
|
---|
421 | public List<User> getSelectedUsers(int rows[]) {
|
---|
422 | LinkedList<User> ret = new LinkedList<User>();
|
---|
423 | if (rows == null || rows.length == 0) return ret;
|
---|
424 | for (int row: rows) {
|
---|
425 | if (data.get(row).user == null) {
|
---|
426 | continue;
|
---|
427 | }
|
---|
428 | ret.add(data.get(row).user);
|
---|
429 | }
|
---|
430 | return ret;
|
---|
431 | }
|
---|
432 | }
|
---|
433 | }
|
---|