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 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | import javax.swing.JOptionPane;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.data.notes.Note;
|
---|
16 | import org.openstreetmap.josm.data.osm.AbstractPrimitive;
|
---|
17 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
18 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
19 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
20 | import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
|
---|
21 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
22 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
23 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
24 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Abstract base class for info actions, opening an URL describing a particular object.
|
---|
28 | * @since 1697
|
---|
29 | */
|
---|
30 | public abstract class AbstractInfoAction extends JosmAction {
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Constructs a new {@code AbstractInfoAction}.
|
---|
34 | * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
|
---|
35 | */
|
---|
36 | public AbstractInfoAction(boolean installAdapters) {
|
---|
37 | super(installAdapters);
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Constructs a new {@code AbstractInfoAction}.
|
---|
42 | * @param name the action's text as displayed on the menu (if it is added to a menu)
|
---|
43 | * @param iconName the filename of the icon to use
|
---|
44 | * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note
|
---|
45 | * that html is not supported for menu actions on some platforms.
|
---|
46 | * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
|
---|
47 | * do want a shortcut, remember you can always register it with group=none, so you
|
---|
48 | * won't be assigned a shortcut unless the user configures one. If you pass null here,
|
---|
49 | * the user CANNOT configure a shortcut for your action.
|
---|
50 | * @param register register this action for the toolbar preferences?
|
---|
51 | * @param toolbarId identifier for the toolbar preferences. The iconName is used, if this parameter is null
|
---|
52 | * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
|
---|
53 | */
|
---|
54 | public AbstractInfoAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register,
|
---|
55 | String toolbarId, boolean installAdapters) {
|
---|
56 | super(name, iconName, tooltip, shortcut, register, toolbarId, installAdapters);
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Asks user confirmation before launching a large number of browser windows.
|
---|
61 | * @param numBrowsers the number of browser windows to open
|
---|
62 | * @return {@code true} if the user confirms, {@code false} otherwise
|
---|
63 | */
|
---|
64 | public static boolean confirmLaunchMultiple(int numBrowsers) {
|
---|
65 | String msg = /* for correct i18n of plural forms - see #9110 */ trn(
|
---|
66 | "You are about to launch {0} browser window.<br>"
|
---|
67 | + "This may both clutter your screen with browser windows<br>"
|
---|
68 | + "and take some time to finish.",
|
---|
69 | "You are about to launch {0} browser windows.<br>"
|
---|
70 | + "This may both clutter your screen with browser windows<br>"
|
---|
71 | + "and take some time to finish.", numBrowsers, numBrowsers);
|
---|
72 | ButtonSpec[] spec = new ButtonSpec[] {
|
---|
73 | new ButtonSpec(
|
---|
74 | tr("Continue"),
|
---|
75 | ImageProvider.get("ok"),
|
---|
76 | trn("Click to continue and to open {0} browser", "Click to continue and to open {0} browsers",
|
---|
77 | numBrowsers, numBrowsers),
|
---|
78 | null // no specific help topic
|
---|
79 | ),
|
---|
80 | new ButtonSpec(
|
---|
81 | tr("Cancel"),
|
---|
82 | ImageProvider.get("cancel"),
|
---|
83 | tr("Click to abort launching external browsers"),
|
---|
84 | null // no specific help topic
|
---|
85 | )
|
---|
86 | };
|
---|
87 | return 0 == HelpAwareOptionPane.showOptionDialog(
|
---|
88 | Main.parent,
|
---|
89 | new StringBuilder(msg).insert(0, "<html>").append("</html>").toString(),
|
---|
90 | tr("Warning"),
|
---|
91 | JOptionPane.WARNING_MESSAGE,
|
---|
92 | null,
|
---|
93 | spec,
|
---|
94 | spec[0],
|
---|
95 | HelpUtil.ht("/WarningMessages#ToManyBrowsersToOpen")
|
---|
96 | );
|
---|
97 | }
|
---|
98 |
|
---|
99 | protected void launchInfoBrowsersForSelectedPrimitivesAndNote() {
|
---|
100 | List<OsmPrimitive> primitivesToShow = new ArrayList<>();
|
---|
101 | DataSet ds = getLayerManager().getEditDataSet();
|
---|
102 | if (ds != null) {
|
---|
103 | primitivesToShow.addAll(ds.getAllSelected());
|
---|
104 | }
|
---|
105 |
|
---|
106 | Note noteToShow = Main.isDisplayingMapView() ? Main.map.noteDialog.getSelectedNote() : null;
|
---|
107 |
|
---|
108 | // filter out new primitives which are not yet uploaded to the server
|
---|
109 | //
|
---|
110 | primitivesToShow.removeIf(AbstractPrimitive::isNew);
|
---|
111 |
|
---|
112 | if (primitivesToShow.isEmpty() && noteToShow == null) {
|
---|
113 | JOptionPane.showMessageDialog(
|
---|
114 | Main.parent,
|
---|
115 | tr("Please select at least one already uploaded node, way, or relation."),
|
---|
116 | tr("Warning"),
|
---|
117 | JOptionPane.WARNING_MESSAGE
|
---|
118 | );
|
---|
119 | return;
|
---|
120 | }
|
---|
121 |
|
---|
122 | // don't launch more than 10 browser instances / browser windows
|
---|
123 | //
|
---|
124 | int max = Math.min(10, primitivesToShow.size());
|
---|
125 | if (primitivesToShow.size() > max && !confirmLaunchMultiple(primitivesToShow.size()))
|
---|
126 | return;
|
---|
127 | for (int i = 0; i < max; i++) {
|
---|
128 | launchInfoBrowser(primitivesToShow.get(i));
|
---|
129 | }
|
---|
130 |
|
---|
131 | if (noteToShow != null) {
|
---|
132 | launchInfoBrowser(noteToShow);
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | protected final void launchInfoBrowser(Object o) {
|
---|
137 | String url = createInfoUrl(o);
|
---|
138 | if (url != null) {
|
---|
139 | String result = OpenBrowser.displayUrl(url);
|
---|
140 | if (result != null) {
|
---|
141 | Main.warn(result);
|
---|
142 | }
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | @Override
|
---|
147 | public void actionPerformed(ActionEvent e) {
|
---|
148 | launchInfoBrowsersForSelectedPrimitivesAndNote();
|
---|
149 | }
|
---|
150 |
|
---|
151 | protected abstract String createInfoUrl(Object infoObject);
|
---|
152 |
|
---|
153 | @Override
|
---|
154 | protected void updateEnabledState() {
|
---|
155 | DataSet ds = getLayerManager().getEditDataSet();
|
---|
156 | setEnabled(ds != null && !ds.selectionEmpty());
|
---|
157 | }
|
---|
158 |
|
---|
159 | @Override
|
---|
160 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
161 | setEnabled(selection != null && !selection.isEmpty());
|
---|
162 | }
|
---|
163 | }
|
---|