Changeset 6597 in josm
- Timestamp:
- 2014-01-02T22:45:23+01:00 (11 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/CreateMultipolygonAction.java
r6575 r6597 36 36 import org.openstreetmap.josm.tools.Pair; 37 37 import org.openstreetmap.josm.tools.Shortcut; 38 import org.openstreetmap.josm.tools.Utils; 38 39 39 40 /** … … 54 55 public class CreateMultipolygonAction extends JosmAction { 55 56 57 private final boolean update; 58 56 59 /** 57 60 * Constructs a new {@code CreateMultipolygonAction}. 58 61 */ 59 public CreateMultipolygonAction() { 60 super(tr("Create multipolygon"), "multipoly_create", tr("Create multipolygon"), 61 Shortcut.registerShortcut("tools:multipoly", tr("Tool: {0}", tr("Create multipolygon")), 62 KeyEvent.VK_A, Shortcut.ALT_CTRL), true); 63 } 62 public CreateMultipolygonAction(final boolean update) { 63 super(getName(update), "multipoly_create", getName(update), 64 update 65 ? null 66 : Shortcut.registerShortcut("tools:multipoly", tr("Tool: {0}", getName(false)), KeyEvent.VK_A, Shortcut.ALT_CTRL), 67 true, update ? "multipoly_update" : "multipoly_create", true); 68 this.update = update; 69 } 70 71 private static String getName(boolean update) { 72 return update ? tr("Update multipolygon") : tr("Create multipolygon"); 73 } 74 64 75 /** 65 76 * The action button has been clicked … … 92 103 } 93 104 94 final Relation multipolygonRelation = getSelectedMultipolygonRelation(selectedRelations); 105 final Relation multipolygonRelation = update 106 ? getSelectedMultipolygonRelation(selectedWays, selectedRelations) 107 : null; 95 108 if (multipolygonRelation != null && (multipolygonRelation.isIncomplete() || multipolygonRelation.hasIncompleteMembers())) { 96 109 new Notification( … … 102 115 } 103 116 104 final Pair<SequenceCommand, Relation> commandAndRelation = createMultipolygonCommand(selectedWays, selectedRelations);117 final Pair<SequenceCommand, Relation> commandAndRelation = createMultipolygonCommand(selectedWays, multipolygonRelation); 105 118 if (commandAndRelation == null) { 106 119 return; … … 131 144 } 132 145 133 private static Relation getSelectedMultipolygonRelation(Collection<Relation> selectedRelations) { 134 return selectedRelations.size() == 1 && "multipolygon".equals(selectedRelations.iterator().next().get("type")) 135 ? selectedRelations.iterator().next() 136 : null; 146 private Relation getSelectedMultipolygonRelation() { 147 return getSelectedMultipolygonRelation(getCurrentDataSet().getSelectedWays(), getCurrentDataSet().getSelectedRelations()); 148 } 149 150 private static Relation getSelectedMultipolygonRelation(Collection<Way> selectedWays, Collection<Relation> selectedRelations) { 151 if (selectedRelations.size() == 1 && "multipolygon".equals(selectedRelations.iterator().next().get("type"))) { 152 return selectedRelations.iterator().next(); 153 } else { 154 final HashSet<Relation> relatedRelations = new HashSet<Relation>(); 155 for (final Way w : selectedWays) { 156 relatedRelations.addAll(Utils.filteredCollection(w.getReferrers(), Relation.class)); 157 } 158 return relatedRelations.size() == 1 ? relatedRelations.iterator().next() : null; 159 } 137 160 } 138 161 … … 140 163 * Returns a {@link Pair} of the old multipolygon {@link Relation} (or null) and the newly created/modified multipolygon {@link Relation}. 141 164 */ 142 public static Pair<Relation, Relation> createMultipolygonRelation(Collection<Way> selectedWays, Collection<Relation> selectedRelations) { 143 144 final Relation selectedMultipolygonRelation = getSelectedMultipolygonRelation(selectedRelations); 145 if (selectedMultipolygonRelation != null) { 146 // add ways of existing relation to include them in polygon analysis 147 selectedWays = new HashSet<Way>(selectedWays); 148 selectedWays.addAll(selectedMultipolygonRelation.getMemberPrimitives(Way.class)); 149 } 165 public static Pair<Relation, Relation> updateMultipolygonRelation(Collection<Way> selectedWays, Relation selectedMultipolygonRelation) { 166 167 // add ways of existing relation to include them in polygon analysis 168 selectedWays = new HashSet<Way>(selectedWays); 169 selectedWays.addAll(selectedMultipolygonRelation.getMemberPrimitives(Way.class)); 150 170 151 171 final MultipolygonCreate polygon = analyzeWays(selectedWays); 152 172 if (polygon == null) { 153 173 return null; //could not make multipolygon. 154 } 155 156 if (selectedMultipolygonRelation != null) { 174 } else { 157 175 return Pair.create(selectedMultipolygonRelation, createRelation(polygon, new Relation(selectedMultipolygonRelation))); 176 } 177 } 178 179 /** 180 * Returns a {@link Pair} null and the newly created/modified multipolygon {@link Relation}. 181 */ 182 public static Pair<Relation, Relation> createMultipolygonRelation(Collection<Way> selectedWays) { 183 184 final MultipolygonCreate polygon = analyzeWays(selectedWays); 185 if (polygon == null) { 186 return null; //could not make multipolygon. 158 187 } else { 159 188 return Pair.create(null, createRelation(polygon, new Relation())); … … 164 193 * Returns a pair of a multipolygon creating/modifying {@link Command} as well as the multipolygon {@link Relation}. 165 194 */ 166 public static Pair<SequenceCommand, Relation> createMultipolygonCommand(Collection<Way> selectedWays, Collection<Relation> selectedRelations) { 167 168 final Pair<Relation, Relation> rr = createMultipolygonRelation(selectedWays, selectedRelations); 195 public static Pair<SequenceCommand, Relation> createMultipolygonCommand(Collection<Way> selectedWays, Relation selectedMultipolygonRelation) { 196 197 final Pair<Relation, Relation> rr = selectedMultipolygonRelation == null 198 ? createMultipolygonRelation(selectedWays) 199 : updateMultipolygonRelation(selectedWays, selectedMultipolygonRelation); 169 200 if (rr == null) { 170 201 return null; … … 177 208 if (existingRelation == null) { 178 209 list.add(new AddCommand(relation)); 179 commandName = tr("Create multipolygon");210 commandName = getName(false); 180 211 } else { 181 212 list.add(new ChangeCommand(existingRelation, relation)); 182 commandName = tr("Update multipolygon");213 commandName = getName(true); 183 214 } 184 215 return Pair.create(new SequenceCommand(commandName, list), relation); … … 200 231 */ 201 232 @Override protected void updateEnabledState(Collection < ? extends OsmPrimitive > selection) { 202 setEnabled(selection != null && !selection.isEmpty());203 putValue(NAME, getSelectedMultipolygonRelation(getCurrentDataSet().getSelectedRelations()) != null204 ? tr("Update multipolygon")205 : tr("Create multipolygon")206 );233 if (update) { 234 setEnabled(getSelectedMultipolygonRelation() != null); 235 } else { 236 setEnabled(!getCurrentDataSet().getSelectedWays().isEmpty()); 237 } 207 238 } 208 239 -
trunk/src/org/openstreetmap/josm/data/validation/tests/MultipolygonTest.java
r6575 r6597 184 184 185 185 // Create new multipolygon using the logics from CreateMultipolygonAction and see if roles match. 186 final Pair<Relation, Relation> newMP = CreateMultipolygonAction.createMultipolygonRelation( 187 r.getMemberPrimitives(Way.class), Collections.singleton(new Relation())); 186 final Pair<Relation, Relation> newMP = CreateMultipolygonAction.createMultipolygonRelation(r.getMemberPrimitives(Way.class)); 188 187 if (newMP != null) { 189 188 for (RelationMember member : r.getMembers()) { -
trunk/src/org/openstreetmap/josm/gui/MainMenu.java
r6544 r6597 254 254 public final JoinAreasAction joinAreas = new JoinAreasAction(); 255 255 /** Tools -> Create multipolygon */ 256 public final CreateMultipolygonAction createMultipolygon = new CreateMultipolygonAction(); 256 public final CreateMultipolygonAction createMultipolygon = new CreateMultipolygonAction(false); 257 /** Tools -> Update multipolygon */ 258 public final CreateMultipolygonAction updateMultipolygon = new CreateMultipolygonAction(true); 257 259 258 260 /* Selection menu */ … … 733 735 add(toolsMenu, joinAreas); 734 736 add(toolsMenu, createMultipolygon); 737 add(toolsMenu, updateMultipolygon); 735 738 736 739 // -- changeset manager toggle action -
trunk/test/unit/org/openstreetmap/josm/actions/CreateMultipolygonActionTest.groovy
r6564 r6597 18 18 } 19 19 20 def getRefToRoleMap(Relation relation) {20 static def getRefToRoleMap(Relation relation) { 21 21 def refToRole = new TreeMap<String, String>() 22 22 for (i in relation.getMembers()) { … … 28 28 void testCreate1() { 29 29 def ds = OsmReader.parseDataSet(new FileInputStream(TestUtils.getTestDataRoot() + "create_multipolygon.osm"), null); 30 def mp = CreateMultipolygonAction.createMultipolygonCommand(ds.getWays(), Collections.emptyList())30 def mp = CreateMultipolygonAction.createMultipolygonCommand(ds.getWays(), null) 31 31 assert mp.a.getDescriptionText() == "Sequence: Create multipolygon" 32 32 assert getRefToRoleMap(mp.b).toString() == "[1:outer, 1.1:inner, 1.1.1:outer, 1.1.2:outer, 1.2:inner]" … … 36 36 def ds = OsmReader.parseDataSet(new FileInputStream(TestUtils.getTestDataRoot() + "create_multipolygon.osm"), null); 37 37 def ways = Utils.filter(ds.getWays(), SearchCompiler.compile("ref=1 OR ref:1.1.", false, false)) 38 def mp = CreateMultipolygonAction.createMultipolygonCommand(ways as Collection<Way>, Collections.emptyList())38 def mp = CreateMultipolygonAction.createMultipolygonCommand(ways as Collection<Way>, null) 39 39 assert getRefToRoleMap(mp.b).toString() == "[1:outer, 1.1.1:inner, 1.1.2:inner]" 40 40 } … … 43 43 def ds = OsmReader.parseDataSet(new FileInputStream(TestUtils.getTestDataRoot() + "create_multipolygon.osm"), null); 44 44 def ways = Utils.filter(ds.getWays(), SearchCompiler.compile("ref=\".*1\$\"", false, true)) 45 def mp = CreateMultipolygonAction.createMultipolygonCommand(ways as Collection<Way>, Collections.emptyList())45 def mp = CreateMultipolygonAction.createMultipolygonCommand(ways as Collection<Way>, null) 46 46 assert mp.b.getMembersCount() == 3 47 47 assert getRefToRoleMap(mp.b).toString() == "[1:outer, 1.1:inner, 1.1.1:outer]" 48 48 def ways2 = Utils.filter(ds.getWays(), SearchCompiler.compile("ref=1.2", false, true)) 49 def mp2 = CreateMultipolygonAction.createMultipolygonCommand(ways2 as Collection<Way>, Collections.singleton(mp.b))49 def mp2 = CreateMultipolygonAction.createMultipolygonCommand(ways2 as Collection<Way>, mp.b) 50 50 assert mp2.b.getMembersCount() == 4 51 51 assert getRefToRoleMap(mp2.b).toString() == "[1:outer, 1.1:inner, 1.1.1:outer, 1.2:inner]" … … 55 55 def ds = OsmReader.parseDataSet(new FileInputStream(TestUtils.getTestDataRoot() + "create_multipolygon.osm"), null); 56 56 def ways = Utils.filter(ds.getWays(), SearchCompiler.compile("ref=1 OR ref:1.1.1", false, false)) 57 def mp = CreateMultipolygonAction.createMultipolygonCommand(ways as Collection<Way>, Collections.emptyList())57 def mp = CreateMultipolygonAction.createMultipolygonCommand(ways as Collection<Way>, null) 58 58 assert getRefToRoleMap(mp.b).toString() == "[1:outer, 1.1.1:inner]" 59 59 def ways2 = Utils.filter(ds.getWays(), SearchCompiler.compile("ref=1.1 OR ref=1.2 OR ref=1.1.2", false, true)) 60 def mp2 = CreateMultipolygonAction.createMultipolygonCommand(ways2 as Collection<Way>, Collections.singleton(mp.b))60 def mp2 = CreateMultipolygonAction.createMultipolygonCommand(ways2 as Collection<Way>, mp.b) 61 61 assert getRefToRoleMap(mp2.b).toString() == "[1:outer, 1.1:inner, 1.1.1:outer, 1.1.2:outer, 1.2:inner]" 62 62 }
Note:
See TracChangeset
for help on using the changeset viewer.