001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.plugins.streetside.gui.dialog;
003
004import java.awt.BorderLayout;
005import java.awt.Component;
006import java.awt.Container;
007import java.awt.FlowLayout;
008import java.util.ArrayList;
009import java.util.List;
010
011import javax.swing.AbstractListModel;
012import javax.swing.JButton;
013import javax.swing.JDialog;
014import javax.swing.JLabel;
015import javax.swing.JList;
016import javax.swing.JPanel;
017import javax.swing.ListCellRenderer;
018import javax.swing.ListSelectionModel;
019import javax.swing.SwingConstants;
020import javax.swing.UIManager;
021
022import org.openstreetmap.josm.gui.layer.geoimage.GeoImageLayer;
023import org.openstreetmap.josm.gui.util.GuiHelper;
024import org.openstreetmap.josm.tools.I18n;
025import org.openstreetmap.josm.tools.ImageProvider;
026
027public class ChooseGeoImageLayersDialog extends JDialog {
028  private static final long serialVersionUID = -1793622345412435234L;
029  private static final String QUESTION = I18n.marktr("Which image layers do you want to import into the Streetside layer?");
030
031  public ChooseGeoImageLayersDialog(final Component parent, final List<GeoImageLayer> layers) {
032      super(GuiHelper.getFrameForComponent(parent), I18n.tr(QUESTION));
033    final Container c = getContentPane();
034    c.setLayout(new BorderLayout(10, 10));
035
036    final JPanel questionPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 10, 10));
037    questionPanel.add(new JLabel(I18n.tr(QUESTION)));
038    c.add(questionPanel, BorderLayout.NORTH);
039
040    final JList<GeoImageLayer> list = new JList<>();
041    list.setModel(new BasicListModel<>(layers));
042    list.setCellRenderer(new GeoImageLayerListCellRenderer());
043    list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
044    c.add(list, BorderLayout.CENTER);
045
046    final JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
047    final JButton cancelButton = new JButton(I18n.tr("Cancel"), new ImageProvider("cancel").get());
048    cancelButton.addActionListener(e -> dispose());
049    cancelButton.requestFocus();
050    buttonPanel.add(cancelButton);
051    final JButton importButton = new JButton(I18n.tr("Import"), new ImageProvider("copy").get());
052
053    // Streetside does not support importing images (Mapillary relic)
054    importButton.setEnabled(false);
055
056    buttonPanel.add(importButton);
057    c.add(buttonPanel, BorderLayout.SOUTH);
058
059    setModal(true);
060    pack();
061    setLocationRelativeTo(parent);
062  }
063
064  protected static class GeoImageLayerListCellRenderer implements ListCellRenderer<GeoImageLayer> {
065    @Override
066    public Component getListCellRendererComponent(
067      JList<? extends GeoImageLayer> list, GeoImageLayer value, int index, boolean isSelected, boolean cellHasFocus
068    ) {
069      final JLabel result = value == null
070          ? null
071          /* i18n: {0} is the layer name, {1} the number of images in it */
072          : new JLabel(I18n.tr("{0} ({1} images)", value.getName(), value.getImages().size()), value.getIcon(), SwingConstants.LEADING);
073      if (result != null) {
074        result.setOpaque(true);
075        result.setBackground(isSelected ? UIManager.getColor("List.selectionBackground") : UIManager.getColor("List.background"));
076      }
077      return result;
078    }
079  }
080
081  private static class BasicListModel<T> extends AbstractListModel<T> {
082    private static final long serialVersionUID = 3107247955341855290L;
083    private final List<T> list;
084
085    public BasicListModel(List<T> list) {
086      this.list = list == null ? new ArrayList<>() : list;
087    }
088
089    @Override
090    public int getSize() {
091      return list.size();
092    }
093
094    @Override
095    public T getElementAt(int index) {
096      return list.get(index);
097    }
098  }
099}