Changeset 6597 in josm


Ignore:
Timestamp:
2014-01-02T22:45:23+01:00 (7 years ago)
Author:
simon04
Message:

see #9492 - split "Create Multipolyon" in % and "Update Multipolygon"; for the latter obtain the relation from the selected ways (if unambiguous)

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/CreateMultipolygonAction.java

    r6575 r6597  
    3636import org.openstreetmap.josm.tools.Pair;
    3737import org.openstreetmap.josm.tools.Shortcut;
     38import org.openstreetmap.josm.tools.Utils;
    3839
    3940/**
     
    5455public class CreateMultipolygonAction extends JosmAction {
    5556
     57    private final boolean update;
     58
    5659    /**
    5760     * Constructs a new {@code CreateMultipolygonAction}.
    5861     */
    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
    6475    /**
    6576     * The action button has been clicked
     
    92103        }
    93104
    94         final Relation multipolygonRelation = getSelectedMultipolygonRelation(selectedRelations);
     105        final Relation multipolygonRelation = update
     106                ? getSelectedMultipolygonRelation(selectedWays, selectedRelations)
     107                : null;
    95108        if (multipolygonRelation != null && (multipolygonRelation.isIncomplete() || multipolygonRelation.hasIncompleteMembers())) {
    96109            new Notification(
     
    102115        }
    103116
    104         final Pair<SequenceCommand, Relation> commandAndRelation = createMultipolygonCommand(selectedWays, selectedRelations);
     117        final Pair<SequenceCommand, Relation> commandAndRelation = createMultipolygonCommand(selectedWays, multipolygonRelation);
    105118        if (commandAndRelation == null) {
    106119            return;
     
    131144    }
    132145
    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        }
    137160    }
    138161
     
    140163     * Returns a {@link Pair} of the old multipolygon {@link Relation} (or null) and the newly created/modified multipolygon {@link Relation}.
    141164     */
    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));
    150170
    151171        final MultipolygonCreate polygon = analyzeWays(selectedWays);
    152172        if (polygon == null) {
    153173            return null; //could not make multipolygon.
    154         }
    155 
    156         if (selectedMultipolygonRelation != null) {
     174        } else {
    157175            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.
    158187        } else {
    159188            return Pair.create(null, createRelation(polygon, new Relation()));
     
    164193     * Returns a pair of a multipolygon creating/modifying {@link Command} as well as the multipolygon {@link Relation}.
    165194     */
    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);
    169200        if (rr == null) {
    170201            return null;
     
    177208        if (existingRelation == null) {
    178209            list.add(new AddCommand(relation));
    179             commandName = tr("Create multipolygon");
     210            commandName = getName(false);
    180211        } else {
    181212            list.add(new ChangeCommand(existingRelation, relation));
    182             commandName = tr("Update multipolygon");
     213            commandName = getName(true);
    183214        }
    184215        return Pair.create(new SequenceCommand(commandName, list), relation);
     
    200231      */
    201232    @Override protected void updateEnabledState(Collection < ? extends OsmPrimitive > selection) {
    202         setEnabled(selection != null && !selection.isEmpty());
    203         putValue(NAME, getSelectedMultipolygonRelation(getCurrentDataSet().getSelectedRelations()) != null
    204                 ? tr("Update multipolygon")
    205                 : tr("Create multipolygon")
    206         );
     233        if (update) {
     234            setEnabled(getSelectedMultipolygonRelation() != null);
     235        } else {
     236            setEnabled(!getCurrentDataSet().getSelectedWays().isEmpty());
     237        }
    207238    }
    208239
  • trunk/src/org/openstreetmap/josm/data/validation/tests/MultipolygonTest.java

    r6575 r6597  
    184184
    185185            // 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));
    188187            if (newMP != null) {
    189188                for (RelationMember member : r.getMembers()) {
  • trunk/src/org/openstreetmap/josm/gui/MainMenu.java

    r6544 r6597  
    254254    public final JoinAreasAction joinAreas = new JoinAreasAction();
    255255    /** 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);
    257259
    258260    /* Selection menu */
     
    733735        add(toolsMenu, joinAreas);
    734736        add(toolsMenu, createMultipolygon);
     737        add(toolsMenu, updateMultipolygon);
    735738
    736739        // -- changeset manager toggle action
  • trunk/test/unit/org/openstreetmap/josm/actions/CreateMultipolygonActionTest.groovy

    r6564 r6597  
    1818    }
    1919
    20     def getRefToRoleMap(Relation relation) {
     20    static def getRefToRoleMap(Relation relation) {
    2121        def refToRole = new TreeMap<String, String>()
    2222        for (i in relation.getMembers()) {
     
    2828    void testCreate1() {
    2929        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)
    3131        assert mp.a.getDescriptionText() == "Sequence: Create multipolygon"
    3232        assert getRefToRoleMap(mp.b).toString() == "[1:outer, 1.1:inner, 1.1.1:outer, 1.1.2:outer, 1.2:inner]"
     
    3636        def ds = OsmReader.parseDataSet(new FileInputStream(TestUtils.getTestDataRoot() + "create_multipolygon.osm"), null);
    3737        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)
    3939        assert getRefToRoleMap(mp.b).toString() == "[1:outer, 1.1.1:inner, 1.1.2:inner]"
    4040    }
     
    4343        def ds = OsmReader.parseDataSet(new FileInputStream(TestUtils.getTestDataRoot() + "create_multipolygon.osm"), null);
    4444        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)
    4646        assert mp.b.getMembersCount() == 3
    4747        assert getRefToRoleMap(mp.b).toString() == "[1:outer, 1.1:inner, 1.1.1:outer]"
    4848        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)
    5050        assert mp2.b.getMembersCount() == 4
    5151        assert getRefToRoleMap(mp2.b).toString() == "[1:outer, 1.1:inner, 1.1.1:outer, 1.2:inner]"
     
    5555        def ds = OsmReader.parseDataSet(new FileInputStream(TestUtils.getTestDataRoot() + "create_multipolygon.osm"), null);
    5656        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)
    5858        assert getRefToRoleMap(mp.b).toString() == "[1:outer, 1.1.1:inner]"
    5959        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)
    6161        assert getRefToRoleMap(mp2.b).toString() == "[1:outer, 1.1:inner, 1.1.1:outer, 1.1.2:outer, 1.2:inner]"
    6262    }
Note: See TracChangeset for help on using the changeset viewer.