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    static public class LayerListCellRenderer extends DefaultListCellRenderer {
031
032        @Override
033        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
034                boolean cellHasFocus) {
035            Layer layer = (Layer) value;
036            JLabel label = (JLabel) super.getListCellRendererComponent(list, layer.getName(), index, isSelected,
037                    cellHasFocus);
038            Icon icon = layer.getIcon();
039            label.setIcon(icon);
040            label.setToolTipText(layer.getToolTipText());
041            return label;
042        }
043    }
044
045    public AbstractMergeAction() {
046        super();
047    }
048
049    public AbstractMergeAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register) {
050        super(name, iconName, tooltip, shortcut, register);
051    }
052
053    public AbstractMergeAction(String name, String iconName, String tooltip, Shortcut shortcut,
054    boolean register, String toolbar, boolean installAdapters) {
055        super(name, iconName, tooltip, shortcut, register, toolbar, installAdapters);
056    }
057
058    protected Layer askTargetLayer(List<Layer> targetLayers) {
059        JosmComboBox layerList = new JosmComboBox(targetLayers.toArray());
060        layerList.setRenderer(new LayerListCellRenderer());
061        layerList.setSelectedIndex(0);
062
063        JPanel pnl = new JPanel(new GridBagLayout());
064        pnl.add(new JLabel(tr("Please select the target layer.")), GBC.eol());
065        pnl.add(layerList, GBC.eol());
066
067        ExtendedDialog ed = new ExtendedDialog(Main.parent,
068                tr("Select target layer"),
069                new String[] { tr("Merge"), tr("Cancel") });
070        ed.setButtonIcons(new String[] { "dialogs/mergedown", "cancel" });
071        ed.setContent(pnl);
072        ed.showDialog();
073        if (ed.getValue() != 1)
074            return null;
075
076        Layer targetLayer = (Layer) layerList.getSelectedItem();
077        return targetLayer;
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}