1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 | import java.util.LinkedHashSet;
|
---|
10 | import java.util.Set;
|
---|
11 | import java.util.stream.Collectors;
|
---|
12 |
|
---|
13 | import javax.swing.JList;
|
---|
14 | import javax.swing.JTable;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.data.osm.OsmData;
|
---|
17 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
18 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
19 | import org.openstreetmap.josm.gui.dialogs.OsmIdSelectionDialog;
|
---|
20 | import org.openstreetmap.josm.gui.history.HistoryBrowserDialogManager;
|
---|
21 | import org.openstreetmap.josm.io.NetworkManager;
|
---|
22 | import org.openstreetmap.josm.io.OnlineResource;
|
---|
23 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Display history information about OSM ways, nodes, or relations.
|
---|
27 | * @since 968
|
---|
28 | */
|
---|
29 | public class HistoryInfoAction extends JosmAction {
|
---|
30 |
|
---|
31 | /** Action shortcut, made public in order to be used from {@code GettingStarted} page. */
|
---|
32 | public static final Shortcut SHORTCUT = Shortcut.registerShortcut("core:historyinfo", tr("View: {0}", tr("History")),
|
---|
33 | KeyEvent.VK_H, Shortcut.CTRL);
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Constructs a new {@code HistoryInfoAction}.
|
---|
37 | */
|
---|
38 | public HistoryInfoAction() {
|
---|
39 | super(tr("History"), "dialogs/history",
|
---|
40 | tr("Display history information about OSM ways, nodes, or relations."),
|
---|
41 | SHORTCUT, true, "action/historyinfo", false);
|
---|
42 | setHelpId(ht("/Action/ObjectHistory"));
|
---|
43 | setEnabled(true);
|
---|
44 | }
|
---|
45 |
|
---|
46 | @Override
|
---|
47 | public void actionPerformed(ActionEvent ae) {
|
---|
48 | // Generic handling of tables displaying OSM primitives
|
---|
49 | Set<PrimitiveId> sel = new LinkedHashSet<>();
|
---|
50 | if (ae.getSource() instanceof JTable) {
|
---|
51 | JTable table = (JTable) ae.getSource();
|
---|
52 | for (int row : table.getSelectedRows()) {
|
---|
53 | for (int col = 0; col < table.getModel().getColumnCount(); col++) {
|
---|
54 | Object value = table.getModel().getValueAt(row, col);
|
---|
55 | if (value instanceof PrimitiveId) {
|
---|
56 | sel.add((PrimitiveId) value);
|
---|
57 | break;
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|
61 | } else if (ae.getSource() instanceof JList) {
|
---|
62 | JList<?> list = (JList<?>) ae.getSource();
|
---|
63 | sel = list.getSelectedValuesList()
|
---|
64 | .stream().filter(v -> v instanceof PrimitiveId)
|
---|
65 | .map(v -> (PrimitiveId) v)
|
---|
66 | .collect(Collectors.toCollection(LinkedHashSet::new));
|
---|
67 | }
|
---|
68 | if (!sel.isEmpty()) {
|
---|
69 | HistoryBrowserDialogManager.getInstance().showHistory(sel);
|
---|
70 | return;
|
---|
71 | }
|
---|
72 | // Otherwise show history for currently selected objects
|
---|
73 | OsmData<?, ?, ?, ?> set = getLayerManager().getActiveData();
|
---|
74 | if (set != null && !set.selectionEmpty()) {
|
---|
75 | HistoryBrowserDialogManager.getInstance().showHistory(set.getAllSelected());
|
---|
76 | } else {
|
---|
77 | HistoryObjectIDDialog dialog = new HistoryObjectIDDialog();
|
---|
78 | if (dialog.showDialog().getValue() == dialog.getContinueButtonIndex()) {
|
---|
79 | HistoryBrowserDialogManager.getInstance().showHistory(dialog.getOsmIds());
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Dialog allowing to choose object id if no one is selected.
|
---|
86 | * @since 6448
|
---|
87 | */
|
---|
88 | public static class HistoryObjectIDDialog extends OsmIdSelectionDialog {
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Constructs a new {@code HistoryObjectIDDialog}.
|
---|
92 | */
|
---|
93 | public HistoryObjectIDDialog() {
|
---|
94 | super(MainApplication.getMainFrame(), tr("Show history"), tr("Show history"), tr("Cancel"));
|
---|
95 | setButtonIcons("dialogs/history", "cancel");
|
---|
96 | init();
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Override
|
---|
100 | public void setupDialog() {
|
---|
101 | super.setupDialog();
|
---|
102 | buttons.get(0).setEnabled(!NetworkManager.isOffline(OnlineResource.OSM_API));
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|