001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions.audio;
003
004import org.openstreetmap.josm.actions.JosmAction;
005import org.openstreetmap.josm.gui.MainApplication;
006import org.openstreetmap.josm.gui.layer.markerlayer.AudioMarker;
007import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
008import org.openstreetmap.josm.tools.Shortcut;
009
010/**
011 * Base class for every action related to audio content.
012 * @since 12565
013 */
014public abstract class AbstractAudioAction extends JosmAction {
015
016    /**
017     * Constructs a new {@code BaseAudioAction}.
018     * @param name the action's text as displayed on the menu (if it is added to a menu)
019     * @param iconName the filename of the icon to use
020     * @param tooltip a longer description of the action that will be displayed in the tooltip
021     * @param shortcut a ready-created shortcut object or null if you don't want a shortcut
022     * @param registerInToolbar register this action for the toolbar preferences?
023     */
024    public AbstractAudioAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean registerInToolbar) {
025        super(name, iconName, tooltip, shortcut, registerInToolbar);
026        updateEnabledState();
027    }
028
029    /**
030     * Checks if there is at least one {@link AudioMarker} is present in the current layout.
031     * @return {@code true} if at least one {@link AudioMarker} is present in the current
032     * layout, {@code false} otherwise.
033     */
034    protected static boolean isAudioMarkerPresent() {
035        return MainApplication.getLayerManager().getLayersOfType(MarkerLayer.class).stream()
036                .flatMap(ml -> ml.data.stream())
037                .anyMatch(m -> m instanceof AudioMarker);
038    }
039
040    @Override
041    protected void updateEnabledState() {
042        setEnabled(isAudioMarkerPresent());
043    }
044}