[1890] | 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 | /**
|
---|
| 29 | * the list cell renderer used to render layer list entries
|
---|
| 30 | *
|
---|
| 31 | */
|
---|
| 32 | static public class LayerListCellRenderer extends DefaultListCellRenderer {
|
---|
| 33 |
|
---|
| 34 | protected boolean isActiveLayer(Layer layer) {
|
---|
| 35 | if (Main.map == null)
|
---|
| 36 | return false;
|
---|
| 37 | if (Main.map.mapView == null)
|
---|
| 38 | return false;
|
---|
| 39 | return Main.map.mapView.getActiveLayer() == layer;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | @Override
|
---|
| 43 | public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
|
---|
| 44 | boolean cellHasFocus) {
|
---|
| 45 | Layer layer = (Layer) value;
|
---|
| 46 | JLabel label = (JLabel) super.getListCellRendererComponent(list, layer.getName(), index, isSelected,
|
---|
| 47 | cellHasFocus);
|
---|
| 48 | Icon icon = layer.getIcon();
|
---|
| 49 | label.setIcon(icon);
|
---|
| 50 | label.setToolTipText(layer.getToolTipText());
|
---|
| 51 | return label;
|
---|
| 52 | }
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | public AbstractMergeAction() {
|
---|
| 56 | super();
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | public AbstractMergeAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register) {
|
---|
| 60 | super(name, iconName, tooltip, shortcut, register);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | protected Layer askTargetLayer(List<Layer> targetLayers) {
|
---|
| 64 | JComboBox layerList = new JComboBox();
|
---|
| 65 | layerList.setRenderer(new LayerListCellRenderer());
|
---|
| 66 | layerList.setModel(new DefaultComboBoxModel(targetLayers.toArray()));
|
---|
| 67 | layerList.setSelectedIndex(0);
|
---|
[2032] | 68 |
|
---|
[1890] | 69 | JPanel pnl = new JPanel();
|
---|
| 70 | pnl.setLayout(new GridBagLayout());
|
---|
| 71 | pnl.add(new JLabel(tr("Please select the target layer.")), GBC.eol());
|
---|
| 72 | pnl.add(layerList, GBC.eol());
|
---|
[2032] | 73 |
|
---|
| 74 | ExtendedDialog ed = new ExtendedDialog(Main.parent,
|
---|
| 75 | tr("Select target layer"),
|
---|
| 76 | new String[] { tr("Merge"), tr("Cancel") });
|
---|
| 77 | ed.setButtonIcons(new String[] { "dialogs/mergedown", "cancel" });
|
---|
| 78 | ed.setContent(pnl);
|
---|
| 79 | ed.showDialog();
|
---|
| 80 | if (ed.getValue() != 1)
|
---|
[1890] | 81 | return null;
|
---|
[2032] | 82 |
|
---|
[1890] | 83 | Layer targetLayer = (Layer) layerList.getSelectedItem();
|
---|
| 84 | return targetLayer;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | protected void warnNoTargetLayersForSourceLayer(Layer sourceLayer) {
|
---|
[2017] | 88 | JOptionPane.showMessageDialog(Main.parent,
|
---|
[1890] | 89 | tr("<html>There are no layers the source layer<br>''{0}''<br>could be merged to.</html>"),
|
---|
| 90 | tr("No target layers"), JOptionPane.WARNING_MESSAGE);
|
---|
| 91 | }
|
---|
| 92 | }
|
---|