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