Changeset 5809 in josm for trunk/src/org
- Timestamp:
- 2013-03-29T12:16:54+01:00 (12 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/PasteTagsAction.java
r5738 r5809 6 6 import static org.openstreetmap.josm.tools.I18n.trn; 7 7 8 import java.awt.GridBagLayout;9 8 import java.awt.event.ActionEvent; 10 9 import java.awt.event.KeyEvent; … … 14 13 import java.util.List; 15 14 import java.util.Map; 16 import javax.swing.JLabel;17 import javax.swing.JOptionPane;18 import javax.swing.JPanel;19 15 20 16 import org.openstreetmap.josm.Main; … … 27 23 import org.openstreetmap.josm.data.osm.Tag; 28 24 import org.openstreetmap.josm.data.osm.TagCollection; 29 import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;30 25 import org.openstreetmap.josm.gui.conflict.tags.PasteTagsConflictResolverDialog; 31 26 import org.openstreetmap.josm.gui.help.HelpUtil; 32 import org.openstreetmap.josm.tools.GBC;33 27 import org.openstreetmap.josm.tools.Shortcut; 34 28 import org.openstreetmap.josm.tools.TextTagParser; 35 import org.openstreetmap.josm.tools.UrlLabel;36 29 import org.openstreetmap.josm.tools.Utils; 37 30 … … 46 39 public final class PasteTagsAction extends JosmAction { 47 40 41 private static final String help = ht("/Action/PasteTags"); 42 private static final String helpUrl = HelpUtil.getHelpTopicUrl(HelpUtil.buildAbsoluteHelpTopic(help)); 43 48 44 public PasteTagsAction() { 49 45 super(tr("Paste Tags"), "pastetags", … … 51 47 Shortcut.registerShortcut("system:pastestyle", tr("Edit: {0}", tr("Paste Tags")), 52 48 KeyEvent.VK_V, Shortcut.CTRL_SHIFT), true); 53 putValue("help", ht("/Action/PasteTags")); 54 } 55 56 private void showBadBufferMessage() { 57 String msg = tr("<html><p> Sorry, it is impossible to paste tags from buffer. It does not contain any JOSM object" 58 + " or suitable text. </p></html>"); 59 JPanel p = new JPanel(new GridBagLayout()); 60 p.add(new JLabel(msg),GBC.eop()); 61 p.add(new UrlLabel( 62 HelpUtil.getHelpTopicUrl(HelpUtil.buildAbsoluteHelpTopic((String)getValue("help")))), 63 GBC.eop()); 64 65 ConditionalOptionPaneUtil.showMessageDialog( 66 "paste_badbuffer", Main.parent, 67 p, tr("Warning"), JOptionPane.WARNING_MESSAGE); 49 putValue("help", help); 68 50 } 69 51 … … 269 251 270 252 String buf = Utils.getClipboardContent(); 271 253 if (buf == null || buf.isEmpty() || buf.matches("(\\d+,)*\\d+")) { 254 pasteTagsFromJOSMBuffer(selection); 255 } else { 256 // Paste tags from arbitrary text 257 pasteTagsFromText(selection, buf); 258 } 259 } 260 261 /** Paste tags from arbitrary text 262 * @return false if action was successful 263 */ 264 public static boolean pasteTagsFromText(Collection<OsmPrimitive> selection, String text) { 265 Map<String, String> tags = TextTagParser.readTagsFromText(text); 272 266 List<Command> commands = new ArrayList<Command>(); 273 if (buf==null) { 274 showBadBufferMessage(); 275 return; 276 } 277 if (buf.matches("(\\d+,)*\\d+")) { // Paste tags from JOSM buffer 278 PasteTagsAction.TagPaster tagPaster = new PasteTagsAction.TagPaster(Main.pasteBuffer.getDirectlyAdded(), selection); 279 for (Tag tag: tagPaster.execute()) { 280 commands.add(new ChangePropertyCommand(selection, tag.getKey(), "".equals(tag.getValue())?null:tag.getValue())); 281 } 282 } else { // Paste tags from arbitrary text 283 Map<String, String> tags = TextTagParser.readTagsFromText(buf); 284 if (tags==null || tags.isEmpty()) { 285 showBadBufferMessage(); 286 return; 287 } 288 if (!TextTagParser.validateTags(tags)) return; 289 String v; 290 for (String key: tags.keySet()) { 291 v = tags.get(key); 292 commands.add(new ChangePropertyCommand(selection, key, "".equals(v)?null:v)); 293 } 294 } 267 if (tags==null || tags.isEmpty()) { 268 TextTagParser.showBadBufferMessage(helpUrl); 269 return false; 270 }; 271 if (!TextTagParser.validateTags(tags)) return false; 272 String v; 273 for (String key: tags.keySet()) { 274 v = tags.get(key); 275 commands.add(new ChangePropertyCommand(selection, key, "".equals(v)?null:v)); 276 } 277 commitCommands(selection, commands); 278 return !commands.isEmpty(); 279 } 280 281 /** Paste tags from JOSM buffer 282 * @param selection objects 283 * @param commands 284 * @return 285 */ 286 public static boolean pasteTagsFromJOSMBuffer(Collection<OsmPrimitive> selection) { 287 List<PrimitiveData> directlyAdded = Main.pasteBuffer.getDirectlyAdded(); 288 if (directlyAdded==null || directlyAdded.isEmpty()) return false; 289 290 PasteTagsAction.TagPaster tagPaster = new PasteTagsAction.TagPaster(directlyAdded, selection); 291 List<Command> commands = new ArrayList<Command>(); 292 for (Tag tag : tagPaster.execute()) { 293 commands.add(new ChangePropertyCommand(selection, tag.getKey(), "".equals(tag.getValue()) ? null : tag.getValue())); 294 } 295 commitCommands(selection, commands); 296 return true; 297 } 298 299 /** 300 * Create and execute SequenceCommand with descriptive title 301 * @param commands 302 */ 303 private static void commitCommands(Collection<OsmPrimitive> selection, List<Command> commands) { 295 304 if (!commands.isEmpty()) { 296 305 String title1 = trn("Pasting {0} tag", "Pasting {0} tags", commands.size(), commands.size()); … … 302 311 )); 303 312 } 304 305 } 306 307 313 } 314 308 315 @Override 309 316 protected void updateEnabledState() { -
trunk/src/org/openstreetmap/josm/tools/TextTagParser.java
r5756 r5809 1 1 package org.openstreetmap.josm.tools; 2 2 3 import java.awt.GridBagLayout; 3 4 import java.util.Arrays; 4 5 import java.util.HashMap; … … 6 7 import java.util.regex.Matcher; 7 8 import java.util.regex.Pattern; 9 import javax.swing.JLabel; 8 10 import javax.swing.JOptionPane; 11 import javax.swing.JPanel; 9 12 import org.openstreetmap.josm.Main; 10 13 import org.openstreetmap.josm.gui.ExtendedDialog; … … 224 227 * Check tags for correctness and display warnings if needed 225 228 * @param tags - map key->value to check 226 * @return true if user decision was "OK"229 * @return true if the tags shoul be pasted 227 230 */ 228 231 public static boolean validateTags(Map<String, String> tags) { … … 234 237 r=warning(trn("There was {0} tag found in the buffer, it is suspicious!", 235 238 "There were {0} tags found in the buffer, it is suspicious!", s, 236 s), "", "t oomanytags");237 if (r==2 ) return false; if (r==3) return true;239 s), "", "tags.paste.toomanytags"); 240 if (r==2 || r==3) return false; if (r==4) return true; 238 241 } 239 242 for (String key: tags.keySet()) { 240 243 value = tags.get(key); 241 244 if (key.length() > MAX_KEY_LENGTH) { 242 r = warning(tr("Key is too long (max {0} characters):", MAX_KEY_LENGTH), key+"="+value, " keytoolong");243 if (r==2 ) return false; if (r==3) return true;245 r = warning(tr("Key is too long (max {0} characters):", MAX_KEY_LENGTH), key+"="+value, "tags.paste.keytoolong"); 246 if (r==2 || r==3) return false; if (r==4) return true; 244 247 } 245 248 if (!key.matches(KEY_PATTERN)) { 246 r = warning(tr("Suspicious characters in key:"), key, " keydoesnotmatch");247 if (r==2 ) return false; if (r==3) return true;249 r = warning(tr("Suspicious characters in key:"), key, "tags.paste.keydoesnotmatch"); 250 if (r==2 || r==3) return false; if (r==4) return true; 248 251 } 249 252 if (value.length() > MAX_VALUE_LENGTH) { 250 r = warning(tr("Value is too long (max {0} characters):", MAX_VALUE_LENGTH), value, " valuetoolong");251 if (r==2 ) return false; if (r==3) return true;253 r = warning(tr("Value is too long (max {0} characters):", MAX_VALUE_LENGTH), value, "tags.paste.valuetoolong"); 254 if (r==2 || r==3) return false; if (r==4) return true; 252 255 } 253 256 } … … 259 262 Main.parent, 260 263 tr("Do you want to paste these tags?"), 261 new String[]{tr("Ok"), tr("Cancel"), tr(" Ignore warnings")});262 ed.setButtonIcons(new String[]{"ok.png", "cancel.png", " pastetags.png"});264 new String[]{tr("Ok"), tr("Cancel"), tr("Clear buffer"), tr("Ignore warnings")}); 265 ed.setButtonIcons(new String[]{"ok.png", "cancel.png", "dialogs/delete.png", "pastetags.png"}); 263 266 ed.setContent("<html><b>"+text + "</b><br/><br/><div width=\"300px\">"+XmlWriter.encode(data,true)+"</html>"); 264 267 ed.setDefaultButton(2); … … 267 270 ed.toggleEnable(code); 268 271 ed.showDialog(); 269 Object o = ed.getValue(); 270 if (o instanceof Integer) 271 return ((Integer)o).intValue(); 272 else 273 return 2; 272 int r = ed.getValue(); 273 if (r==0) r = 2; 274 // clean clipboard if user asked 275 if (r==3) Utils.copyToClipboard(""); 276 return r; 277 } 278 279 /** 280 * Shows message that the buffer can not be pasted, allowing user to clean the buffer 281 * @param helpUrl 282 */ 283 public static void showBadBufferMessage(String helpUrl) { 284 String msg = tr("<html><p> Sorry, it is impossible to paste tags from buffer. It does not contain any JOSM object" 285 + " or suitable text. </p></html>"); 286 JPanel p = new JPanel(new GridBagLayout()); 287 p.add(new JLabel(msg),GBC.eop()); 288 if (helpUrl != null) { 289 p.add(new UrlLabel(helpUrl), GBC.eop()); 290 } 291 292 ExtendedDialog ed = new ExtendedDialog( 293 Main.parent, 294 tr("Warning"), 295 new String[]{tr("Ok"), tr("Clear buffer")}); 296 297 ed.setButtonIcons(new String[]{"ok.png", "dialogs/delete.png"}); 298 299 ed.setContent(p); 300 ed.setDefaultButton(1); 301 ed.setCancelButton(1); 302 ed.setIcon(JOptionPane.WARNING_MESSAGE); 303 ed.toggleEnable("tags.paste.cleanbadbuffer"); 304 ed.showDialog(); 305 306 int r = ed.getValue(); 307 // clean clipboard if user asked 308 if (r==2) Utils.copyToClipboard(""); 274 309 } 275 310 }
Note:
See TracChangeset
for help on using the changeset viewer.