    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.awt.event.KeyEvent;
    009import java.io.IOException;
    010import java.util.ArrayList;
    011import java.util.Set;
    012import java.util.concurrent.ConcurrentSkipListSet;
    013
    014import javax.swing.JButton;
    015import javax.swing.JDialog;
    016import javax.swing.JOptionPane;
    017
    018import org.openstreetmap.josm.Main;
    019import org.openstreetmap.josm.actions.JosmAction;
    020import org.openstreetmap.josm.gui.MainApplication;
    021import org.openstreetmap.josm.plugins.streetside.StreetsideAbstractImage;
    022import org.openstreetmap.josm.plugins.streetside.StreetsideImage;
    023import org.openstreetmap.josm.plugins.streetside.StreetsideImportedImage;
    024import org.openstreetmap.josm.plugins.streetside.StreetsideLayer;
    025import org.openstreetmap.josm.plugins.streetside.StreetsidePlugin;
    026import org.openstreetmap.josm.plugins.streetside.gui.StreetsideExportDialog;
    027import org.openstreetmap.josm.plugins.streetside.io.export.StreetsideExportManager;
    028import org.openstreetmap.josm.tools.ImageProvider;
    029import org.openstreetmap.josm.tools.ImageProvider.ImageSizes;
    030import org.openstreetmap.josm.tools.Logging;
    031import org.openstreetmap.josm.tools.Shortcut;
    032
    033/**
    034 * Action that launches a StreetsideExportDialog and lets you export the images.
    035 *
    036 * @author nokutu
    037 *
    038 */
    039public class StreetsideExportAction extends JosmAction {
    040
    041  private static final long serialVersionUID = 6131359489725632369L;
    042
    043  private StreetsideExportDialog dialog;
    044
    045  /**
    046   * Main constructor.
    047   */
    048  public StreetsideExportAction() {
    049    super(tr("Export Streetside images"), new ImageProvider(StreetsidePlugin.LOGO).setSize(ImageSizes.DEFAULT),
    050        tr("Export Streetside images"), Shortcut.registerShortcut("Export Streetside",
    051            tr("Export Streetside images"), KeyEvent.CHAR_UNDEFINED,
    052            Shortcut.NONE), false, "streetsideExport", true);
    053    setEnabled(false);
    054  }
    055
    056  @Override
    057  public void actionPerformed(ActionEvent ae) {
    058    JOptionPane pane = new JOptionPane();
    059
    060    JButton ok = new JButton("Ok");
    061    ok.addActionListener(e -> pane.setValue(JOptionPane.OK_OPTION));
    062    JButton cancel = new JButton(tr("Cancel"));
    063    cancel.addActionListener(e -> pane.setValue(JOptionPane.CANCEL_OPTION));
    064
    065    dialog = new StreetsideExportDialog(ok);
    066    pane.setMessage(dialog);
    067    pane.setOptions(new JButton[] {ok, cancel});
    068
    069    JDialog dlg = pane.createDialog(Main.parent, tr("Export Streetside images"));
    070    dlg.setMinimumSize(new Dimension(400, 150));
    071    dlg.setVisible(true);
    072
    073    // Checks if the inputs are correct and starts the export process.
    074    if (pane.getValue() != null
    075        && (int) pane.getValue() == JOptionPane.OK_OPTION
    076        && dialog.chooser != null) {
    077      if (dialog.group.isSelected(dialog.all.getModel())) {
    078        export(StreetsideLayer.getInstance().getData().getImages());
    079      } else if (dialog.group.isSelected(dialog.sequence.getModel())) {
    080        Set<StreetsideAbstractImage> images = new ConcurrentSkipListSet<>();
    081        for (StreetsideAbstractImage image : StreetsideLayer.getInstance().getData().getMultiSelectedImages()) {
    082          if (image instanceof StreetsideImage) {
    083            if (!images.contains(image)) {
    084              images.addAll(image.getSequence().getImages());
    085            }
    086          } else {
    087            images.add(image);
    088          }
    089        }
    090        export(images);
    091      } else if (dialog.group.isSelected(dialog.selected.getModel())) {
    092        export(StreetsideLayer.getInstance().getData().getMultiSelectedImages());
    093      }
    094      // This option ignores the selected directory.
    095    } else if (dialog.group.isSelected(dialog.rewrite.getModel())) {
    096      ArrayList<StreetsideImportedImage> images = new ArrayList<>();
    097      StreetsideLayer.getInstance().getData().getImages().stream().filter(img -> img instanceof StreetsideImportedImage).forEach(img -> images.add((StreetsideImportedImage) img));
    098      try {
    099        MainApplication.worker.execute(new StreetsideExportManager(images));
    100      } catch (IOException e1) {
    101        Logging.error(e1);
    102      }
    103    }
    104    dlg.dispose();
    105  }
    106
    107  /**
    108   * Exports the given images from the database.
    109   *
    110   * @param images
    111   *          The set of images to be exported.
    112   */
    113  public void export(Set<StreetsideAbstractImage> images) {
    114    MainApplication.worker.execute(new StreetsideExportManager(images,
    115        dialog.chooser.getSelectedFile().toString()));
    116  }
    117
    118  @Override
    119  protected boolean listenToSelectionChange() {
    120    return false;
    121  }
    122
    123  /**
    124   * Enabled when streetside layer is in layer list
    125   */
    126  @Override
    127  protected void updateEnabledState() {
    128    super.updateEnabledState();
    129    setEnabled(StreetsideLayer.hasInstance());
    130  }
    131}