source: josm/trunk/src/org/openstreetmap/josm/gui/history/OpenChangesetPopupMenu.java@ 12871

Last change on this file since 12871 was 12871, checked in by simon04, 7 years ago

fix #14578 - Open external history viewers

Popup menu is added to version info of history dialog and changeset manager.

File size: 3.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.history;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Rectangle;
7import java.awt.event.ActionEvent;
8import java.text.MessageFormat;
9import java.util.Arrays;
10import java.util.List;
11
12import javax.swing.AbstractAction;
13import javax.swing.Action;
14import javax.swing.JComponent;
15import javax.swing.JPopupMenu;
16
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.data.StructUtils;
19import org.openstreetmap.josm.data.StructUtils.StructEntry;
20import org.openstreetmap.josm.spi.preferences.Config;
21import org.openstreetmap.josm.tools.ImageProvider;
22import org.openstreetmap.josm.tools.OpenBrowser;
23
24/**
25 * A menu displaying links to external history viewers for a changeset.
26 *
27 * @since 12871
28 */
29public 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}", "OSM History Viewer"), "http://osmhv.openstreetmap.de/changeset.jsp?id={0}"),
56 new ChangesetViewerEntry(tr("Open {0}", "achavi (Augmented OSM Change Viewer)"), "https://overpass-api.de/achavi/?changeset={0}")
57 );
58
59 /**
60 * Auxiliary class to save a link to a history viewer in the preferences.
61 */
62 public static class ChangesetViewerEntry {
63 @StructEntry
64 public String name;
65 @StructEntry
66 public String url;
67
68 /**
69 * Constructs a new {@code ChangesetViewerEntry}.
70 */
71 public ChangesetViewerEntry() {
72 }
73
74 ChangesetViewerEntry(String name, String url) {
75 this.name = name;
76 this.url = url;
77 }
78
79 Action toAction(final long changesetId) {
80 final String url = MessageFormat.format(this.url, Long.toString(changesetId));
81 return new OpenBrowserAction(name, url);
82 }
83 }
84
85 static class OpenBrowserAction extends AbstractAction {
86 final String url;
87
88 OpenBrowserAction(String name, String url) {
89 super(name);
90 putValue(SHORT_DESCRIPTION, tr("Open {0}", url));
91 new ImageProvider("help/internet").getResource().attachImageIcon(this, true);
92 this.url = url;
93 }
94
95 @Override
96 public void actionPerformed(ActionEvent e) {
97 OpenBrowser.displayUrl(url);
98 }
99 }
100}
Note: See TracBrowser for help on using the repository browser.