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