source: josm/src/org/openstreetmap/josm/actions/CombineWayAction.java@ 283

Last change on this file since 283 was 283, checked in by imi, 17 years ago
  • fixed lots of typos (thanks Bruce)
File size: 3.7 KB
Line 
1package org.openstreetmap.josm.actions;
2
3import static org.openstreetmap.josm.tools.I18n.tr;
4
5import java.awt.GridBagLayout;
6import java.awt.event.ActionEvent;
7import java.awt.event.KeyEvent;
8import java.util.Collection;
9import java.util.HashMap;
10import java.util.LinkedList;
11import java.util.Map;
12import java.util.Set;
13import java.util.TreeMap;
14import java.util.TreeSet;
15import java.util.Map.Entry;
16
17import javax.swing.Box;
18import javax.swing.JComboBox;
19import javax.swing.JLabel;
20import javax.swing.JOptionPane;
21import javax.swing.JPanel;
22
23import org.openstreetmap.josm.Main;
24import org.openstreetmap.josm.command.ChangeCommand;
25import org.openstreetmap.josm.command.Command;
26import org.openstreetmap.josm.command.DeleteCommand;
27import org.openstreetmap.josm.command.SequenceCommand;
28import org.openstreetmap.josm.data.SelectionChangedListener;
29import org.openstreetmap.josm.data.osm.OsmPrimitive;
30import org.openstreetmap.josm.data.osm.Way;
31import org.openstreetmap.josm.tools.GBC;
32
33/**
34 * Combines multiple ways into one.
35 *
36 * @author Imi
37 */
38public 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);
86 } else
87 newWay.put(e.getKey(), e.getValue().iterator().next());
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}
Note: See TracBrowser for help on using the repository browser.