001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.event.ActionEvent;
007import java.util.ArrayList;
008import java.util.Collection;
009import java.util.List;
010
011import javax.swing.JOptionPane;
012
013import org.openstreetmap.josm.data.notes.Note;
014import org.openstreetmap.josm.data.osm.IPrimitive;
015import org.openstreetmap.josm.data.osm.OsmData;
016import org.openstreetmap.josm.data.osm.OsmPrimitive;
017import org.openstreetmap.josm.gui.MainApplication;
018import org.openstreetmap.josm.tools.Logging;
019import org.openstreetmap.josm.tools.OpenBrowser;
020import org.openstreetmap.josm.tools.Shortcut;
021import org.openstreetmap.josm.tools.Utils;
022
023/**
024 * Abstract base class for info actions, opening an URL describing a particular object.
025 * @since 1697
026 */
027public abstract class AbstractInfoAction extends JosmAction {
028
029    /**
030     * Constructs a new {@code AbstractInfoAction}.
031     * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
032     */
033    protected AbstractInfoAction(boolean installAdapters) {
034        super(installAdapters);
035    }
036
037    /**
038     * Constructs a new {@code AbstractInfoAction}.
039     * @param name the action's text as displayed on the menu (if it is added to a menu)
040     * @param iconName the filename of the icon to use
041     * @param tooltip  a longer description of the action that will be displayed in the tooltip. Please note
042     *           that html is not supported for menu actions on some platforms.
043     * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
044     *            do want a shortcut, remember you can always register it with group=none, so you
045     *            won't be assigned a shortcut unless the user configures one. If you pass null here,
046     *            the user CANNOT configure a shortcut for your action.
047     * @param register register this action for the toolbar preferences?
048     * @param toolbarId identifier for the toolbar preferences. The iconName is used, if this parameter is null
049     * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
050     */
051    protected AbstractInfoAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register,
052            String toolbarId, boolean installAdapters) {
053        super(name, iconName, tooltip, shortcut, register, toolbarId, installAdapters);
054    }
055
056    protected void launchInfoBrowsersForSelectedPrimitivesAndNote() {
057        List<IPrimitive> primitivesToShow = new ArrayList<>();
058        OsmData<?, ?, ?, ?> ds = getLayerManager().getActiveData();
059        if (ds != null) {
060            primitivesToShow.addAll(ds.getAllSelected());
061        }
062
063        Note noteToShow = MainApplication.isDisplayingMapView() ? MainApplication.getMap().noteDialog.getSelectedNote() : null;
064
065        // filter out new primitives which are not yet uploaded to the server
066        //
067        primitivesToShow.removeIf(IPrimitive::isNew);
068
069        if (primitivesToShow.isEmpty() && noteToShow == null) {
070            JOptionPane.showMessageDialog(
071                    MainApplication.getMainFrame(),
072                    tr("Please select at least one already uploaded node, way, or relation."),
073                    tr("Warning"),
074                    JOptionPane.WARNING_MESSAGE
075            );
076            return;
077        }
078
079        // don't launch more than 10 browser instances / browser windows
080        //
081        int max = Math.min(10, primitivesToShow.size());
082        if (primitivesToShow.size() > max && !OpenBrowserAction.confirmLaunchMultiple(primitivesToShow.size()))
083            return;
084        for (int i = 0; i < max; i++) {
085            launchInfoBrowser(primitivesToShow.get(i));
086        }
087
088        if (noteToShow != null) {
089            launchInfoBrowser(noteToShow);
090        }
091    }
092
093    protected final void launchInfoBrowser(Object o) {
094        String url = createInfoUrl(o);
095        if (url != null) {
096            String result = OpenBrowser.displayUrl(url);
097            if (result != null) {
098                Logging.warn(result);
099            }
100        }
101    }
102
103    @Override
104    public void actionPerformed(ActionEvent e) {
105        launchInfoBrowsersForSelectedPrimitivesAndNote();
106    }
107
108    protected abstract String createInfoUrl(Object infoObject);
109
110    @Override
111    protected void updateEnabledState() {
112        OsmData<?, ?, ?, ?> ds = getLayerManager().getActiveData();
113        setEnabled(ds != null && !ds.selectionEmpty());
114    }
115
116    @Override
117    protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
118        setEnabled(!Utils.isEmpty(selection));
119    }
120}