001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005import static org.openstreetmap.josm.tools.I18n.trn;
006
007import java.awt.event.ActionEvent;
008import java.util.ArrayList;
009import java.util.Collection;
010import java.util.Iterator;
011import java.util.List;
012
013import javax.swing.JOptionPane;
014
015import org.openstreetmap.josm.Main;
016import org.openstreetmap.josm.data.osm.OsmPrimitive;
017import org.openstreetmap.josm.gui.HelpAwareOptionPane;
018import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
019import org.openstreetmap.josm.gui.help.HelpUtil;
020import org.openstreetmap.josm.tools.ImageProvider;
021import org.openstreetmap.josm.tools.OpenBrowser;
022import org.openstreetmap.josm.tools.Shortcut;
023
024public abstract class AbstractInfoAction extends JosmAction {
025
026    public AbstractInfoAction(boolean installAdapters) {
027        super(installAdapters);
028    }
029
030    public AbstractInfoAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register, String toolbarId, boolean installAdapters) {
031        super(name, iconName, tooltip, shortcut, register, toolbarId, installAdapters);
032    }
033
034    public static boolean confirmLaunchMultiple(int numBrowsers) {
035        String msg  = /* for correct i18n of plural forms - see #9110 */ trn(
036                "You are about to launch {0} browser window.<br>"
037                        + "This may both clutter your screen with browser windows<br>"
038                        + "and take some time to finish.",
039                "You are about to launch {0} browser windows.<br>"
040                        + "This may both clutter your screen with browser windows<br>"
041                        + "and take some time to finish.", numBrowsers, numBrowsers);
042        msg = "<html>" + msg + "</html>";
043        ButtonSpec[] spec = new ButtonSpec[] {
044                new ButtonSpec(
045                        tr("Continue"),
046                        ImageProvider.get("ok"),
047                        trn("Click to continue and to open {0} browser", "Click to continue and to open {0} browsers", numBrowsers, numBrowsers),
048                        null // no specific help topic
049                ),
050                new ButtonSpec(
051                        tr("Cancel"),
052                        ImageProvider.get("cancel"),
053                        tr("Click to abort launching external browsers"),
054                        null // no specific help topic
055                )
056        };
057        int ret = HelpAwareOptionPane.showOptionDialog(
058                Main.parent,
059                msg,
060                tr("Warning"),
061                JOptionPane.WARNING_MESSAGE,
062                null,
063                spec,
064                spec[0],
065                HelpUtil.ht("/WarningMessages#ToManyBrowsersToOpen")
066        );
067        return ret == 0;
068    }
069
070    protected void launchInfoBrowsersForSelectedPrimitives() {
071        List<OsmPrimitive> primitivesToShow = new ArrayList<>(getCurrentDataSet().getAllSelected());
072
073        // filter out new primitives which are not yet uploaded to the server
074        //
075        Iterator<OsmPrimitive> it = primitivesToShow.iterator();
076        while(it.hasNext()) {
077            if (it.next().isNew()) {
078                it.remove();
079            }
080        }
081
082        if (primitivesToShow.isEmpty()) {
083            JOptionPane.showMessageDialog(
084                    Main.parent,
085                    tr("Please select at least one already uploaded node, way, or relation."),
086                    tr("Warning"),
087                    JOptionPane.WARNING_MESSAGE
088            );
089            return;
090        }
091
092        // don't launch more than 10 browser instances / browser windows
093        //
094        int max = Math.min(10, primitivesToShow.size());
095        if (primitivesToShow.size() > max && ! confirmLaunchMultiple(primitivesToShow.size()))
096            return;
097        for(int i = 0; i < max; i++) {
098            OpenBrowser.displayUrl(createInfoUrl(primitivesToShow.get(i)));
099        }
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}