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