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

Last change on this file since 290 was 290, checked in by imi, 18 years ago
  • added support for multiple data layers
File size: 3.8 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.DataSet;
30import org.openstreetmap.josm.data.osm.OsmPrimitive;
31import org.openstreetmap.josm.data.osm.Way;
32import org.openstreetmap.josm.tools.GBC;
33
34/**
35 * Combines multiple ways into one.
36 *
37 * @author Imi
38 */
39public 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}
Note: See TracBrowser for help on using the repository browser.