1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import javax.swing.JOptionPane;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.data.notes.Note;
|
---|
14 | import org.openstreetmap.josm.data.osm.IPrimitive;
|
---|
15 | import org.openstreetmap.josm.data.osm.OsmData;
|
---|
16 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
17 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
18 | import org.openstreetmap.josm.tools.Logging;
|
---|
19 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
20 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
21 | import org.openstreetmap.josm.tools.Utils;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Abstract base class for info actions, opening an URL describing a particular object.
|
---|
25 | * @since 1697
|
---|
26 | */
|
---|
27 | public abstract class AbstractInfoAction extends JosmAction {
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Constructs a new {@code AbstractInfoAction}.
|
---|
31 | * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
|
---|
32 | */
|
---|
33 | protected AbstractInfoAction(boolean installAdapters) {
|
---|
34 | super(installAdapters);
|
---|
35 | }
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Constructs a new {@code AbstractInfoAction}.
|
---|
39 | * @param name the action's text as displayed on the menu (if it is added to a menu)
|
---|
40 | * @param iconName the filename of the icon to use
|
---|
41 | * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note
|
---|
42 | * that html is not supported for menu actions on some platforms.
|
---|
43 | * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
|
---|
44 | * do want a shortcut, remember you can always register it with group=none, so you
|
---|
45 | * won't be assigned a shortcut unless the user configures one. If you pass null here,
|
---|
46 | * the user CANNOT configure a shortcut for your action.
|
---|
47 | * @param register register this action for the toolbar preferences?
|
---|
48 | * @param toolbarId identifier for the toolbar preferences. The iconName is used, if this parameter is null
|
---|
49 | * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
|
---|
50 | */
|
---|
51 | protected AbstractInfoAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register,
|
---|
52 | String toolbarId, boolean installAdapters) {
|
---|
53 | super(name, iconName, tooltip, shortcut, register, toolbarId, installAdapters);
|
---|
54 | }
|
---|
55 |
|
---|
56 | protected void launchInfoBrowsersForSelectedPrimitivesAndNote() {
|
---|
57 | List<IPrimitive> primitivesToShow = new ArrayList<>();
|
---|
58 | OsmData<?, ?, ?, ?> ds = getLayerManager().getActiveData();
|
---|
59 | if (ds != null) {
|
---|
60 | primitivesToShow.addAll(ds.getAllSelected());
|
---|
61 | }
|
---|
62 |
|
---|
63 | Note noteToShow = MainApplication.isDisplayingMapView() ? MainApplication.getMap().noteDialog.getSelectedNote() : null;
|
---|
64 |
|
---|
65 | // filter out new primitives which are not yet uploaded to the server
|
---|
66 | //
|
---|
67 | primitivesToShow.removeIf(IPrimitive::isNew);
|
---|
68 |
|
---|
69 | if (primitivesToShow.isEmpty() && noteToShow == null) {
|
---|
70 | JOptionPane.showMessageDialog(
|
---|
71 | MainApplication.getMainFrame(),
|
---|
72 | tr("Please select at least one already uploaded node, way, or relation."),
|
---|
73 | tr("Warning"),
|
---|
74 | JOptionPane.WARNING_MESSAGE
|
---|
75 | );
|
---|
76 | return;
|
---|
77 | }
|
---|
78 |
|
---|
79 | // don't launch more than 10 browser instances / browser windows
|
---|
80 | //
|
---|
81 | int max = Math.min(10, primitivesToShow.size());
|
---|
82 | if (primitivesToShow.size() > max && !OpenBrowserAction.confirmLaunchMultiple(primitivesToShow.size()))
|
---|
83 | return;
|
---|
84 | for (int i = 0; i < max; i++) {
|
---|
85 | launchInfoBrowser(primitivesToShow.get(i));
|
---|
86 | }
|
---|
87 |
|
---|
88 | if (noteToShow != null) {
|
---|
89 | launchInfoBrowser(noteToShow);
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | protected final void launchInfoBrowser(Object o) {
|
---|
94 | String url = createInfoUrl(o);
|
---|
95 | if (url != null) {
|
---|
96 | String result = OpenBrowser.displayUrl(url);
|
---|
97 | if (result != null) {
|
---|
98 | Logging.warn(result);
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | @Override
|
---|
104 | public void actionPerformed(ActionEvent e) {
|
---|
105 | launchInfoBrowsersForSelectedPrimitivesAndNote();
|
---|
106 | }
|
---|
107 |
|
---|
108 | protected abstract String createInfoUrl(Object infoObject);
|
---|
109 |
|
---|
110 | @Override
|
---|
111 | protected void updateEnabledState() {
|
---|
112 | OsmData<?, ?, ?, ?> ds = getLayerManager().getActiveData();
|
---|
113 | setEnabled(ds != null && !ds.selectionEmpty());
|
---|
114 | }
|
---|
115 |
|
---|
116 | @Override
|
---|
117 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
118 | setEnabled(!Utils.isEmpty(selection));
|
---|
119 | }
|
---|
120 | }
|
---|