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