1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.remotecontrol;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.awt.Component;
|
---|
8 | import java.awt.Font;
|
---|
9 | import java.awt.GridBagLayout;
|
---|
10 | import java.awt.event.ActionEvent;
|
---|
11 | import java.awt.event.KeyEvent;
|
---|
12 | import java.awt.event.MouseEvent;
|
---|
13 | import java.util.Collection;
|
---|
14 | import java.util.HashMap;
|
---|
15 | import java.util.HashSet;
|
---|
16 | import java.util.Map;
|
---|
17 | import java.util.Map.Entry;
|
---|
18 | import java.util.Set;
|
---|
19 |
|
---|
20 | import javax.swing.AbstractAction;
|
---|
21 | import javax.swing.JCheckBox;
|
---|
22 | import javax.swing.JPanel;
|
---|
23 | import javax.swing.JTable;
|
---|
24 | import javax.swing.KeyStroke;
|
---|
25 | import javax.swing.table.DefaultTableModel;
|
---|
26 | import javax.swing.table.TableCellEditor;
|
---|
27 | import javax.swing.table.TableCellRenderer;
|
---|
28 | import javax.swing.table.TableModel;
|
---|
29 |
|
---|
30 | import org.openstreetmap.josm.Main;
|
---|
31 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
32 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
33 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
34 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
35 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
36 | import org.openstreetmap.josm.gui.util.TableHelper;
|
---|
37 | import org.openstreetmap.josm.tools.GBC;
|
---|
38 | import org.openstreetmap.josm.tools.Utils;
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Dialog to add tags as part of the remotecontrol.
|
---|
42 | * Existing Keys get grey color and unchecked selectboxes so they will not overwrite the old Key-Value-Pairs by default.
|
---|
43 | * You can choose the tags you want to add by selectboxes. You can edit the tags before you apply them.
|
---|
44 | * @author master
|
---|
45 | * @since 3850
|
---|
46 | */
|
---|
47 | public class AddTagsDialog extends ExtendedDialog {
|
---|
48 |
|
---|
49 | private final JTable propertyTable;
|
---|
50 | private final transient Collection<? extends OsmPrimitive> sel;
|
---|
51 | private final int[] count;
|
---|
52 |
|
---|
53 | private final String sender;
|
---|
54 | private static final Set<String> trustedSenders = new HashSet<>();
|
---|
55 |
|
---|
56 | static final class PropertyTableModel extends DefaultTableModel {
|
---|
57 | private final Class<?>[] types = {Boolean.class, String.class, Object.class, ExistingValues.class};
|
---|
58 |
|
---|
59 | PropertyTableModel(int rowCount) {
|
---|
60 | super(new String[] {tr("Assume"), tr("Key"), tr("Value"), tr("Existing values")}, rowCount);
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Override
|
---|
64 | public Class<?> getColumnClass(int c) {
|
---|
65 | return types[c];
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Class for displaying "delete from ... objects" in the table
|
---|
71 | */
|
---|
72 | static class DeleteTagMarker {
|
---|
73 | private final int num;
|
---|
74 |
|
---|
75 | DeleteTagMarker(int num) {
|
---|
76 | this.num = num;
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | public String toString() {
|
---|
81 | return tr("<delete from {0} objects>", num);
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Class for displaying list of existing tag values in the table
|
---|
87 | */
|
---|
88 | static class ExistingValues {
|
---|
89 | private final String tag;
|
---|
90 | private final Map<String, Integer> valueCount;
|
---|
91 |
|
---|
92 | ExistingValues(String tag) {
|
---|
93 | this.tag = tag;
|
---|
94 | this.valueCount = new HashMap<>();
|
---|
95 | }
|
---|
96 |
|
---|
97 | int addValue(String val) {
|
---|
98 | Integer c = valueCount.get(val);
|
---|
99 | int r = c == null ? 1 : (c.intValue()+1);
|
---|
100 | valueCount.put(val, r);
|
---|
101 | return r;
|
---|
102 | }
|
---|
103 |
|
---|
104 | @Override
|
---|
105 | public String toString() {
|
---|
106 | StringBuilder sb = new StringBuilder();
|
---|
107 | for (String k: valueCount.keySet()) {
|
---|
108 | if (sb.length() > 0) sb.append(", ");
|
---|
109 | sb.append(k);
|
---|
110 | }
|
---|
111 | return sb.toString();
|
---|
112 | }
|
---|
113 |
|
---|
114 | private String getToolTip() {
|
---|
115 | StringBuilder sb = new StringBuilder(64);
|
---|
116 | sb.append("<html>")
|
---|
117 | .append(tr("Old values of"))
|
---|
118 | .append(" <b>")
|
---|
119 | .append(tag)
|
---|
120 | .append("</b><br/>");
|
---|
121 | for (Entry<String, Integer> e : valueCount.entrySet()) {
|
---|
122 | sb.append("<b>")
|
---|
123 | .append(e.getValue())
|
---|
124 | .append(" x </b>")
|
---|
125 | .append(e.getKey())
|
---|
126 | .append("<br/>");
|
---|
127 | }
|
---|
128 | sb.append("</html>");
|
---|
129 | return sb.toString();
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Constructs a new {@code AddTagsDialog}.
|
---|
135 | * @param tags tags to add
|
---|
136 | * @param senderName String for skipping confirmations. Use empty string for always confirmed adding.
|
---|
137 | * @param primitives OSM objects that will be modified
|
---|
138 | */
|
---|
139 | public AddTagsDialog(String[][] tags, String senderName, Collection<? extends OsmPrimitive> primitives) {
|
---|
140 | super(Main.parent, tr("Add tags to selected objects"), new String[] {tr("Add selected tags"), tr("Add all tags"), tr("Cancel")},
|
---|
141 | false,
|
---|
142 | true);
|
---|
143 | setToolTipTexts(tr("Add checked tags to selected objects"), tr("Shift+Enter: Add all tags to selected objects"), "");
|
---|
144 |
|
---|
145 | this.sender = senderName;
|
---|
146 |
|
---|
147 | final DefaultTableModel tm = new PropertyTableModel(tags.length);
|
---|
148 |
|
---|
149 | sel = primitives;
|
---|
150 | count = new int[tags.length];
|
---|
151 |
|
---|
152 | for (int i = 0; i < tags.length; i++) {
|
---|
153 | count[i] = 0;
|
---|
154 | String key = tags[i][0];
|
---|
155 | String value = tags[i][1], oldValue;
|
---|
156 | Boolean b = Boolean.TRUE;
|
---|
157 | ExistingValues old = new ExistingValues(key);
|
---|
158 | for (OsmPrimitive osm : sel) {
|
---|
159 | oldValue = osm.get(key);
|
---|
160 | if (oldValue != null) {
|
---|
161 | old.addValue(oldValue);
|
---|
162 | if (!oldValue.equals(value)) {
|
---|
163 | b = Boolean.FALSE;
|
---|
164 | count[i]++;
|
---|
165 | }
|
---|
166 | }
|
---|
167 | }
|
---|
168 | tm.setValueAt(b, i, 0);
|
---|
169 | tm.setValueAt(tags[i][0], i, 1);
|
---|
170 | tm.setValueAt(tags[i][1].isEmpty() ? new DeleteTagMarker(count[i]) : tags[i][1], i, 2);
|
---|
171 | tm.setValueAt(old, i, 3);
|
---|
172 | }
|
---|
173 |
|
---|
174 | propertyTable = new JTable(tm) {
|
---|
175 |
|
---|
176 | @Override
|
---|
177 | public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
|
---|
178 | Component c = super.prepareRenderer(renderer, row, column);
|
---|
179 | if (count[row] > 0) {
|
---|
180 | c.setFont(c.getFont().deriveFont(Font.ITALIC));
|
---|
181 | c.setForeground(new Color(100, 100, 100));
|
---|
182 | } else {
|
---|
183 | c.setFont(c.getFont().deriveFont(Font.PLAIN));
|
---|
184 | c.setForeground(new Color(0, 0, 0));
|
---|
185 | }
|
---|
186 | return c;
|
---|
187 | }
|
---|
188 |
|
---|
189 | @Override
|
---|
190 | public TableCellEditor getCellEditor(int row, int column) {
|
---|
191 | Object value = getValueAt(row, column);
|
---|
192 | if (value instanceof DeleteTagMarker) return null;
|
---|
193 | if (value instanceof ExistingValues) return null;
|
---|
194 | return getDefaultEditor(value.getClass());
|
---|
195 | }
|
---|
196 |
|
---|
197 | @Override
|
---|
198 | public String getToolTipText(MouseEvent event) {
|
---|
199 | int r = rowAtPoint(event.getPoint());
|
---|
200 | int c = columnAtPoint(event.getPoint());
|
---|
201 | if (r < 0 || c < 0) {
|
---|
202 | return getToolTipText();
|
---|
203 | }
|
---|
204 | Object o = getValueAt(r, c);
|
---|
205 | if (c == 1 || c == 2) return o.toString();
|
---|
206 | if (c == 3) return ((ExistingValues) o).getToolTip();
|
---|
207 | return tr("Enable the checkbox to accept the value");
|
---|
208 | }
|
---|
209 | };
|
---|
210 |
|
---|
211 | propertyTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
|
---|
212 | // a checkbox has a size of 15 px
|
---|
213 | propertyTable.getColumnModel().getColumn(0).setMaxWidth(15);
|
---|
214 | TableHelper.adjustColumnWidth(propertyTable, 1, 150);
|
---|
215 | TableHelper.adjustColumnWidth(propertyTable, 2, 400);
|
---|
216 | TableHelper.adjustColumnWidth(propertyTable, 3, 300);
|
---|
217 | // get edit results if the table looses the focus, for example if a user clicks "add tags"
|
---|
218 | propertyTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
|
---|
219 | propertyTable.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.SHIFT_DOWN_MASK), "shiftenter");
|
---|
220 | propertyTable.getActionMap().put("shiftenter", new AbstractAction() {
|
---|
221 | @Override public void actionPerformed(ActionEvent e) {
|
---|
222 | buttonAction(1, e); // add all tags on Shift-Enter
|
---|
223 | }
|
---|
224 | });
|
---|
225 |
|
---|
226 | // set the content of this AddTagsDialog consisting of the tableHeader and the table itself.
|
---|
227 | JPanel tablePanel = new JPanel(new GridBagLayout());
|
---|
228 | tablePanel.add(propertyTable.getTableHeader(), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
229 | tablePanel.add(propertyTable, GBC.eol().fill(GBC.BOTH));
|
---|
230 | if (!sender.isEmpty() && !trustedSenders.contains(sender)) {
|
---|
231 | final JCheckBox c = new JCheckBox();
|
---|
232 | c.setAction(new AbstractAction(tr("Accept all tags from {0} for this session", sender)) {
|
---|
233 | @Override public void actionPerformed(ActionEvent e) {
|
---|
234 | if (c.isSelected())
|
---|
235 | trustedSenders.add(sender);
|
---|
236 | else
|
---|
237 | trustedSenders.remove(sender);
|
---|
238 | }
|
---|
239 | });
|
---|
240 | tablePanel.add(c, GBC.eol().insets(20, 10, 0, 0));
|
---|
241 | }
|
---|
242 | setContent(tablePanel);
|
---|
243 | setDefaultButton(2);
|
---|
244 | }
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * If you click the "Add tags" button build a ChangePropertyCommand for every key that has a checked checkbox
|
---|
248 | * to apply the key value pair to all selected osm objects.
|
---|
249 | * You get a entry for every key in the command queue.
|
---|
250 | */
|
---|
251 | @Override
|
---|
252 | protected void buttonAction(int buttonIndex, ActionEvent evt) {
|
---|
253 | // if layer all layers were closed, ignore all actions
|
---|
254 | if (buttonIndex != 2 && MainApplication.getLayerManager().getEditDataSet() != null) {
|
---|
255 | TableModel tm = propertyTable.getModel();
|
---|
256 | for (int i = 0; i < tm.getRowCount(); i++) {
|
---|
257 | if (buttonIndex == 1 || (Boolean) tm.getValueAt(i, 0)) {
|
---|
258 | String key = (String) tm.getValueAt(i, 1);
|
---|
259 | Object value = tm.getValueAt(i, 2);
|
---|
260 | MainApplication.undoRedo.add(new ChangePropertyCommand(sel,
|
---|
261 | key, value instanceof String ? (String) value : ""));
|
---|
262 | }
|
---|
263 | }
|
---|
264 | }
|
---|
265 | if (buttonIndex == 2) {
|
---|
266 | trustedSenders.remove(sender);
|
---|
267 | }
|
---|
268 | setVisible(false);
|
---|
269 | }
|
---|
270 |
|
---|
271 | /**
|
---|
272 | * parse addtags parameters Example URL (part):
|
---|
273 | * addtags=wikipedia:de%3DResidenzschloss Dresden|name:en%3DDresden Castle
|
---|
274 | * @param args request arguments
|
---|
275 | * @param sender is a string for skipping confirmations. Use empty string for always confirmed adding.
|
---|
276 | * @param primitives OSM objects that will be modified
|
---|
277 | */
|
---|
278 | public static void addTags(final Map<String, String> args, final String sender, final Collection<? extends OsmPrimitive> primitives) {
|
---|
279 | if (args.containsKey("addtags")) {
|
---|
280 | GuiHelper.executeByMainWorkerInEDT(() -> {
|
---|
281 | Set<String> tagSet = new HashSet<>();
|
---|
282 | for (String tag1 : Utils.decodeUrl(args.get("addtags")).split("\\|")) {
|
---|
283 | if (!tag1.trim().isEmpty() && tag1.contains("=")) {
|
---|
284 | tagSet.add(tag1.trim());
|
---|
285 | }
|
---|
286 | }
|
---|
287 | if (!tagSet.isEmpty()) {
|
---|
288 | String[][] keyValue = new String[tagSet.size()][2];
|
---|
289 | int i = 0;
|
---|
290 | for (String tag2 : tagSet) {
|
---|
291 | // support a = b===c as "a"="b===c"
|
---|
292 | String[] pair = tag2.split("\\s*=\\s*", 2);
|
---|
293 | keyValue[i][0] = pair[0];
|
---|
294 | keyValue[i][1] = pair.length < 2 ? "" : pair[1];
|
---|
295 | i++;
|
---|
296 | }
|
---|
297 | addTags(keyValue, sender, primitives);
|
---|
298 | }
|
---|
299 | });
|
---|
300 | }
|
---|
301 | }
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * Ask user and add the tags he confirm.
|
---|
305 | * @param keyValue is a table or {{tag1,val1},{tag2,val2},...}
|
---|
306 | * @param sender is a string for skipping confirmations. Use empty string for always confirmed adding.
|
---|
307 | * @param primitives OSM objects that will be modified
|
---|
308 | * @since 7521
|
---|
309 | */
|
---|
310 | public static void addTags(String[][] keyValue, String sender, Collection<? extends OsmPrimitive> primitives) {
|
---|
311 | if (trustedSenders.contains(sender)) {
|
---|
312 | if (MainApplication.getLayerManager().getEditDataSet() != null) {
|
---|
313 | for (String[] row : keyValue) {
|
---|
314 | MainApplication.undoRedo.add(new ChangePropertyCommand(primitives, row[0], row[1]));
|
---|
315 | }
|
---|
316 | }
|
---|
317 | } else {
|
---|
318 | new AddTagsDialog(keyValue, sender, primitives).showDialog();
|
---|
319 | }
|
---|
320 | }
|
---|
321 | }
|
---|