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.notes.Note;
017import org.openstreetmap.josm.data.osm.OsmPrimitive;
018import org.openstreetmap.josm.gui.HelpAwareOptionPane;
019import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
020import org.openstreetmap.josm.gui.help.HelpUtil;
021import org.openstreetmap.josm.tools.ImageProvider;
022import org.openstreetmap.josm.tools.OpenBrowser;
023import org.openstreetmap.josm.tools.Shortcut;
024
025public abstract class AbstractInfoAction extends JosmAction {
026
027    public AbstractInfoAction(boolean installAdapters) {
028        super(installAdapters);
029    }
030
031    public AbstractInfoAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register,
032            String toolbarId, boolean installAdapters) {
033        super(name, iconName, tooltip, shortcut, register, toolbarId, installAdapters);
034    }
035
036    public static boolean confirmLaunchMultiple(int numBrowsers) {
037        String msg  = /* for correct i18n of plural forms - see #9110 */ trn(
038                "You are about to launch {0} browser window.<br>"
039                        + "This may both clutter your screen with browser windows<br>"
040                        + "and take some time to finish.",
041                "You are about to launch {0} browser windows.<br>"
042                        + "This may both clutter your screen with browser windows<br>"
043                        + "and take some time to finish.", numBrowsers, numBrowsers);
044        msg = "<html>" + msg + "</html>";
045        ButtonSpec[] spec = new ButtonSpec[] {
046                new ButtonSpec(
047                        tr("Continue"),
048                        ImageProvider.get("ok"),
049                        trn("Click to continue and to open {0} browser", "Click to continue and to open {0} browsers",
050                                numBrowsers, numBrowsers),
051                        null // no specific help topic
052                ),
053                new ButtonSpec(
054                        tr("Cancel"),
055                        ImageProvider.get("cancel"),
056                        tr("Click to abort launching external browsers"),
057                        null // no specific help topic
058                )
059        };
060        int ret = HelpAwareOptionPane.showOptionDialog(
061                Main.parent,
062                msg,
063                tr("Warning"),
064                JOptionPane.WARNING_MESSAGE,
065                null,
066                spec,
067                spec[0],
068                HelpUtil.ht("/WarningMessages#ToManyBrowsersToOpen")
069        );
070        return ret == 0;
071    }
072
073    protected void launchInfoBrowsersForSelectedPrimitivesAndNote() {
074        List<OsmPrimitive> primitivesToShow = new ArrayList<>();
075        if (getCurrentDataSet() != null) {
076            primitivesToShow.addAll(getCurrentDataSet().getAllSelected());
077        }
078
079        Note noteToShow = Main.isDisplayingMapView() ? Main.map.noteDialog.getSelectedNote() : null;
080
081        // filter out new primitives which are not yet uploaded to the server
082        //
083        Iterator<OsmPrimitive> it = primitivesToShow.iterator();
084        while (it.hasNext()) {
085            if (it.next().isNew()) {
086                it.remove();
087            }
088        }
089
090        if (primitivesToShow.isEmpty() && noteToShow == null) {
091            JOptionPane.showMessageDialog(
092                    Main.parent,
093                    tr("Please select at least one already uploaded node, way, or relation."),
094                    tr("Warning"),
095                    JOptionPane.WARNING_MESSAGE
096            );
097            return;
098        }
099
100        // don't launch more than 10 browser instances / browser windows
101        //
102        int max = Math.min(10, primitivesToShow.size());
103        if (primitivesToShow.size() > max && !confirmLaunchMultiple(primitivesToShow.size()))
104            return;
105        for (int i = 0; i < max; i++) {
106            launchInfoBrowser(primitivesToShow.get(i));
107        }
108
109        if (noteToShow != null) {
110            launchInfoBrowser(noteToShow);
111        }
112    }
113
114    protected final void launchInfoBrowser(Object o) {
115        String url = createInfoUrl(o);
116        if (url != null) {
117            String result = OpenBrowser.displayUrl(url);
118            if (result != null) {
119                Main.warn(result);
120            }
121        }
122    }
123
124    @Override
125    public void actionPerformed(ActionEvent e) {
126        launchInfoBrowsersForSelectedPrimitivesAndNote();
127    }
128
129    protected abstract String createInfoUrl(Object infoObject);
130
131    @Override
132    protected void updateEnabledState() {
133        setEnabled(getCurrentDataSet() != null && !getCurrentDataSet().getSelected().isEmpty());
134    }
135
136    @Override
137    protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
138        setEnabled(selection != null && !selection.isEmpty());
139    }
140}