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