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

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

fix #16730 - make sure we try to display popup menus only if their parent is visible on screen

File size: 3.8 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.Container;
7import java.awt.Rectangle;
8import java.awt.event.ActionEvent;
9import java.text.MessageFormat;
10import java.util.Arrays;
11import java.util.List;
12
13import javax.swing.AbstractAction;
14import javax.swing.Action;
15import javax.swing.JComponent;
16import javax.swing.JPopupMenu;
17
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 Container parentParent = parent.getParent();
50 if (parentParent.isShowing()) {
51 final Rectangle r = parent.getBounds();
52 show(parentParent, r.x, r.y + r.height);
53 }
54 }
55
56 private static final List<ChangesetViewerEntry> DEFAULT_ENTRIES = Arrays.asList(
57 new ChangesetViewerEntry(tr("View changeset in web browser"), Config.getUrls().getBaseBrowseUrl() + "/changeset/{0}"),
58 new ChangesetViewerEntry(tr("Open {0}", "achavi (Augmented OSM Change Viewer)"), "https://overpass-api.de/achavi/?changeset={0}"),
59 new ChangesetViewerEntry(tr("Open {0}", "OSMCha (OSM Changeset Analyzer)"), "https://osmcha.mapbox.com/changesets/{0}"),
60 new ChangesetViewerEntry(tr("Open {0}", "OSM History Viewer"), "http://osmhv.openstreetmap.de/changeset.jsp?id={0}"),
61 new ChangesetViewerEntry(tr("Open {0}", "WhoDidIt (OSM Changeset Analyzer)"),
62 "http://simon04.dev.openstreetmap.org/whodidit/index.html?changeset={0}&show=1")
63 );
64
65 /**
66 * Auxiliary class to save a link to a history viewer in the preferences.
67 */
68 public static class ChangesetViewerEntry {
69 /** Name to be displayed in popup menu */
70 @StructEntry
71 public String name;
72 /** Templated service url. <code>{0}</code> will be replaced by changeset id */
73 @StructEntry
74 public String url;
75
76 /**
77 * Constructs a new {@code ChangesetViewerEntry}.
78 */
79 public ChangesetViewerEntry() {
80 }
81
82 ChangesetViewerEntry(String name, String url) {
83 this.name = name;
84 this.url = url;
85 }
86
87 Action toAction(final long changesetId) {
88 return new OpenBrowserAction(name, MessageFormat.format(this.url, Long.toString(changesetId)));
89 }
90 }
91
92 static class OpenBrowserAction extends AbstractAction {
93 final String url;
94
95 OpenBrowserAction(String name, String url) {
96 super(name);
97 putValue(SHORT_DESCRIPTION, tr("Open {0}", url));
98 new ImageProvider("help/internet").getResource().attachImageIcon(this, true);
99 this.url = url;
100 }
101
102 @Override
103 public void actionPerformed(ActionEvent e) {
104 OpenBrowser.displayUrl(url);
105 }
106 }
107}
Note: See TracBrowser for help on using the repository browser.