1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.history;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Container;
|
---|
7 | import java.awt.Rectangle;
|
---|
8 | import java.text.MessageFormat;
|
---|
9 | import java.util.Arrays;
|
---|
10 | import java.util.List;
|
---|
11 | import java.util.Objects;
|
---|
12 |
|
---|
13 | import javax.swing.Action;
|
---|
14 | import javax.swing.JComponent;
|
---|
15 | import javax.swing.JPopupMenu;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.actions.OpenBrowserAction;
|
---|
18 | import org.openstreetmap.josm.data.StructUtils;
|
---|
19 | import org.openstreetmap.josm.data.StructUtils.StructEntry;
|
---|
20 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
21 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * A menu displaying links to external history viewers for a changeset.
|
---|
25 | *
|
---|
26 | * @since 12871
|
---|
27 | */
|
---|
28 | public class OpenChangesetPopupMenu extends JPopupMenu {
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Constructs a new {@code OpenChangesetPopupMenu} for the given changeset id.
|
---|
32 | *
|
---|
33 | * @param changesetId the changeset id
|
---|
34 | * @param primitiveId the primitive id
|
---|
35 | * @since 14432
|
---|
36 | */
|
---|
37 | public OpenChangesetPopupMenu(final long changesetId, final PrimitiveId primitiveId) {
|
---|
38 | StructUtils.getListOfStructs(Config.getPref(), "history-dialog.tools", DEFAULT_ENTRIES, ChangesetViewerEntry.class)
|
---|
39 | .stream()
|
---|
40 | .map(entry -> entry.toAction(changesetId, primitiveId))
|
---|
41 | .filter(Objects::nonNull)
|
---|
42 | .forEach(this::add);
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Displays the popup menu at the lower-left corner of {@code parent}.
|
---|
47 | *
|
---|
48 | * @param parent the parent component to use for positioning this menu
|
---|
49 | */
|
---|
50 | public void show(final JComponent parent) {
|
---|
51 | Container parentParent = parent.getParent();
|
---|
52 | if (parentParent.isShowing()) {
|
---|
53 | final Rectangle r = parent.getBounds();
|
---|
54 | show(parentParent, r.x, r.y + r.height);
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | private static final List<ChangesetViewerEntry> DEFAULT_ENTRIES = Arrays.asList(
|
---|
59 | new ChangesetViewerEntry(tr("View changeset in web browser"), Config.getUrls().getBaseBrowseUrl() + "/changeset/{0}"),
|
---|
60 | new ChangesetViewerEntry(tr("Open {0}", "achavi (Augmented OSM Change Viewer)"), "https://overpass-api.de/achavi/?changeset={0}"),
|
---|
61 | new ChangesetViewerEntry(tr("Open {0}", "OSMCha (OSM Changeset Analyzer)"), "https://osmcha.mapbox.com/changesets/{0}"),
|
---|
62 | new ChangesetViewerEntry(tr("Open {0}", "OSM History Viewer (Mapki)"), "http://osm.mapki.com/history/{1}.php?id={2}"),
|
---|
63 | new ChangesetViewerEntry(tr("Open {0}", "OSM History Viewer (Pewu)"), "https://pewu.github.io/osm-history/#/{1}/{2}"),
|
---|
64 | new ChangesetViewerEntry(tr("Open {0}", "WhoDidIt (OSM Changeset Analyzer)"),
|
---|
65 | "http://simon04.dev.openstreetmap.org/whodidit/index.html?changeset={0}&show=1")
|
---|
66 | );
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Auxiliary class to save a link to a history viewer in the preferences.
|
---|
70 | */
|
---|
71 | public static class ChangesetViewerEntry {
|
---|
72 | /** Name to be displayed in popup menu */
|
---|
73 | @StructEntry
|
---|
74 | public String name;
|
---|
75 | /**
|
---|
76 | * Templated service url.
|
---|
77 | * <code>{0}</code> will be replaced by changeset id
|
---|
78 | * <code>{1}</code> will be replaced by object type (node, way, relation)
|
---|
79 | * <code>{2}</code> will be replaced by object id
|
---|
80 | */
|
---|
81 | @StructEntry
|
---|
82 | public String url;
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Constructs a new {@code ChangesetViewerEntry}.
|
---|
86 | */
|
---|
87 | public ChangesetViewerEntry() {
|
---|
88 | }
|
---|
89 |
|
---|
90 | ChangesetViewerEntry(String name, String url) {
|
---|
91 | this.name = Objects.requireNonNull(name);
|
---|
92 | this.url = Objects.requireNonNull(url);
|
---|
93 | }
|
---|
94 |
|
---|
95 | Action toAction(final long changesetId, PrimitiveId primitiveId) {
|
---|
96 | if (primitiveId != null) {
|
---|
97 | return new OpenBrowserAction(name, MessageFormat.format(url,
|
---|
98 | Long.toString(changesetId), primitiveId.getType().getAPIName(), Long.toString(primitiveId.getUniqueId())));
|
---|
99 | } else if (url.contains("{0}")) {
|
---|
100 | return new OpenBrowserAction(name, MessageFormat.format(url, Long.toString(changesetId)));
|
---|
101 | }
|
---|
102 | return null;
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | }
|
---|