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