Ticket #15110: audio-disable.patch

File audio-disable.patch, 12.5 KB (added by bafonins, 7 years ago)
  • src/org/openstreetmap/josm/actions/audio/AudioBackAction.java

     
    1010import java.io.IOException;
    1111
    1212import org.openstreetmap.josm.Main;
    13 import org.openstreetmap.josm.actions.JosmAction;
    1413import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
    1514import org.openstreetmap.josm.io.audio.AudioPlayer;
    1615import org.openstreetmap.josm.io.audio.AudioUtil;
     
    2019 * Jump the audio backward 10 seconds and start playing if paused.
    2120 * @since 547
    2221 */
    23 public class AudioBackAction extends JosmAction {
     22public class AudioBackAction extends BaseAudioAction {
    2423
    2524    /**
    2625     * Constructs a new {@code AudioBackAction}.
     
    3534    public void actionPerformed(ActionEvent e) {
    3635        try {
    3736            if (AudioPlayer.playing() || AudioPlayer.paused())
    38                 AudioPlayer.play(AudioPlayer.url(), AudioPlayer.position()
     37                AudioPlayer.play(AudioPlayer.url().orElse(null), AudioPlayer.position()
    3938                - Main.pref.getDouble("audio.forwardbackamount", 10.0));
    4039            else
    4140                MarkerLayer.playAudio();
     
    4342            AudioUtil.audioMalfunction(ex);
    4443        }
    4544    }
    46 }
     45}
     46 No newline at end of file
  • src/org/openstreetmap/josm/actions/audio/AudioFastSlowAction.java

     
    55import java.io.IOException;
    66
    77import org.openstreetmap.josm.Main;
    8 import org.openstreetmap.josm.actions.JosmAction;
    98import org.openstreetmap.josm.io.audio.AudioPlayer;
    109import org.openstreetmap.josm.io.audio.AudioUtil;
    1110import org.openstreetmap.josm.tools.Shortcut;
     
    1413 * Abstract superclass of {@link AudioFasterAction} and {@link AudioSlowerAction}.
    1514 * @since 563
    1615 */
    17 public abstract class AudioFastSlowAction extends JosmAction {
     16public abstract class AudioFastSlowAction extends BaseAudioAction {
    1817
    1918    private double multiplier;
    2019
     
    4140            return;
    4241        try {
    4342            if (AudioPlayer.playing() || AudioPlayer.paused())
    44                 AudioPlayer.play(AudioPlayer.url(), AudioPlayer.position(), speed * multiplier);
     43                AudioPlayer.play(AudioPlayer.url().orElse(null), AudioPlayer.position(), speed * multiplier);
    4544        } catch (IOException | InterruptedException ex) {
    4645            AudioUtil.audioMalfunction(ex);
    4746        }
    4847    }
    49 }
     48}
     49 No newline at end of file
  • src/org/openstreetmap/josm/actions/audio/AudioFwdAction.java

     
    99import java.io.IOException;
    1010
    1111import org.openstreetmap.josm.Main;
    12 import org.openstreetmap.josm.actions.JosmAction;
    1312import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
    1413import org.openstreetmap.josm.io.audio.AudioPlayer;
    1514import org.openstreetmap.josm.io.audio.AudioUtil;
     
    1918 * Jump the audio forward 10 seconds.
    2019 * @since 547
    2120 */
    22 public class AudioFwdAction extends JosmAction {
     21public class AudioFwdAction extends BaseAudioAction {
    2322
    2423    /**
    2524     * Constructs a new {@code AudioFwdAction}.
     
    3332    public void actionPerformed(ActionEvent e) {
    3433        try {
    3534            if (AudioPlayer.playing() || AudioPlayer.paused())
    36                 AudioPlayer.play(AudioPlayer.url(), AudioPlayer.position()
     35                AudioPlayer.play(AudioPlayer.url().orElse(null), AudioPlayer.position()
    3736                + Main.pref.getDouble("audio.forwardbackamount", 10.0));
    3837            else
    3938                MarkerLayer.playAudio();
     
    4140            AudioUtil.audioMalfunction(ex);
    4241        }
    4342    }
    44 }
     43}
     44 No newline at end of file
  • src/org/openstreetmap/josm/actions/audio/AudioNextAction.java

     
    77import java.awt.event.ActionEvent;
    88import java.awt.event.KeyEvent;
    99
    10 import org.openstreetmap.josm.actions.JosmAction;
    1110import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
    1211import org.openstreetmap.josm.tools.Shortcut;
    1312
     
    1615 * Play from the first such Marker if none has been played, or repeat the last marker if at the end.
    1716 * @since 547
    1817 */
    19 public class AudioNextAction extends JosmAction {
     18public class AudioNextAction extends BaseAudioAction {
    2019
    2120    /**
    2221     * Constructs a new {@code AudioNextAction}.
     
    3029    public void actionPerformed(ActionEvent e) {
    3130        MarkerLayer.playNextMarker();
    3231    }
    33 }
     32}
     33 No newline at end of file
  • src/org/openstreetmap/josm/actions/audio/AudioPlayPauseAction.java

     
    99import java.io.IOException;
    1010import java.net.URL;
    1111
    12 import org.openstreetmap.josm.actions.JosmAction;
    1312import org.openstreetmap.josm.gui.layer.markerlayer.AudioMarker;
    1413import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
    1514import org.openstreetmap.josm.io.audio.AudioPlayer;
     
    2322 * If fast forwarding or slow forwarding, resume normal speed.
    2423 * @since 547
    2524 */
    26 public class AudioPlayPauseAction extends JosmAction {
     25public class AudioPlayPauseAction extends BaseAudioAction {
    2726
    2827    /**
    2928     * Constructs a new {@code AudioPlayPauseAction}.
     
    3534
    3635    @Override
    3736    public void actionPerformed(ActionEvent e) {
    38         URL url = AudioPlayer.url();
     37        URL url = AudioPlayer.url().orElse(null);
    3938        try {
    4039            if (url != null && AudioPlayer.paused()) {
    4140                AudioPlayer.play(url);
     
    5857            AudioUtil.audioMalfunction(ex);
    5958        }
    6059    }
    61 }
     60}
     61 No newline at end of file
  • src/org/openstreetmap/josm/actions/audio/AudioPrevAction.java

     
    77import java.awt.event.ActionEvent;
    88import java.awt.event.KeyEvent;
    99
    10 import org.openstreetmap.josm.actions.JosmAction;
    1110import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
    1211import org.openstreetmap.josm.tools.Shortcut;
    1312
     
    1615 * Play from the first such Marker if none has been played or already at the first marker.
    1716 * @since 547
    1817 */
    19 public class AudioPrevAction extends JosmAction {
     18public class AudioPrevAction extends BaseAudioAction {
    2019
    2120    /**
    2221     * Constructs a new {@code AudioPrevAction}.
     
    3029    public void actionPerformed(ActionEvent e) {
    3130        MarkerLayer.playPreviousMarker();
    3231    }
    33 }
     32}
     33 No newline at end of file
  • src/org/openstreetmap/josm/actions/audio/BaseAudioAction.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.actions.audio;
     3
     4import org.openstreetmap.josm.Main;
     5import org.openstreetmap.josm.actions.JosmAction;
     6import org.openstreetmap.josm.gui.layer.LayerManager;
     7import org.openstreetmap.josm.gui.layer.markerlayer.AudioMarker;
     8import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
     9import org.openstreetmap.josm.tools.Shortcut;
     10
     11/**
     12 * Base class for every action related to audio content.
     13 */
     14public abstract class BaseAudioAction extends JosmAction {
     15
     16    public BaseAudioAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean registerInToolbar) {
     17        super(name, iconName, tooltip, shortcut, registerInToolbar);
     18
     19        // initial check
     20        setEnabled(isAudioMarkerPresent());
     21
     22        // reevaluate presence of audio markers each time layouts change
     23        Main.getLayerManager().addLayerChangeListener(getLayerChangeListener());
     24    }
     25
     26    /**
     27     * Checks if there is at least one {@link AudioMarker} is present in the current layout.
     28     * @return {@code true} if at least one {@link AudioMarker} is present in the current
     29     * layout, {@code false} otherwise.
     30     */
     31    protected boolean isAudioMarkerPresent() {
     32        return Main.getLayerManager().getLayers().stream()
     33                .filter(l -> l instanceof MarkerLayer)
     34                .map(ml -> (MarkerLayer) ml)
     35                .flatMap(ml -> ml.data.stream())
     36                .anyMatch(m -> m instanceof AudioMarker);
     37    }
     38
     39    /**
     40     * Creates {@link LayerManager.LayerChangeListener} that disabled/enables the audio
     41     * actions depending if there is at least one {@link AudioMarker} present in the
     42     * current layout.
     43     * @return An instance of {@link LayerManager.LayerChangeListener}.
     44     */
     45    private LayerManager.LayerChangeListener getLayerChangeListener() {
     46        return new LayerManager.LayerChangeListener() {
     47            @Override
     48            public void layerAdded(LayerManager.LayerAddEvent e) {
     49                setEnabled(isAudioMarkerPresent());
     50            }
     51
     52            @Override
     53            public void layerRemoving(LayerManager.LayerRemoveEvent e) {
     54                setEnabled(isAudioMarkerPresent());
     55            }
     56
     57            @Override
     58            public void layerOrderChanged(LayerManager.LayerOrderChangeEvent e) {
     59                // nothing
     60            }
     61        };
     62    }
     63}
  • src/org/openstreetmap/josm/gui/layer/markerlayer/MarkerLayer.java

     
    303303
    304304        // make our new marker
    305305        AudioMarker newAudioMarker = new AudioMarker(coor,
    306                 null, AudioPlayer.url(), this, time, offset);
     306                null, AudioPlayer.url().orElse(null), this, time, offset);
    307307
    308308        // insert it at the right place in a copy the collection
    309309        Collection<Marker> newData = new ArrayList<>();
     
    570570            invalidate();
    571571        }
    572572    }
    573 }
     573}
     574 No newline at end of file
  • src/org/openstreetmap/josm/io/audio/AudioPlayer.java

     
    33
    44import java.io.IOException;
    55import java.net.URL;
     6import java.util.Optional;
    67
    78import org.openstreetmap.josm.Main;
    89import org.openstreetmap.josm.tools.JosmRuntimeException;
     
    154155
    155156    /**
    156157     * To get the Url of the playing or recently played audio.
    157      * @return url - could be null
     158     * @return url The Url of the playing or recently played audio.
    158159     */
    159     public static URL url() {
     160    public static Optional<URL> url() {
    160161        AudioPlayer instance = AudioPlayer.getInstance();
    161         return instance == null ? null : instance.playingUrl;
     162        return Optional.ofNullable(instance == null ? null : instance.playingUrl);
    162163    }
    163164
    164165    /**
     
    167168     */
    168169    public static boolean paused() {
    169170        AudioPlayer instance = AudioPlayer.getInstance();
    170         return instance == null ? false : (instance.state == State.PAUSED);
     171        return instance != null && (instance.state == State.PAUSED);
    171172    }
    172173
    173174    /**
     
    176177     */
    177178    public static boolean playing() {
    178179        AudioPlayer instance = AudioPlayer.getInstance();
    179         return instance == null ? false : (instance.state == State.PLAYING);
     180        return instance != null && (instance.state == State.PLAYING);
    180181    }
    181182
    182183    /**
     
    311312    public void playing(URL playingURL) {
    312313        this.playingUrl = playingURL;
    313314    }
    314 }
     315}
     316 No newline at end of file