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

Last change on this file since 13494 was 13206, checked in by Don-vip, 6 years ago

enable PMD rule OptimizableToArrayCall

  • Property svn:eol-style set to native
File size: 6.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.JLabel;
13import javax.swing.JList;
14import javax.swing.JOptionPane;
15import javax.swing.JPanel;
16
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.gui.ExtendedDialog;
19import org.openstreetmap.josm.gui.layer.Layer;
20import org.openstreetmap.josm.gui.widgets.JosmComboBox;
21import org.openstreetmap.josm.tools.GBC;
22import org.openstreetmap.josm.tools.Shortcut;
23import org.openstreetmap.josm.tools.Utils;
24
25/**
26 * Abstract superclass of different "Merge" actions.
27 * @since 1890
28 */
29public abstract class AbstractMergeAction extends JosmAction {
30
31 /**
32 * the list cell renderer used to render layer list entries
33 */
34 public static class LayerListCellRenderer extends DefaultListCellRenderer {
35
36 @Override
37 public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
38 Layer layer = (Layer) value;
39 JLabel label = (JLabel) super.getListCellRendererComponent(list, layer.getName(), index, isSelected, cellHasFocus);
40 label.setIcon(layer.getIcon());
41 label.setToolTipText(layer.getToolTipText());
42 return label;
43 }
44 }
45
46 /**
47 * Constructs a new {@code AbstractMergeAction}.
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?
57 */
58 public AbstractMergeAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register) {
59 super(name, iconName, tooltip, shortcut, register);
60 }
61
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 */
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
81 /**
82 * Ask user to choose the target layer.
83 * @param targetLayers list of candidate target layers.
84 * @return the chosen layer
85 */
86 protected static Layer askTargetLayer(List<Layer> targetLayers) {
87 return askTargetLayer(targetLayers.toArray(new Layer[0]),
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);
106 layerList.setRenderer(new LayerListCellRenderer());
107 layerList.setSelectedIndex(0);
108
109 JPanel pnl = new JPanel(new GridBagLayout());
110 pnl.add(new JLabel(label), GBC.eol());
111 pnl.add(layerList, GBC.eol().fill(GBC.HORIZONTAL));
112 if (GraphicsEnvironment.isHeadless()) {
113 // return first layer in headless mode, for unit tests
114 return targetLayers[0];
115 }
116 ExtendedDialog ed = new ExtendedDialog(Main.parent, title, buttonText, tr("Cancel"));
117 ed.setButtonIcons(buttonIcon, "cancel");
118 ed.setContent(pnl);
119 ed.showDialog();
120 if (ed.getValue() != 1) {
121 return null;
122 }
123 return (T) layerList.getSelectedItem();
124 }
125
126 /**
127 * Warns user when there no layers the source layer could be merged to.
128 * @param sourceLayer source layer
129 */
130 protected void warnNoTargetLayersForSourceLayer(Layer sourceLayer) {
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 }
136 }
137}
Note: See TracBrowser for help on using the repository browser.