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.Iterator;
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | import javax.swing.JOptionPane;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
17 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
18 | import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
|
---|
19 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
20 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
21 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
22 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
23 |
|
---|
24 | public abstract class AbstractInfoAction extends JosmAction {
|
---|
25 |
|
---|
26 | public AbstractInfoAction(boolean installAdapters) {
|
---|
27 | super(installAdapters);
|
---|
28 | }
|
---|
29 |
|
---|
30 | public AbstractInfoAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register, String toolbarId, boolean installAdapters) {
|
---|
31 | super(name, iconName, tooltip, shortcut, register, toolbarId, installAdapters);
|
---|
32 | }
|
---|
33 |
|
---|
34 | public static boolean confirmLaunchMultiple(int numBrowsers) {
|
---|
35 | String msg = /* for correct i18n of plural forms - see #9110 */ trn(
|
---|
36 | "You are about to launch {0} browser window.<br>"
|
---|
37 | + "This may both clutter your screen with browser windows<br>"
|
---|
38 | + "and take some time to finish.",
|
---|
39 | "You are about to launch {0} browser windows.<br>"
|
---|
40 | + "This may both clutter your screen with browser windows<br>"
|
---|
41 | + "and take some time to finish.", numBrowsers, numBrowsers);
|
---|
42 | msg = "<html>" + msg + "</html>";
|
---|
43 | ButtonSpec[] spec = new ButtonSpec[] {
|
---|
44 | new ButtonSpec(
|
---|
45 | tr("Continue"),
|
---|
46 | ImageProvider.get("ok"),
|
---|
47 | trn("Click to continue and to open {0} browser", "Click to continue and to open {0} browsers", numBrowsers, numBrowsers),
|
---|
48 | null // no specific help topic
|
---|
49 | ),
|
---|
50 | new ButtonSpec(
|
---|
51 | tr("Cancel"),
|
---|
52 | ImageProvider.get("cancel"),
|
---|
53 | tr("Click to abort launching external browsers"),
|
---|
54 | null // no specific help topic
|
---|
55 | )
|
---|
56 | };
|
---|
57 | int ret = HelpAwareOptionPane.showOptionDialog(
|
---|
58 | Main.parent,
|
---|
59 | msg,
|
---|
60 | tr("Warning"),
|
---|
61 | JOptionPane.WARNING_MESSAGE,
|
---|
62 | null,
|
---|
63 | spec,
|
---|
64 | spec[0],
|
---|
65 | HelpUtil.ht("/WarningMessages#ToManyBrowsersToOpen")
|
---|
66 | );
|
---|
67 | return ret == 0;
|
---|
68 | }
|
---|
69 |
|
---|
70 | protected void launchInfoBrowsersForSelectedPrimitives() {
|
---|
71 | List<OsmPrimitive> primitivesToShow = new ArrayList<>(getCurrentDataSet().getAllSelected());
|
---|
72 |
|
---|
73 | // filter out new primitives which are not yet uploaded to the server
|
---|
74 | //
|
---|
75 | Iterator<OsmPrimitive> it = primitivesToShow.iterator();
|
---|
76 | while(it.hasNext()) {
|
---|
77 | if (it.next().isNew()) {
|
---|
78 | it.remove();
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | if (primitivesToShow.isEmpty()) {
|
---|
83 | JOptionPane.showMessageDialog(
|
---|
84 | Main.parent,
|
---|
85 | tr("Please select at least one already uploaded node, way, or relation."),
|
---|
86 | tr("Warning"),
|
---|
87 | JOptionPane.WARNING_MESSAGE
|
---|
88 | );
|
---|
89 | return;
|
---|
90 | }
|
---|
91 |
|
---|
92 | // don't launch more than 10 browser instances / browser windows
|
---|
93 | //
|
---|
94 | int max = Math.min(10, primitivesToShow.size());
|
---|
95 | if (primitivesToShow.size() > max && ! confirmLaunchMultiple(primitivesToShow.size()))
|
---|
96 | return;
|
---|
97 | for(int i = 0; i < max; i++) {
|
---|
98 | OpenBrowser.displayUrl(createInfoUrl(primitivesToShow.get(i)));
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | @Override
|
---|
103 | public void actionPerformed(ActionEvent e) {
|
---|
104 | launchInfoBrowsersForSelectedPrimitives();
|
---|
105 | }
|
---|
106 |
|
---|
107 | protected abstract String createInfoUrl(Object infoObject);
|
---|
108 |
|
---|
109 | @Override
|
---|
110 | protected void updateEnabledState() {
|
---|
111 | setEnabled(getCurrentDataSet() != null && !getCurrentDataSet().getSelected().isEmpty());
|
---|
112 | }
|
---|
113 |
|
---|
114 | @Override
|
---|
115 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
116 | setEnabled(selection != null && !selection.isEmpty());
|
---|
117 | }
|
---|
118 | }
|
---|