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