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.GraphicsEnvironment;
|
---|
8 | import java.awt.GridBagLayout;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import javax.swing.DefaultListCellRenderer;
|
---|
12 | import javax.swing.Icon;
|
---|
13 | import javax.swing.JLabel;
|
---|
14 | import javax.swing.JList;
|
---|
15 | import javax.swing.JOptionPane;
|
---|
16 | import javax.swing.JPanel;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.Main;
|
---|
19 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
20 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
21 | import org.openstreetmap.josm.gui.widgets.JosmComboBox;
|
---|
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 | public static class LayerListCellRenderer extends DefaultListCellRenderer {
|
---|
32 |
|
---|
33 | @Override
|
---|
34 | public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
|
---|
35 | Layer layer = (Layer) value;
|
---|
36 | JLabel label = (JLabel) super.getListCellRendererComponent(list, layer.getName(), index, isSelected, cellHasFocus);
|
---|
37 | Icon icon = layer.getIcon();
|
---|
38 | label.setIcon(icon);
|
---|
39 | label.setToolTipText(layer.getToolTipText());
|
---|
40 | return label;
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Constructs a new {@code AbstractMergeAction}.
|
---|
46 | */
|
---|
47 | public AbstractMergeAction() {
|
---|
48 | super();
|
---|
49 | }
|
---|
50 |
|
---|
51 | public AbstractMergeAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register) {
|
---|
52 | super(name, iconName, tooltip, shortcut, register);
|
---|
53 | }
|
---|
54 |
|
---|
55 | public AbstractMergeAction(String name, String iconName, String tooltip, Shortcut shortcut,
|
---|
56 | boolean register, String toolbar, boolean installAdapters) {
|
---|
57 | super(name, iconName, tooltip, shortcut, register, toolbar, installAdapters);
|
---|
58 | }
|
---|
59 |
|
---|
60 | protected static Layer askTargetLayer(List<Layer> targetLayers) {
|
---|
61 | return askTargetLayer(targetLayers.toArray(new Layer[targetLayers.size()]),
|
---|
62 | tr("Please select the target layer."),
|
---|
63 | tr("Select target layer"),
|
---|
64 | tr("Merge"), "dialogs/mergedown");
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Asks a target layer.
|
---|
69 | * @param <T> type of layer
|
---|
70 | * @param targetLayers array of proposed target layers
|
---|
71 | * @param label label displayed in dialog
|
---|
72 | * @param title title of dialog
|
---|
73 | * @param buttonText text of button used to select target layer
|
---|
74 | * @param buttonIcon icon name of button used to select target layer
|
---|
75 | * @return choosen target layer
|
---|
76 | */
|
---|
77 | @SuppressWarnings("unchecked")
|
---|
78 | public static <T extends Layer> T askTargetLayer(T[] targetLayers, String label, String title, String buttonText, String buttonIcon) {
|
---|
79 | JosmComboBox<T> layerList = new JosmComboBox<>(targetLayers);
|
---|
80 | layerList.setRenderer(new LayerListCellRenderer());
|
---|
81 | layerList.setSelectedIndex(0);
|
---|
82 |
|
---|
83 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
84 | pnl.add(new JLabel(label), GBC.eol());
|
---|
85 | pnl.add(layerList, GBC.eol());
|
---|
86 | if (GraphicsEnvironment.isHeadless()) {
|
---|
87 | // return first layer in headless mode, for unit tests
|
---|
88 | return targetLayers[0];
|
---|
89 | }
|
---|
90 | ExtendedDialog ed = new ExtendedDialog(Main.parent, title, new String[] {buttonText, tr("Cancel")});
|
---|
91 | ed.setButtonIcons(new String[] {buttonIcon, "cancel"});
|
---|
92 | ed.setContent(pnl);
|
---|
93 | ed.showDialog();
|
---|
94 | if (ed.getValue() != 1) {
|
---|
95 | return null;
|
---|
96 | }
|
---|
97 | return (T) layerList.getSelectedItem();
|
---|
98 | }
|
---|
99 |
|
---|
100 | protected void warnNoTargetLayersForSourceLayer(Layer sourceLayer) {
|
---|
101 | JOptionPane.showMessageDialog(Main.parent,
|
---|
102 | tr("<html>There are no layers the source layer<br>''{0}''<br>could be merged to.</html>", sourceLayer.getName()),
|
---|
103 | tr("No target layers"), JOptionPane.WARNING_MESSAGE);
|
---|
104 | }
|
---|
105 | }
|
---|