source: josm/trunk/src/org/openstreetmap/josm/actions/AbstractMergeAction.java@ 11848

Last change on this file since 11848 was 11848, checked in by Don-vip, 7 years ago

fix #14613 - Special HTML characters not escaped in GUI error messages

  • Property svn:eol-style set to native
File size: 4.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.GraphicsEnvironment;
8import java.awt.GridBagLayout;
9import java.util.List;
10
11import javax.swing.DefaultListCellRenderer;
12import javax.swing.Icon;
13import javax.swing.JLabel;
14import javax.swing.JList;
15import javax.swing.JOptionPane;
16import javax.swing.JPanel;
17
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.gui.ExtendedDialog;
20import org.openstreetmap.josm.gui.layer.Layer;
21import org.openstreetmap.josm.gui.widgets.JosmComboBox;
22import org.openstreetmap.josm.tools.GBC;
23import org.openstreetmap.josm.tools.Shortcut;
24import org.openstreetmap.josm.tools.Utils;
25
26public abstract class AbstractMergeAction extends JosmAction {
27
28 /**
29 * the list cell renderer used to render layer list entries
30 *
31 */
32 public static class LayerListCellRenderer extends DefaultListCellRenderer {
33
34 @Override
35 public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
36 Layer layer = (Layer) value;
37 JLabel label = (JLabel) super.getListCellRendererComponent(list, layer.getName(), index, isSelected, cellHasFocus);
38 Icon icon = layer.getIcon();
39 label.setIcon(icon);
40 label.setToolTipText(layer.getToolTipText());
41 return label;
42 }
43 }
44
45 /**
46 * Constructs a new {@code AbstractMergeAction}.
47 */
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
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
61 protected static Layer askTargetLayer(List<Layer> targetLayers) {
62 return askTargetLayer(targetLayers.toArray(new Layer[targetLayers.size()]),
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);
81 layerList.setRenderer(new LayerListCellRenderer());
82 layerList.setSelectedIndex(0);
83
84 JPanel pnl = new JPanel(new GridBagLayout());
85 pnl.add(new JLabel(label), GBC.eol());
86 pnl.add(layerList, GBC.eol());
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"});
93 ed.setContent(pnl);
94 ed.showDialog();
95 if (ed.getValue() != 1) {
96 return null;
97 }
98 return (T) layerList.getSelectedItem();
99 }
100
101 protected void warnNoTargetLayersForSourceLayer(Layer sourceLayer) {
102 JOptionPane.showMessageDialog(Main.parent,
103 tr("<html>There are no layers the source layer<br>''{0}''<br>could be merged to.</html>",
104 Utils.escapeReservedCharactersHTML(sourceLayer.getName())),
105 tr("No target layers"), JOptionPane.WARNING_MESSAGE);
106 }
107}
Note: See TracBrowser for help on using the repository browser.