| 1 | // License: GPL. For details, see LICENSE file. |
|---|
| 2 | package org.openstreetmap.josm.actions; |
|---|
| 3 | |
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr; |
|---|
| 5 | |
|---|
| 6 | import java.awt.Component; |
|---|
| 7 | import java.awt.GridBagLayout; |
|---|
| 8 | import java.util.List; |
|---|
| 9 | |
|---|
| 10 | import javax.swing.DefaultComboBoxModel; |
|---|
| 11 | import javax.swing.DefaultListCellRenderer; |
|---|
| 12 | import javax.swing.Icon; |
|---|
| 13 | import javax.swing.JComboBox; |
|---|
| 14 | import javax.swing.JLabel; |
|---|
| 15 | import javax.swing.JList; |
|---|
| 16 | import javax.swing.JOptionPane; |
|---|
| 17 | import javax.swing.JPanel; |
|---|
| 18 | |
|---|
| 19 | import org.openstreetmap.josm.Main; |
|---|
| 20 | import org.openstreetmap.josm.gui.ExtendedDialog; |
|---|
| 21 | import org.openstreetmap.josm.gui.layer.Layer; |
|---|
| 22 | import org.openstreetmap.josm.tools.GBC; |
|---|
| 23 | import org.openstreetmap.josm.tools.Shortcut; |
|---|
| 24 | |
|---|
| 25 | public abstract class AbstractMergeAction extends JosmAction { |
|---|
| 26 | |
|---|
| 27 | /** |
|---|
| 28 | * the list cell renderer used to render layer list entries |
|---|
| 29 | * |
|---|
| 30 | */ |
|---|
| 31 | static public class LayerListCellRenderer extends DefaultListCellRenderer { |
|---|
| 32 | |
|---|
| 33 | @Override |
|---|
| 34 | public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, |
|---|
| 35 | boolean cellHasFocus) { |
|---|
| 36 | Layer layer = (Layer) value; |
|---|
| 37 | JLabel label = (JLabel) super.getListCellRendererComponent(list, layer.getName(), index, isSelected, |
|---|
| 38 | cellHasFocus); |
|---|
| 39 | Icon icon = layer.getIcon(); |
|---|
| 40 | label.setIcon(icon); |
|---|
| 41 | label.setToolTipText(layer.getToolTipText()); |
|---|
| 42 | return label; |
|---|
| 43 | } |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | public AbstractMergeAction() { |
|---|
| 47 | super(); |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | public AbstractMergeAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register) { |
|---|
| 51 | super(name, iconName, tooltip, shortcut, register); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | public AbstractMergeAction(String name, String iconName, String tooltip, Shortcut shortcut, |
|---|
| 55 | boolean register, String toolbar, boolean installAdapters) { |
|---|
| 56 | super(name, iconName, tooltip, shortcut, register, toolbar, installAdapters); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | protected Layer askTargetLayer(List<Layer> targetLayers) { |
|---|
| 60 | JComboBox layerList = new JComboBox(); |
|---|
| 61 | layerList.setRenderer(new LayerListCellRenderer()); |
|---|
| 62 | layerList.setModel(new DefaultComboBoxModel(targetLayers.toArray())); |
|---|
| 63 | layerList.setSelectedIndex(0); |
|---|
| 64 | |
|---|
| 65 | JPanel pnl = new JPanel(); |
|---|
| 66 | pnl.setLayout(new GridBagLayout()); |
|---|
| 67 | pnl.add(new JLabel(tr("Please select the target layer.")), GBC.eol()); |
|---|
| 68 | pnl.add(layerList, GBC.eol()); |
|---|
| 69 | |
|---|
| 70 | ExtendedDialog ed = new ExtendedDialog(Main.parent, |
|---|
| 71 | tr("Select target layer"), |
|---|
| 72 | new String[] { tr("Merge"), tr("Cancel") }); |
|---|
| 73 | ed.setButtonIcons(new String[] { "dialogs/mergedown", "cancel" }); |
|---|
| 74 | ed.setContent(pnl); |
|---|
| 75 | ed.showDialog(); |
|---|
| 76 | if (ed.getValue() != 1) |
|---|
| 77 | return null; |
|---|
| 78 | |
|---|
| 79 | Layer targetLayer = (Layer) layerList.getSelectedItem(); |
|---|
| 80 | return targetLayer; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | protected void warnNoTargetLayersForSourceLayer(Layer sourceLayer) { |
|---|
| 84 | JOptionPane.showMessageDialog(Main.parent, |
|---|
| 85 | tr("<html>There are no layers the source layer<br>''{0}''<br>could be merged to.</html>", sourceLayer.getName()), |
|---|
| 86 | tr("No target layers"), JOptionPane.WARNING_MESSAGE); |
|---|
| 87 | } |
|---|
| 88 | } |
|---|