source: josm/trunk/src/org/openstreetmap/josm/actions/CreateMultipolygonAction.java@ 4958

Last change on this file since 4958 was 4958, checked in by stoecker, 12 years ago

fix remaining shortcut deprecations

  • Property svn:eol-style set to native
File size: 8.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.awt.event.KeyEvent;
8import java.util.ArrayList;
9import java.util.Collection;
10import java.util.HashMap;
11import java.util.List;
12import java.util.Map;
13
14import javax.swing.JOptionPane;
15
16import javax.swing.SwingUtilities;
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.command.AddCommand;
19import org.openstreetmap.josm.command.ChangePropertyCommand;
20import org.openstreetmap.josm.command.Command;
21import org.openstreetmap.josm.command.SequenceCommand;
22import org.openstreetmap.josm.data.osm.MultipolygonCreate;
23import org.openstreetmap.josm.data.osm.MultipolygonCreate.JoinedPolygon;
24import org.openstreetmap.josm.data.osm.OsmPrimitive;
25import org.openstreetmap.josm.data.osm.Relation;
26import org.openstreetmap.josm.data.osm.RelationMember;
27import org.openstreetmap.josm.data.osm.Way;
28import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor;
29import org.openstreetmap.josm.tools.Shortcut;
30
31/**
32 * Create multipolygon from selected ways automatically.
33 *
34 * New relation with type=multipolygon is created
35 *
36 * If one or more of ways is already in relation with type=multipolygon or the
37 * way is not closed, then error is reported and no relation is created
38 *
39 * The "inner" and "outer" roles are guessed automatically. First, bbox is
40 * calculated for each way. then the largest area is assumed to be outside and
41 * the rest inside. In cases with one "outside" area and several cut-ins, the
42 * guess should be always good ... In more complex (multiple outer areas) or
43 * buggy (inner and outer ways intersect) scenarios the result is likely to be
44 * wrong.
45 */
46public class CreateMultipolygonAction extends JosmAction {
47
48 public CreateMultipolygonAction() {
49 super(tr("Create multipolygon"), "multipoly_create", tr("Create multipolygon."),
50 Shortcut.registerShortcut("tools:multipoly", tr("Tool: {0}", tr("Create multipolygon")),
51 KeyEvent.VK_A, Shortcut.GROUP_MENU+Shortcut.GROUPS_ALT2), true);
52 }
53 /**
54 * The action button has been clicked
55 *
56 * @param e Action Event
57 */
58 public void actionPerformed(ActionEvent e) {
59 if (Main.main.getEditLayer() == null) {
60 JOptionPane.showMessageDialog(Main.parent, tr("No data loaded."));
61 return;
62 }
63
64 Collection<Way> selectedWays = Main.main.getCurrentDataSet().getSelectedWays();
65
66 if (selectedWays.size() < 1) {
67 // Sometimes it make sense creating multipoly of only one way (so it will form outer way)
68 // and then splitting the way later (so there are multiple ways forming outer way)
69 JOptionPane.showMessageDialog(Main.parent, tr("You must select at least one way."));
70 return;
71 }
72
73 MultipolygonCreate polygon = this.analyzeWays(selectedWays);
74
75 if (polygon == null)
76 return; //could not make multipolygon.
77
78 final Relation relation = this.createRelation(polygon);
79
80 if (Main.pref.getBoolean("multipoly.show-relation-editor", false)) {
81 //Open relation edit window, if set up in preferences
82 RelationEditor editor = RelationEditor.getEditor(Main.main.getEditLayer(), relation, null);
83
84 editor.setModal(true);
85 editor.setVisible(true);
86
87 //TODO: cannot get the resulting relation from RelationEditor :(.
88 /*
89 if (relationCountBefore < relationCountAfter) {
90 //relation saved, clean up the tags
91 List<Command> list = this.removeTagsFromInnerWays(relation);
92 if (list.size() > 0)
93 {
94 Main.main.undoRedo.add(new SequenceCommand(tr("Remove tags from multipolygon inner ways"), list));
95 }
96 }
97 */
98
99 } else {
100 //Just add the relation
101 List<Command> list = this.removeTagsFromInnerWays(relation);
102 list.add(new AddCommand(relation));
103 Main.main.undoRedo.add(new SequenceCommand(tr("Create multipolygon"), list));
104 // Use 'SwingUtilities.invokeLater' to make sure the relationListDialog
105 // knows about the new relation before we try to select it.
106 // (Yes, we are already in event dispatch thread. But DatasetEventManager
107 // uses 'SwingUtilities.invokeLater' to fire events so we have to do
108 // the same.)
109 SwingUtilities.invokeLater(new Runnable() {
110 public void run() {
111 Main.map.relationListDialog.selectRelation(relation);
112 }
113 });
114 }
115
116
117 }
118
119 /** Enable this action only if something is selected */
120 @Override protected void updateEnabledState() {
121 if (getCurrentDataSet() == null) {
122 setEnabled(false);
123 } else {
124 updateEnabledState(getCurrentDataSet().getSelected());
125 }
126 }
127
128 /** Enable this action only if something is selected */
129 @Override protected void updateEnabledState(Collection < ? extends OsmPrimitive > selection) {
130 setEnabled(selection != null && !selection.isEmpty());
131 }
132
133 /**
134 * This method analyzes ways and creates multipolygon.
135 * @param selectedWays
136 * @return null, if there was a problem with the ways.
137 */
138 private MultipolygonCreate analyzeWays(Collection < Way > selectedWays) {
139
140 MultipolygonCreate pol = new MultipolygonCreate();
141 String error = pol.makeFromWays(selectedWays);
142
143 if (error != null) {
144 JOptionPane.showMessageDialog(Main.parent, error);
145 return null;
146 } else {
147 return pol;
148 }
149 }
150
151 /**
152 * Builds a relation from polygon ways.
153 * @param pol
154 * @return
155 */
156 private Relation createRelation(MultipolygonCreate pol) {
157 // Create new relation
158 Relation rel = new Relation();
159 rel.put("type", "multipolygon");
160 // Add ways to it
161 for (JoinedPolygon jway:pol.outerWays) {
162 for (Way way:jway.ways) {
163 rel.addMember(new RelationMember("outer", way));
164 }
165 }
166
167 for (JoinedPolygon jway:pol.innerWays) {
168 for (Way way:jway.ways) {
169 rel.addMember(new RelationMember("inner", way));
170 }
171 }
172 return rel;
173 }
174
175 /**
176 * This method removes tags/value pairs from inner ways that are present in relation or outer ways.
177 * @param relation
178 */
179 private List<Command> removeTagsFromInnerWays(Relation relation) {
180 Map<String, String> values = new HashMap<String, String>();
181
182 if (relation.hasKeys()){
183 for(String key: relation.keySet()) {
184 values.put(key, relation.get(key));
185 }
186 }
187
188 List<Way> innerWays = new ArrayList<Way>();
189
190 for (RelationMember m: relation.getMembers()) {
191
192 if (m.hasRole() && m.getRole() == "inner" && m.isWay() && m.getWay().hasKeys()) {
193 innerWays.add(m.getWay());
194 }
195
196 if (m.hasRole() && m.getRole() == "outer" && m.isWay() && m.getWay().hasKeys()) {
197 Way way = m.getWay();
198 for (String key: way.keySet()) {
199 if (!values.containsKey(key)) { //relation values take precedence
200 values.put(key, way.get(key));
201 }
202 }
203 }
204 }
205
206 List<Command> commands = new ArrayList<Command>();
207
208 for(String key: values.keySet()) {
209 List<OsmPrimitive> affectedWays = new ArrayList<OsmPrimitive>();
210 String value = values.get(key);
211
212 for (Way way: innerWays) {
213 if (value.equals(way.get(key))) {
214 affectedWays.add(way);
215 }
216 }
217
218 if (affectedWays.size() > 0) {
219 commands.add(new ChangePropertyCommand(affectedWays, key, null));
220 }
221 }
222
223 return commands;
224 }
225}
Note: See TracBrowser for help on using the repository browser.