[2512] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
[6507] | 5 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
[2512] | 6 |
|
---|
| 7 | import java.awt.event.ActionEvent;
|
---|
| 8 | import java.util.ArrayList;
|
---|
| 9 | import java.util.Collection;
|
---|
| 10 | import java.util.Iterator;
|
---|
[6316] | 11 | import java.util.List;
|
---|
[2512] | 12 |
|
---|
| 13 | import javax.swing.JOptionPane;
|
---|
| 14 |
|
---|
| 15 | import org.openstreetmap.josm.Main;
|
---|
| 16 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[2616] | 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;
|
---|
[2512] | 21 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
| 22 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
| 23 |
|
---|
| 24 | public abstract class AbstractInfoAction extends JosmAction {
|
---|
| 25 |
|
---|
[3443] | 26 | public AbstractInfoAction(boolean installAdapters) {
|
---|
| 27 | super(installAdapters);
|
---|
[2512] | 28 | }
|
---|
| 29 |
|
---|
[4942] | 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);
|
---|
[2512] | 32 | }
|
---|
| 33 |
|
---|
[2616] | 34 | public static boolean confirmLaunchMultiple(int numBrowsers) {
|
---|
[6507] | 35 | String msg = /* for correct i18n of plural forms - see #9110 */ trn(
|
---|
[6679] | 36 | "You are about to launch {0} browser window.<br>"
|
---|
[6507] | 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);
|
---|
[2512] | 42 | msg = "<html>" + msg + "</html>";
|
---|
[2616] | 43 | ButtonSpec[] spec = new ButtonSpec[] {
|
---|
| 44 | new ButtonSpec(
|
---|
| 45 | tr("Continue"),
|
---|
| 46 | ImageProvider.get("ok"),
|
---|
[6679] | 47 | trn("Click to continue and to open {0} browser", "Click to continue and to open {0} browsers", numBrowsers, numBrowsers),
|
---|
[2616] | 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 | )
|
---|
[2512] | 56 | };
|
---|
[2616] | 57 | int ret = HelpAwareOptionPane.showOptionDialog(
|
---|
[2512] | 58 | Main.parent,
|
---|
| 59 | msg,
|
---|
| 60 | tr("Warning"),
|
---|
| 61 | JOptionPane.WARNING_MESSAGE,
|
---|
| 62 | null,
|
---|
[2616] | 63 | spec,
|
---|
| 64 | spec[0],
|
---|
| 65 | HelpUtil.ht("/WarningMessages#ToManyBrowsersToOpen")
|
---|
[2512] | 66 | );
|
---|
[2616] | 67 | return ret == 0;
|
---|
[2512] | 68 | }
|
---|
| 69 |
|
---|
| 70 | protected void launchInfoBrowsersForSelectedPrimitives() {
|
---|
[7005] | 71 | List<OsmPrimitive> primitivesToShow = new ArrayList<>(getCurrentDataSet().getAllSelected());
|
---|
[2512] | 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++) {
|
---|
[6743] | 98 | OpenBrowser.displayUrl(createInfoUrl(primitivesToShow.get(i)));
|
---|
[2512] | 99 | }
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[6084] | 102 | @Override
|
---|
[2512] | 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 | }
|
---|