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