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.List;
011
012import javax.swing.JOptionPane;
013
014import org.openstreetmap.josm.Main;
015import org.openstreetmap.josm.data.notes.Note;
016import org.openstreetmap.josm.data.osm.IPrimitive;
017import org.openstreetmap.josm.data.osm.OsmData;
018import org.openstreetmap.josm.data.osm.OsmPrimitive;
019import org.openstreetmap.josm.gui.HelpAwareOptionPane;
020import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
021import org.openstreetmap.josm.gui.MainApplication;
022import org.openstreetmap.josm.gui.help.HelpUtil;
023import org.openstreetmap.josm.tools.ImageProvider;
024import org.openstreetmap.josm.tools.Logging;
025import org.openstreetmap.josm.tools.OpenBrowser;
026import org.openstreetmap.josm.tools.Shortcut;
027
028/**
029 * Abstract base class for info actions, opening an URL describing a particular object.
030 * @since 1697
031 */
032public abstract class AbstractInfoAction extends JosmAction {
033
034    /**
035     * Constructs a new {@code AbstractInfoAction}.
036     * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
037     */
038    public AbstractInfoAction(boolean installAdapters) {
039        super(installAdapters);
040    }
041
042    /**
043     * Constructs a new {@code AbstractInfoAction}.
044     * @param name the action's text as displayed on the menu (if it is added to a menu)
045     * @param iconName the filename of the icon to use
046     * @param tooltip  a longer description of the action that will be displayed in the tooltip. Please note
047     *           that html is not supported for menu actions on some platforms.
048     * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
049     *            do want a shortcut, remember you can always register it with group=none, so you
050     *            won't be assigned a shortcut unless the user configures one. If you pass null here,
051     *            the user CANNOT configure a shortcut for your action.
052     * @param register register this action for the toolbar preferences?
053     * @param toolbarId identifier for the toolbar preferences. The iconName is used, if this parameter is null
054     * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
055     */
056    public AbstractInfoAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register,
057            String toolbarId, boolean installAdapters) {
058        super(name, iconName, tooltip, shortcut, register, toolbarId, installAdapters);
059    }
060
061    /**
062     * Asks user confirmation before launching a large number of browser windows.
063     * @param numBrowsers the number of browser windows to open
064     * @return {@code true} if the user confirms, {@code false} otherwise
065     */
066    public static boolean confirmLaunchMultiple(int numBrowsers) {
067        String msg = /* for correct i18n of plural forms - see #9110 */ trn(
068                "You are about to launch {0} browser window.<br>"
069                        + "This may both clutter your screen with browser windows<br>"
070                        + "and take some time to finish.",
071                "You are about to launch {0} browser windows.<br>"
072                        + "This may both clutter your screen with browser windows<br>"
073                        + "and take some time to finish.", numBrowsers, numBrowsers);
074        ButtonSpec[] spec = new ButtonSpec[] {
075                new ButtonSpec(
076                        tr("Continue"),
077                        new ImageProvider("ok"),
078                        trn("Click to continue and to open {0} browser", "Click to continue and to open {0} browsers",
079                                numBrowsers, numBrowsers),
080                        null // no specific help topic
081                ),
082                new ButtonSpec(
083                        tr("Cancel"),
084                        new ImageProvider("cancel"),
085                        tr("Click to abort launching external browsers"),
086                        null // no specific help topic
087                )
088        };
089        return 0 == HelpAwareOptionPane.showOptionDialog(
090                Main.parent,
091                new StringBuilder(msg).insert(0, "<html>").append("</html>").toString(),
092                tr("Warning"),
093                JOptionPane.WARNING_MESSAGE,
094                null,
095                spec,
096                spec[0],
097                HelpUtil.ht("/WarningMessages#ToManyBrowsersToOpen")
098        );
099    }
100
101    protected void launchInfoBrowsersForSelectedPrimitivesAndNote() {
102        List<IPrimitive> primitivesToShow = new ArrayList<>();
103        OsmData<?, ?, ?, ?> ds = getLayerManager().getActiveData();
104        if (ds != null) {
105            primitivesToShow.addAll(ds.getAllSelected());
106        }
107
108        Note noteToShow = MainApplication.isDisplayingMapView() ? MainApplication.getMap().noteDialog.getSelectedNote() : null;
109
110        // filter out new primitives which are not yet uploaded to the server
111        //
112        primitivesToShow.removeIf(IPrimitive::isNew);
113
114        if (primitivesToShow.isEmpty() && noteToShow == null) {
115            JOptionPane.showMessageDialog(
116                    Main.parent,
117                    tr("Please select at least one already uploaded node, way, or relation."),
118                    tr("Warning"),
119                    JOptionPane.WARNING_MESSAGE
120            );
121            return;
122        }
123
124        // don't launch more than 10 browser instances / browser windows
125        //
126        int max = Math.min(10, primitivesToShow.size());
127        if (primitivesToShow.size() > max && !confirmLaunchMultiple(primitivesToShow.size()))
128            return;
129        for (int i = 0; i < max; i++) {
130            launchInfoBrowser(primitivesToShow.get(i));
131        }
132
133        if (noteToShow != null) {
134            launchInfoBrowser(noteToShow);
135        }
136    }
137
138    protected final void launchInfoBrowser(Object o) {
139        String url = createInfoUrl(o);
140        if (url != null) {
141            String result = OpenBrowser.displayUrl(url);
142            if (result != null) {
143                Logging.warn(result);
144            }
145        }
146    }
147
148    @Override
149    public void actionPerformed(ActionEvent e) {
150        launchInfoBrowsersForSelectedPrimitivesAndNote();
151    }
152
153    protected abstract String createInfoUrl(Object infoObject);
154
155    @Override
156    protected void updateEnabledState() {
157        OsmData<?, ?, ?, ?> ds = getLayerManager().getActiveData();
158        setEnabled(ds != null && !ds.selectionEmpty());
159    }
160
161    @Override
162    protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
163        setEnabled(selection != null && !selection.isEmpty());
164    }
165}