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