001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.plugins.streetside.gui;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.Component;
007import java.awt.event.ActionEvent;
008import java.awt.event.ActionListener;
009
010import javax.swing.AbstractAction;
011import javax.swing.BoxLayout;
012import javax.swing.ButtonGroup;
013import javax.swing.JButton;
014import javax.swing.JFileChooser;
015import javax.swing.JLabel;
016import javax.swing.JPanel;
017import javax.swing.JRadioButton;
018
019import org.openstreetmap.josm.plugins.streetside.StreetsideImage;
020import org.openstreetmap.josm.plugins.streetside.StreetsideImportedImage;
021import org.openstreetmap.josm.plugins.streetside.StreetsideLayer;
022
023
024/**
025 * GUI for exporting images.
026 *
027 * @author nokutu
028 *
029 */
030public class StreetsideExportDialog extends JPanel implements ActionListener {
031
032  private static final long serialVersionUID = -2746815082016025516L;
033  /** Button to export all downloaded images. */
034  public final JRadioButton all;
035  /**
036   * Button to export all images in the sequence of the selected StreetsideImage.
037   */
038  public final JRadioButton sequence;
039  /**
040   * Button to export all images belonging to the selected
041   * {@link StreetsideImage} objects.
042   */
043  public final JRadioButton selected;
044  /** Button to rewrite all imported images. */
045  public final JRadioButton rewrite;
046  /** Group of button containing all the options. */
047  public final ButtonGroup group;
048  private final JButton choose;
049  private final JLabel path;
050  /** File chooser. */
051  public JFileChooser chooser;
052  private final JButton ok;
053
054  /**
055   * Main constructor.
056   *
057   * @param ok
058   *          The button for to OK option.
059   */
060  public StreetsideExportDialog(JButton ok) {
061    this.ok = ok;
062    ok.setEnabled(false);
063
064    setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
065
066    RewriteButtonAction action = new RewriteButtonAction(this);
067    this.group = new ButtonGroup();
068    this.all = new JRadioButton(action);
069    this.all.setText(tr("Export all images"));
070    this.sequence = new JRadioButton(action);
071    this.sequence.setText(tr("Export selected sequence"));
072    this.selected = new JRadioButton(action);
073    this.selected.setText(tr("Export selected images"));
074    this.rewrite = new JRadioButton(action);
075    this.rewrite.setText(tr("Rewrite imported images"));
076    this.group.add(this.all);
077    this.group.add(this.sequence);
078    this.group.add(this.selected);
079    this.group.add(this.rewrite);
080    // Some options are disabled depending on the circumstances
081    sequence.setEnabled(StreetsideLayer.getInstance().getData().getSelectedImage() instanceof StreetsideImage);
082    if (StreetsideLayer.getInstance().getData().getMultiSelectedImages().isEmpty()) {
083     this.selected.setEnabled(false);
084    }
085    this.rewrite.setEnabled(StreetsideLayer.getInstance().getData().getImages().parallelStream().anyMatch(img -> img instanceof StreetsideImportedImage));
086
087    this.path = new JLabel(tr("Select a directory"));
088    this.choose = new JButton(tr("Explore"));
089    this.choose.addActionListener(this);
090
091    // All options belong to the same JPanel so the are in line.
092    JPanel jpanel = new JPanel();
093    jpanel.setLayout(new BoxLayout(jpanel, BoxLayout.PAGE_AXIS));
094    jpanel.add(this.all);
095    jpanel.add(this.sequence);
096    jpanel.add(this.selected);
097    jpanel.add(this.rewrite);
098    jpanel.setAlignmentX(Component.CENTER_ALIGNMENT);
099    this.path.setAlignmentX(Component.CENTER_ALIGNMENT);
100    this.choose.setAlignmentX(Component.CENTER_ALIGNMENT);
101
102    add(jpanel);
103    add(this.path);
104    add(this.choose);
105  }
106
107  /**
108   * Creates the folder chooser GUI.
109   */
110  @Override
111  public void actionPerformed(ActionEvent e) {
112    this.chooser = new JFileChooser();
113    this.chooser.setCurrentDirectory(new java.io.File(System
114        .getProperty("user.home")));
115    this.chooser.setDialogTitle(tr("Select a directory"));
116    this.chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
117    this.chooser.setAcceptAllFileFilterUsed(false);
118
119    if (this.chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
120      this.path.setText(this.chooser.getSelectedFile().toString());
121      this.updateUI();
122      this.ok.setEnabled(true);
123    }
124  }
125
126  /**
127   * Enables/disables some parts of the panel depending if the rewrite button is
128   * active.
129   *
130   * @author nokutu
131   *
132   */
133  public class RewriteButtonAction extends AbstractAction {
134
135    private static final long serialVersionUID = 1035332841101190301L;
136
137        private String lastPath;
138    private final StreetsideExportDialog dlg;
139
140    /**
141     * Main constructor.
142     *
143     * @param dlg
144     *          Parent dialog.
145     */
146    public RewriteButtonAction(StreetsideExportDialog dlg) {
147      this.dlg = dlg;
148    }
149
150    @Override
151    public void actionPerformed(ActionEvent arg0) {
152      StreetsideExportDialog.this.choose
153          .setEnabled(!StreetsideExportDialog.this.rewrite.isSelected());
154      if (StreetsideExportDialog.this.rewrite.isSelected()) {
155        this.lastPath = this.dlg.path.getText();
156        this.dlg.path.setText(" ");
157      } else if (this.lastPath != null) {
158        this.dlg.path.setText(this.lastPath);
159      }
160    }
161  }
162}