Changeset 1169 in josm for trunk/src/org/openstreetmap/josm/actions/PasteTagsAction.java
- Timestamp:
- 2008-12-23T15:07:05+01:00 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/PasteTagsAction.java
r1084 r1169 24 24 public final class PasteTagsAction extends JosmAction implements SelectionChangedListener { 25 25 26 27 28 29 30 31 32 33 26 public PasteTagsAction(JosmAction copyAction) { 27 super(tr("Paste Tags"), "pastetags", 28 tr("Apply tags of contents of paste buffer to all selected items."), 29 Shortcut.registerShortcut("system:pastestyle", tr("Edit: {0}", tr("Paste Tags")), KeyEvent.VK_V, Shortcut.GROUP_MENU, Shortcut.SHIFT_DEFAULT), true); 30 DataSet.selListeners.add(this); 31 copyAction.addListener(this); 32 setEnabled(false); 33 } 34 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 35 private void pasteKeys(Collection<Command> clist, Collection<? extends OsmPrimitive> pasteBufferSubset, Collection<OsmPrimitive> selectionSubset) { 36 /* scan the paste buffer, and add tags to each of the selected objects. 37 * If a tag already exists, it is overwritten */ 38 if (selectionSubset != null && ! selectionSubset.isEmpty()) { 39 for (Iterator<? extends OsmPrimitive> it = pasteBufferSubset.iterator(); it.hasNext();) { 40 OsmPrimitive osm = it.next(); 41 Map<String, String> m = osm.keys; 42 if(m != null) 43 { 44 for (String key : m.keySet()) { 45 if (! key.equals("created_by")) 46 clist.add(new ChangePropertyCommand(selectionSubset, key, osm.keys.get(key))); 47 } 48 } 49 } 50 } 51 } 52 52 53 54 55 56 57 58 59 60 61 53 public void actionPerformed(ActionEvent e) { 54 Collection<Command> clist = new LinkedList<Command>(); 55 pasteKeys(clist, Main.pasteBuffer.nodes, Main.ds.getSelectedNodes()); 56 pasteKeys(clist, Main.pasteBuffer.ways, Main.ds.getSelectedWays()); 57 pasteKeys(clist, Main.pasteBuffer.relations, Main.ds.getSelectedRelations()); 58 Main.main.undoRedo.add(new SequenceCommand(tr("Paste Tags"), clist)); 59 Main.ds.setSelected(Main.ds.getSelected()); // to force selection listeners, in particular the tag panel, to update 60 Main.map.mapView.repaint(); 61 } 62 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 63 private boolean containsSameKeysWithDifferentValues(Collection<? extends OsmPrimitive> osms) { 64 Map<String,String> kvSeen = new HashMap<String,String>(); 65 for (Iterator<? extends OsmPrimitive> it = osms.iterator(); it.hasNext();) { 66 OsmPrimitive osm = it.next(); 67 if (osm.keys == null || osm.keys.isEmpty()) 68 continue; 69 for (String key : osm.keys.keySet()) { 70 if (key.equals("created_by")) // we ignore created_by 71 continue; 72 String value = osm.keys.get(key); 73 if (! kvSeen.containsKey(key)) 74 kvSeen.put(key, value); 75 else if (! kvSeen.get(key).equals(value)) 76 return true; 77 } 78 } 79 return false; 80 } 81 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 82 /** 83 * Determines whether to enable the widget depending on the contents of the paste 84 * buffer and current selection 85 * @param pasteBuffer 86 */ 87 private void possiblyEnable(Collection<? extends OsmPrimitive> selection, DataSet pasteBuffer) { 88 /* only enable if there is something selected to paste into and 89 if we don't have conflicting keys in the pastebuffer */ 90 setEnabled(selection != null && 91 ! selection.isEmpty() && 92 ! pasteBuffer.allPrimitives().isEmpty() && 93 (Main.ds.getSelectedNodes().isEmpty() || 94 ! containsSameKeysWithDifferentValues(pasteBuffer.nodes)) && 95 (Main.ds.getSelectedWays().isEmpty() || 96 ! containsSameKeysWithDifferentValues(pasteBuffer.ways)) && 97 (Main.ds.getSelectedRelations().isEmpty() || 98 ! containsSameKeysWithDifferentValues(pasteBuffer.relations))); 99 } 100 100 101 102 103 101 @Override public void pasteBufferChanged(DataSet newPasteBuffer) { 102 possiblyEnable(Main.ds.getSelected(), newPasteBuffer); 103 } 104 104 105 106 107 105 public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) { 106 possiblyEnable(newSelection, Main.pasteBuffer); 107 } 108 108 }
Note:
See TracChangeset
for help on using the changeset viewer.