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.Rectangle;
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.text.MessageFormat;
|
---|
9 | import java.util.Arrays;
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | import javax.swing.AbstractAction;
|
---|
13 | import javax.swing.Action;
|
---|
14 | import javax.swing.JComponent;
|
---|
15 | import javax.swing.JPopupMenu;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.data.StructUtils;
|
---|
19 | import org.openstreetmap.josm.data.StructUtils.StructEntry;
|
---|
20 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
21 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
22 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * A menu displaying links to external history viewers for a changeset.
|
---|
26 | *
|
---|
27 | * @since 12871
|
---|
28 | */
|
---|
29 | public class OpenChangesetPopupMenu extends JPopupMenu {
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Constructs a new {@code OpenChangesetPopupMenu} for the given changeset id.
|
---|
33 | *
|
---|
34 | * @param changesetId the changeset id
|
---|
35 | */
|
---|
36 | public OpenChangesetPopupMenu(final long changesetId) {
|
---|
37 | StructUtils.getListOfStructs(Config.getPref(), "history-dialog.tools", DEFAULT_ENTRIES, ChangesetViewerEntry.class)
|
---|
38 | .stream()
|
---|
39 | .map(entry -> entry.toAction(changesetId))
|
---|
40 | .forEach(this::add);
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Displays the popup menu at the lower-left corner of {@code parent}.
|
---|
45 | *
|
---|
46 | * @param parent the parent component to use for positioning this menu
|
---|
47 | */
|
---|
48 | public void show(final JComponent parent) {
|
---|
49 | final Rectangle r = parent.getBounds();
|
---|
50 | show(parent.getParent(), r.x, r.y + r.height);
|
---|
51 | }
|
---|
52 |
|
---|
53 | private static final List<ChangesetViewerEntry> DEFAULT_ENTRIES = Arrays.asList(
|
---|
54 | new ChangesetViewerEntry(tr("View changeset in web browser"), Main.getBaseBrowseUrl() + "/changeset/{0}"),
|
---|
55 | new ChangesetViewerEntry(tr("Open {0}", "achavi (Augmented OSM Change Viewer)"), "https://overpass-api.de/achavi/?changeset={0}"),
|
---|
56 | new ChangesetViewerEntry(tr("Open {0}", "OSMCha (OSM Changeset Analyzer)"), "https://osmcha.mapbox.com/changesets/{0}"),
|
---|
57 | new ChangesetViewerEntry(tr("Open {0}", "OSM History Viewer"), "http://osmhv.openstreetmap.de/changeset.jsp?id={0}"),
|
---|
58 | new ChangesetViewerEntry(tr("Open {0}", "WhoDidIt (OSM Changeset Analyzer)"),
|
---|
59 | "http://simon04.dev.openstreetmap.org/whodidit/index.html?changeset={0}&show=1")
|
---|
60 | );
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Auxiliary class to save a link to a history viewer in the preferences.
|
---|
64 | */
|
---|
65 | public static class ChangesetViewerEntry {
|
---|
66 | /** Name to be displayed in popup menu */
|
---|
67 | @StructEntry
|
---|
68 | public String name;
|
---|
69 | /** Templated service url. <code>{0}</code> will be replaced by changeset id */
|
---|
70 | @StructEntry
|
---|
71 | public String url;
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Constructs a new {@code ChangesetViewerEntry}.
|
---|
75 | */
|
---|
76 | public ChangesetViewerEntry() {
|
---|
77 | }
|
---|
78 |
|
---|
79 | ChangesetViewerEntry(String name, String url) {
|
---|
80 | this.name = name;
|
---|
81 | this.url = url;
|
---|
82 | }
|
---|
83 |
|
---|
84 | Action toAction(final long changesetId) {
|
---|
85 | return new OpenBrowserAction(name, MessageFormat.format(this.url, Long.toString(changesetId)));
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | static class OpenBrowserAction extends AbstractAction {
|
---|
90 | final String url;
|
---|
91 |
|
---|
92 | OpenBrowserAction(String name, String url) {
|
---|
93 | super(name);
|
---|
94 | putValue(SHORT_DESCRIPTION, tr("Open {0}", url));
|
---|
95 | new ImageProvider("help/internet").getResource().attachImageIcon(this, true);
|
---|
96 | this.url = url;
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Override
|
---|
100 | public void actionPerformed(ActionEvent e) {
|
---|
101 | OpenBrowser.displayUrl(url);
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|