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 | protected Layer askTargetLayer(List<Layer> targetLayers) {
|
---|
55 | JComboBox layerList = new JComboBox();
|
---|
56 | layerList.setRenderer(new LayerListCellRenderer());
|
---|
57 | layerList.setModel(new DefaultComboBoxModel(targetLayers.toArray()));
|
---|
58 | layerList.setSelectedIndex(0);
|
---|
59 |
|
---|
60 | JPanel pnl = new JPanel();
|
---|
61 | pnl.setLayout(new GridBagLayout());
|
---|
62 | pnl.add(new JLabel(tr("Please select the target layer.")), GBC.eol());
|
---|
63 | pnl.add(layerList, GBC.eol());
|
---|
64 |
|
---|
65 | ExtendedDialog ed = new ExtendedDialog(Main.parent,
|
---|
66 | tr("Select target layer"),
|
---|
67 | new String[] { tr("Merge"), tr("Cancel") });
|
---|
68 | ed.setButtonIcons(new String[] { "dialogs/mergedown", "cancel" });
|
---|
69 | ed.setContent(pnl);
|
---|
70 | ed.showDialog();
|
---|
71 | if (ed.getValue() != 1)
|
---|
72 | return null;
|
---|
73 |
|
---|
74 | Layer targetLayer = (Layer) layerList.getSelectedItem();
|
---|
75 | return targetLayer;
|
---|
76 | }
|
---|
77 |
|
---|
78 | protected void warnNoTargetLayersForSourceLayer(Layer sourceLayer) {
|
---|
79 | JOptionPane.showMessageDialog(Main.parent,
|
---|
80 | tr("<html>There are no layers the source layer<br>''{0}''<br>could be merged to.</html>"),
|
---|
81 | tr("No target layers"), JOptionPane.WARNING_MESSAGE);
|
---|
82 | }
|
---|
83 | }
|
---|