[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.JLabel;
|
---|
| 13 | import javax.swing.JList;
|
---|
| 14 | import javax.swing.JOptionPane;
|
---|
| 15 | import javax.swing.JPanel;
|
---|
| 16 |
|
---|
| 17 | import org.openstreetmap.josm.Main;
|
---|
| 18 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
| 19 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
[5429] | 20 | import org.openstreetmap.josm.gui.widgets.JosmComboBox;
|
---|
[2512] | 21 | import org.openstreetmap.josm.tools.GBC;
|
---|
| 22 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[11848] | 23 | import org.openstreetmap.josm.tools.Utils;
|
---|
[2512] | 24 |
|
---|
[11885] | 25 | /**
|
---|
| 26 | * Abstract superclass of different "Merge" actions.
|
---|
| 27 | * @since 1890
|
---|
| 28 | */
|
---|
[2512] | 29 | public abstract class AbstractMergeAction extends JosmAction {
|
---|
| 30 |
|
---|
| 31 | /**
|
---|
| 32 | * the list cell renderer used to render layer list entries
|
---|
| 33 | */
|
---|
[6889] | 34 | public static class LayerListCellRenderer extends DefaultListCellRenderer {
|
---|
[2512] | 35 |
|
---|
| 36 | @Override
|
---|
[7001] | 37 | public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
|
---|
[2512] | 38 | Layer layer = (Layer) value;
|
---|
[7001] | 39 | JLabel label = (JLabel) super.getListCellRendererComponent(list, layer.getName(), index, isSelected, cellHasFocus);
|
---|
[11885] | 40 | label.setIcon(layer.getIcon());
|
---|
[2512] | 41 | label.setToolTipText(layer.getToolTipText());
|
---|
| 42 | return label;
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[6792] | 46 | /**
|
---|
| 47 | * Constructs a new {@code AbstractMergeAction}.
|
---|
[11885] | 48 | * @param name the action's text as displayed on the menu (if it is added to a menu)
|
---|
| 49 | * @param iconName the filename of the icon to use
|
---|
| 50 | * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note
|
---|
| 51 | * that html is not supported for menu actions on some platforms.
|
---|
| 52 | * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
|
---|
| 53 | * do want a shortcut, remember you can always register it with group=none, so you
|
---|
| 54 | * won't be assigned a shortcut unless the user configures one. If you pass null here,
|
---|
| 55 | * the user CANNOT configure a shortcut for your action.
|
---|
| 56 | * @param register register this action for the toolbar preferences?
|
---|
[6792] | 57 | */
|
---|
[2512] | 58 | public AbstractMergeAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register) {
|
---|
| 59 | super(name, iconName, tooltip, shortcut, register);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[11885] | 62 | /**
|
---|
| 63 | * Constructs a new {@code AbstractMergeAction}.
|
---|
| 64 | * @param name the action's text as displayed on the menu (if it is added to a menu)
|
---|
| 65 | * @param iconName the filename of the icon to use
|
---|
| 66 | * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note
|
---|
| 67 | * that html is not supported for menu actions on some platforms.
|
---|
| 68 | * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
|
---|
| 69 | * do want a shortcut, remember you can always register it with group=none, so you
|
---|
| 70 | * won't be assigned a shortcut unless the user configures one. If you pass null here,
|
---|
| 71 | * the user CANNOT configure a shortcut for your action.
|
---|
| 72 | * @param register register this action for the toolbar preferences?
|
---|
| 73 | * @param toolbar identifier for the toolbar preferences. The iconName is used, if this parameter is null
|
---|
| 74 | * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
|
---|
| 75 | */
|
---|
[4851] | 76 | public AbstractMergeAction(String name, String iconName, String tooltip, Shortcut shortcut,
|
---|
| 77 | boolean register, String toolbar, boolean installAdapters) {
|
---|
| 78 | super(name, iconName, tooltip, shortcut, register, toolbar, installAdapters);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[11885] | 81 | /**
|
---|
| 82 | * Ask user to choose the target layer.
|
---|
| 83 | * @param targetLayers list of candidate target layers.
|
---|
| 84 | * @return the chosen layer
|
---|
| 85 | */
|
---|
[10074] | 86 | protected static Layer askTargetLayer(List<Layer> targetLayers) {
|
---|
[11017] | 87 | return askTargetLayer(targetLayers.toArray(new Layer[targetLayers.size()]),
|
---|
[10074] | 88 | tr("Please select the target layer."),
|
---|
| 89 | tr("Select target layer"),
|
---|
| 90 | tr("Merge"), "dialogs/mergedown");
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | /**
|
---|
| 94 | * Asks a target layer.
|
---|
| 95 | * @param <T> type of layer
|
---|
| 96 | * @param targetLayers array of proposed target layers
|
---|
| 97 | * @param label label displayed in dialog
|
---|
| 98 | * @param title title of dialog
|
---|
| 99 | * @param buttonText text of button used to select target layer
|
---|
| 100 | * @param buttonIcon icon name of button used to select target layer
|
---|
| 101 | * @return choosen target layer
|
---|
| 102 | */
|
---|
| 103 | @SuppressWarnings("unchecked")
|
---|
| 104 | public static <T extends Layer> T askTargetLayer(T[] targetLayers, String label, String title, String buttonText, String buttonIcon) {
|
---|
| 105 | JosmComboBox<T> layerList = new JosmComboBox<>(targetLayers);
|
---|
[2512] | 106 | layerList.setRenderer(new LayerListCellRenderer());
|
---|
| 107 | layerList.setSelectedIndex(0);
|
---|
| 108 |
|
---|
[5450] | 109 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
[10074] | 110 | pnl.add(new JLabel(label), GBC.eol());
|
---|
[2512] | 111 | pnl.add(layerList, GBC.eol());
|
---|
[10074] | 112 | if (GraphicsEnvironment.isHeadless()) {
|
---|
| 113 | // return first layer in headless mode, for unit tests
|
---|
| 114 | return targetLayers[0];
|
---|
| 115 | }
|
---|
[12279] | 116 | ExtendedDialog ed = new ExtendedDialog(Main.parent, title, buttonText, tr("Cancel"));
|
---|
| 117 | ed.setButtonIcons(buttonIcon, "cancel");
|
---|
[2512] | 118 | ed.setContent(pnl);
|
---|
| 119 | ed.showDialog();
|
---|
[10074] | 120 | if (ed.getValue() != 1) {
|
---|
[2512] | 121 | return null;
|
---|
[10074] | 122 | }
|
---|
| 123 | return (T) layerList.getSelectedItem();
|
---|
[2512] | 124 | }
|
---|
| 125 |
|
---|
[11885] | 126 | /**
|
---|
| 127 | * Warns user when there no layers the source layer could be merged to.
|
---|
| 128 | * @param sourceLayer source layer
|
---|
| 129 | */
|
---|
[2512] | 130 | protected void warnNoTargetLayersForSourceLayer(Layer sourceLayer) {
|
---|
[11885] | 131 | String message = tr("<html>There are no layers the source layer<br>''{0}''<br>could be merged to.</html>",
|
---|
| 132 | Utils.escapeReservedCharactersHTML(sourceLayer.getName()));
|
---|
| 133 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
| 134 | JOptionPane.showMessageDialog(Main.parent, message, tr("No target layers"), JOptionPane.WARNING_MESSAGE);
|
---|
| 135 | }
|
---|
[2512] | 136 | }
|
---|
| 137 | }
|
---|