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

Last change on this file since 13146 was 13043, checked in by Don-vip, 6 years ago

see #15474 - fix warnings

File size: 3.7 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}", "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}
Note: See TracBrowser for help on using the repository browser.