001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.Component;
007import java.awt.GridBagLayout;
008import java.util.List;
009
010import javax.swing.DefaultListCellRenderer;
011import javax.swing.Icon;
012import javax.swing.JLabel;
013import javax.swing.JList;
014import javax.swing.JOptionPane;
015import javax.swing.JPanel;
016
017import org.openstreetmap.josm.Main;
018import org.openstreetmap.josm.gui.ExtendedDialog;
019import org.openstreetmap.josm.gui.layer.Layer;
020import org.openstreetmap.josm.gui.widgets.JosmComboBox;
021import org.openstreetmap.josm.tools.GBC;
022import org.openstreetmap.josm.tools.Shortcut;
023
024public abstract class AbstractMergeAction extends JosmAction {
025
026    /**
027     * the list cell renderer used to render layer list entries
028     *
029     */
030    public static class LayerListCellRenderer extends DefaultListCellRenderer {
031
032        @Override
033        public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
034            Layer layer = (Layer) value;
035            JLabel label = (JLabel) super.getListCellRendererComponent(list, layer.getName(), index, isSelected, cellHasFocus);
036            Icon icon = layer.getIcon();
037            label.setIcon(icon);
038            label.setToolTipText(layer.getToolTipText());
039            return label;
040        }
041    }
042
043    /**
044     * Constructs a new {@code AbstractMergeAction}.
045     */
046    public AbstractMergeAction() {
047        super();
048    }
049
050    public AbstractMergeAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register) {
051        super(name, iconName, tooltip, shortcut, register);
052    }
053
054    public AbstractMergeAction(String name, String iconName, String tooltip, Shortcut shortcut,
055    boolean register, String toolbar, boolean installAdapters) {
056        super(name, iconName, tooltip, shortcut, register, toolbar, installAdapters);
057    }
058
059    protected Layer askTargetLayer(List<Layer> targetLayers) {
060        JosmComboBox<Layer> layerList = new JosmComboBox<>(targetLayers.toArray(new Layer[0]));
061        layerList.setRenderer(new LayerListCellRenderer());
062        layerList.setSelectedIndex(0);
063
064        JPanel pnl = new JPanel(new GridBagLayout());
065        pnl.add(new JLabel(tr("Please select the target layer.")), GBC.eol());
066        pnl.add(layerList, GBC.eol());
067
068        ExtendedDialog ed = new ExtendedDialog(Main.parent,
069                tr("Select target layer"),
070                new String[] { tr("Merge"), tr("Cancel") });
071        ed.setButtonIcons(new String[] { "dialogs/mergedown", "cancel" });
072        ed.setContent(pnl);
073        ed.showDialog();
074        if (ed.getValue() != 1)
075            return null;
076
077        return (Layer) layerList.getSelectedItem();
078    }
079
080    protected void warnNoTargetLayersForSourceLayer(Layer sourceLayer) {
081        JOptionPane.showMessageDialog(Main.parent,
082                tr("<html>There are no layers the source layer<br>''{0}''<br>could be merged to.</html>", sourceLayer.getName()),
083                tr("No target layers"), JOptionPane.WARNING_MESSAGE);
084    }
085}