001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions;
003
004import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
005import static org.openstreetmap.josm.tools.I18n.tr;
006
007import java.awt.event.KeyEvent;
008import java.util.Collection;
009
010import org.openstreetmap.josm.Main;
011import org.openstreetmap.josm.data.notes.Note;
012import org.openstreetmap.josm.data.osm.OsmPrimitive;
013import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
014import org.openstreetmap.josm.gui.MainApplication;
015import org.openstreetmap.josm.tools.Shortcut;
016
017/**
018 * Display object information about OSM nodes, ways, or relations in web browser.
019 * @since 4408
020 */
021public class InfoWebAction extends AbstractInfoAction {
022
023    /**
024     * Constructs a new {@code InfoWebAction}.
025     */
026    public InfoWebAction() {
027        super(tr("Advanced info (web)"), "info",
028                tr("Display object information about OSM nodes, ways, or relations in web browser."),
029                Shortcut.registerShortcut("core:infoweb",
030                        tr("Advanced info (web)"), KeyEvent.VK_I, Shortcut.CTRL_SHIFT),
031                true, "action/infoweb", true);
032        putValue("help", ht("/Action/InfoAboutElementsWeb"));
033    }
034
035    @Override
036    protected String createInfoUrl(Object infoObject) {
037        if (infoObject instanceof OsmPrimitive) {
038            OsmPrimitive primitive = (OsmPrimitive) infoObject;
039            return Main.getBaseBrowseUrl() + '/' + OsmPrimitiveType.from(primitive).getAPIName() + '/' + primitive.getId();
040        } else if (infoObject instanceof Note) {
041            Note note = (Note) infoObject;
042            return Main.getBaseBrowseUrl() + "/note/" + note.getId();
043        } else {
044            return null;
045        }
046    }
047
048    @Override
049    protected void updateEnabledState() {
050        super.updateEnabledState();
051        updateEnabledStateWithNotes();
052    }
053
054    @Override
055    protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
056        super.updateEnabledState(selection);
057        updateEnabledStateWithNotes();
058    }
059
060    private void updateEnabledStateWithNotes() {
061        // Allows enabling if a note is selected, even if no OSM object is selected
062        if (!isEnabled() && MainApplication.isDisplayingMapView() && MainApplication.getMap().noteDialog.getSelectedNote() != null) {
063            setEnabled(true);
064        }
065    }
066
067    /**
068     * Called when the note selection has changed.
069     * TODO: make a proper listener mechanism to handle change of note selection
070     * @since 8475
071     */
072    public final void noteSelectionChanged() {
073        updateEnabledState();
074    }
075}