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

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

see #16841 - checkstyle

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