    001// License: GPL. For details, see LICENSE file.
    002package org.openstreetmap.josm.plugins.streetside.actions;
    003
    004import static org.openstreetmap.josm.tools.I18n.tr;
    005
    006import java.awt.Dimension;
    007import java.awt.event.ActionEvent;
    008import java.util.ArrayList;
    009import java.util.List;
    010
    011import javax.swing.JDialog;
    012import javax.swing.JOptionPane;
    013
    014import org.openstreetmap.josm.Main;
    015import org.openstreetmap.josm.actions.JosmAction;
    016import org.openstreetmap.josm.plugins.streetside.StreetsideAbstractImage;
    017import org.openstreetmap.josm.plugins.streetside.StreetsideDataListener;
    018import org.openstreetmap.josm.plugins.streetside.StreetsideLayer;
    019import org.openstreetmap.josm.plugins.streetside.StreetsidePlugin;
    020import org.openstreetmap.josm.plugins.streetside.gui.StreetsideMainDialog;
    021import org.openstreetmap.josm.plugins.streetside.gui.StreetsideWalkDialog;
    022import org.openstreetmap.josm.tools.ImageProvider;
    023import org.openstreetmap.josm.tools.ImageProvider.ImageSizes;
    024
    025/**
    026 * Walks forward at a given interval.
    027 *
    028 * @author nokutu
    029 *
    030 */
    031public class StreetsideWalkAction extends JosmAction implements StreetsideDataListener {
    032
    033  private static final long serialVersionUID = 3454223919402245818L;
    034
    035  private WalkThread thread;
    036  private final List<WalkListener> listeners = new ArrayList<>();
    037
    038  /**
    039   *
    040   */
    041  public StreetsideWalkAction() {
    042    super(tr("Walk mode"), new ImageProvider(StreetsidePlugin.LOGO).setSize(ImageSizes.DEFAULT),
    043        tr("Walk mode"), null,
    044        false, "streetsideWalk", true);
    045  }
    046
    047  @Override
    048  public void actionPerformed(ActionEvent arg0) {
    049    StreetsideWalkDialog dialog = new StreetsideWalkDialog();
    050    JOptionPane pane = new JOptionPane(dialog, JOptionPane.PLAIN_MESSAGE,
    051        JOptionPane.OK_CANCEL_OPTION);
    052    JDialog dlg = pane.createDialog(Main.parent, tr("Walk mode"));
    053    dlg.setMinimumSize(new Dimension(400, 150));
    054    dlg.setVisible(true);
    055    if (pane.getValue() != null
    056        && (int) pane.getValue() == JOptionPane.OK_OPTION) {
    057      thread = new WalkThread((int) dialog.spin.getValue(),
    058          dialog.waitForPicture.isSelected(),
    059          dialog.followSelection.isSelected(), dialog.goForward.isSelected());
    060      fireWalkStarted();
    061      thread.start();
    062      StreetsideMainDialog.getInstance().setMode(StreetsideMainDialog.MODE.WALK);
    063    }
    064  }
    065
    066  @Override
    067  public void imagesAdded() {
    068    // Nothing
    069  }
    070
    071  /**
    072   * Adds a listener.
    073   *
    074   * @param lis
    075   *          The listener to be added.
    076   */
    077  public void addListener(WalkListener lis) {
    078    listeners.add(lis);
    079  }
    080
    081  /**
    082   * Removes a listener.
    083   *
    084   * @param lis
    085   *          The listener to be added.
    086   */
    087  public void removeListener(WalkListener lis) {
    088    listeners.remove(lis);
    089  }
    090
    091  private void fireWalkStarted() {
    092    if (listeners.isEmpty()) {
    093      return;
    094    }
    095    for (WalkListener lis : listeners) {
    096      lis.walkStarted(thread);
    097    }
    098  }
    099
    100  @Override
    101  protected boolean listenToSelectionChange() {
    102    return false;
    103  }
    104
    105  @Override
    106  public void selectedImageChanged(StreetsideAbstractImage oldImage, StreetsideAbstractImage newImage) {
    107    if (oldImage == null && newImage != null) {
    108      setEnabled(true);
    109    } else if (oldImage != null && newImage == null) {
    110      setEnabled(false);
    111    }
    112  }
    113
    114  /**
    115   * Enabled when a mapillary image is selected.
    116   */
    117  @Override
    118  protected void updateEnabledState() {
    119    super.updateEnabledState();
    120    setEnabled(StreetsideLayer.hasInstance() && StreetsideLayer.getInstance().getData().getSelectedImage() != null);
    121  }
    122
    123}