[213] | 1 | package org.openstreetmap.josm.actions;
|
---|
| 2 |
|
---|
| 3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 4 |
|
---|
| 5 | import java.awt.GridBagLayout;
|
---|
| 6 | import java.awt.event.ActionEvent;
|
---|
| 7 | import java.awt.event.KeyEvent;
|
---|
| 8 | import java.util.Collection;
|
---|
| 9 | import java.util.HashMap;
|
---|
| 10 | import java.util.LinkedList;
|
---|
| 11 | import java.util.Map;
|
---|
| 12 | import java.util.Set;
|
---|
| 13 | import java.util.TreeMap;
|
---|
| 14 | import java.util.TreeSet;
|
---|
| 15 | import java.util.Map.Entry;
|
---|
| 16 |
|
---|
| 17 | import javax.swing.Box;
|
---|
| 18 | import javax.swing.JComboBox;
|
---|
| 19 | import javax.swing.JLabel;
|
---|
| 20 | import javax.swing.JOptionPane;
|
---|
| 21 | import javax.swing.JPanel;
|
---|
| 22 |
|
---|
| 23 | import org.openstreetmap.josm.Main;
|
---|
| 24 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
| 25 | import org.openstreetmap.josm.command.Command;
|
---|
| 26 | import org.openstreetmap.josm.command.DeleteCommand;
|
---|
| 27 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
| 28 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
| 29 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 30 | import org.openstreetmap.josm.data.osm.Way;
|
---|
| 31 | import org.openstreetmap.josm.tools.GBC;
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * Combines multiple ways into one.
|
---|
| 35 | *
|
---|
| 36 | * @author Imi
|
---|
| 37 | */
|
---|
| 38 | public class CombineWayAction extends JosmAction implements SelectionChangedListener {
|
---|
| 39 |
|
---|
| 40 | public CombineWayAction() {
|
---|
| 41 | super(tr("Combine Way"), "combineway", tr("Combine several ways into one."), KeyEvent.VK_C, KeyEvent.CTRL_MASK | KeyEvent.SHIFT_MASK, true);
|
---|
| 42 | Main.ds.listeners.add(this);
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | public void actionPerformed(ActionEvent event) {
|
---|
| 46 | Collection<OsmPrimitive> selection = Main.ds.getSelected();
|
---|
| 47 | LinkedList<Way> selectedWays = new LinkedList<Way>();
|
---|
| 48 |
|
---|
| 49 | for (OsmPrimitive osm : selection)
|
---|
| 50 | if (osm instanceof Way)
|
---|
| 51 | selectedWays.add((Way)osm);
|
---|
| 52 |
|
---|
| 53 | if (selectedWays.size() < 2) {
|
---|
| 54 | JOptionPane.showMessageDialog(Main.parent, tr("Please select at least two ways to combine."));
|
---|
| 55 | return;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | // collect properties for later conflict resolving
|
---|
| 59 | Map<String, Set<String>> props = new TreeMap<String, Set<String>>();
|
---|
| 60 | for (Way w : selectedWays) {
|
---|
| 61 | for (Entry<String,String> e : w.entrySet()) {
|
---|
| 62 | if (!props.containsKey(e.getKey()))
|
---|
| 63 | props.put(e.getKey(), new TreeSet<String>());
|
---|
| 64 | props.get(e.getKey()).add(e.getValue());
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | Way oldWay = selectedWays.poll();
|
---|
| 69 | Way newWay = new Way(oldWay);
|
---|
| 70 | LinkedList<Command> cmds = new LinkedList<Command>();
|
---|
| 71 |
|
---|
| 72 | for (Way w : selectedWays)
|
---|
| 73 | newWay.segments.addAll(w.segments);
|
---|
| 74 |
|
---|
| 75 | // display conflict dialog
|
---|
| 76 | Map<String, JComboBox> components = new HashMap<String, JComboBox>();
|
---|
| 77 | JPanel p = new JPanel(new GridBagLayout());
|
---|
| 78 | for (Entry<String, Set<String>> e : props.entrySet()) {
|
---|
| 79 | if (e.getValue().size() > 1) {
|
---|
| 80 | JComboBox c = new JComboBox(e.getValue().toArray());
|
---|
| 81 | c.setEditable(true);
|
---|
| 82 | p.add(new JLabel(e.getKey()), GBC.std());
|
---|
| 83 | p.add(Box.createHorizontalStrut(10), GBC.std());
|
---|
| 84 | p.add(c, GBC.eol());
|
---|
| 85 | components.put(e.getKey(), c);
|
---|
[271] | 86 | } else
|
---|
| 87 | newWay.put(e.getKey(), e.getValue().iterator().next());
|
---|
[213] | 88 | }
|
---|
| 89 | if (!components.isEmpty()) {
|
---|
| 90 | int answer = JOptionPane.showConfirmDialog(Main.parent, p, tr("Enter values for all conflicts."), JOptionPane.OK_CANCEL_OPTION);
|
---|
| 91 | if (answer != JOptionPane.OK_OPTION)
|
---|
| 92 | return;
|
---|
| 93 | for (Entry<String, JComboBox> e : components.entrySet())
|
---|
| 94 | newWay.put(e.getKey(), e.getValue().getEditor().getItem().toString());
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | cmds.add(new DeleteCommand(selectedWays));
|
---|
| 98 | cmds.add(new ChangeCommand(oldWay, newWay));
|
---|
| 99 | Main.main.editLayer().add(new SequenceCommand(tr("Combine {0} Ways", selectedWays.size()), cmds));
|
---|
| 100 | Main.ds.setSelected(oldWay);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | /**
|
---|
| 104 | * Enable the "Combine way" menu option if more then one way is selected
|
---|
| 105 | */
|
---|
| 106 | public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
|
---|
| 107 | boolean first = false;
|
---|
| 108 | for (OsmPrimitive osm : newSelection) {
|
---|
| 109 | if (osm instanceof Way) {
|
---|
| 110 | if (first) {
|
---|
| 111 | setEnabled(true);
|
---|
| 112 | return;
|
---|
| 113 | }
|
---|
| 114 | first = true;
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 | setEnabled(false);
|
---|
| 118 | }
|
---|
| 119 | }
|
---|