Ignore:
Timestamp:
2024-03-03T10:25:25+01:00 (9 months ago)
Author:
GerdP
Message:

fix #23521: fix some memory leaks

  • dispose dialogs
  • either avoid to create clones of ways or relations or use setNodes(null) / setMembers(null)
  • replaces most ChangeCommand instances by better specialized alternatives
  • add some comments
  • fix some checkstyle / sonar issues
File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/reltoolbox/src/relcontext/actions/ReconstructPolygonAction.java

    r36142 r36217  
    88import java.util.ArrayList;
    99import java.util.Arrays;
     10import java.util.Collection;
    1011import java.util.Collections;
    1112import java.util.HashSet;
     
    1920import org.openstreetmap.josm.command.AddCommand;
    2021import org.openstreetmap.josm.command.ChangeCommand;
     22import org.openstreetmap.josm.command.ChangeMembersCommand;
    2123import org.openstreetmap.josm.command.Command;
    2224import org.openstreetmap.josm.command.DeleteCommand;
     
    4345 */
    4446public class ReconstructPolygonAction extends JosmAction implements ChosenRelationListener {
    45     private final ChosenRelation rel;
     47    private final transient ChosenRelation rel;
    4648
    4749    private static final List<String> IRRELEVANT_KEYS = Arrays.asList("source", "created_by", "note");
    4850
     51    /**
     52     * Reconstruct one or more polygons from multipolygon relation.
     53     * @param rel the multipolygon relation
     54     */
    4955    public ReconstructPolygonAction(ChosenRelation rel) {
    5056        super(tr("Reconstruct polygon"), "dialogs/filter", tr("Reconstruct polygon from multipolygon relation"),
    5157                Shortcut.registerShortcut("reltoolbox:reconstructpoly", tr("Relation Toolbox: {0}",
    5258                        tr("Reconstruct polygon from multipolygon relation")),
    53                         KeyEvent.CHAR_UNDEFINED, Shortcut.NONE), false);
     59                        KeyEvent.CHAR_UNDEFINED, Shortcut.NONE), false, false);
    5460        this.rel = rel;
    5561        rel.addChosenRelationListener(this);
     
    7278        if (wont) {
    7379            JOptionPane.showMessageDialog(MainApplication.getMainFrame(),
    74                     tr("Multipolygon must consist only of ways with one referring relation"),
     80                    tr("Multipolygon must consist only of ways"),
    7581                    tr("Reconstruct polygon"), JOptionPane.ERROR_MESSAGE);
    7682            return;
     
    8591
    8692        rel.clear();
    87         List<OsmPrimitive> newSelection = new ArrayList<>();
    8893        List<Command> commands = new ArrayList<>();
    8994        Command relationDeleteCommand = DeleteCommand.delete(Collections.singleton(r), true, true);
     
    108113                // this ring has inner rings, so we leave a multipolygon in
    109114                // place and don't reconstruct the rings.
     115                List<RelationMember> members = new ArrayList<>();
    110116                Relation n;
     117                for (Way w : p.ways) {
     118                    members.add(new RelationMember("outer", w));
     119                }
     120                for (JoinedPolygon i : myInnerWays) {
     121                    for (Way w : i.ways) {
     122                        members.add(new RelationMember("inner", w));
     123                    }
     124                }
    111125                if (relationReused) {
    112126                    n = new Relation();
    113127                    n.setKeys(r.getKeys());
    114                 } else {
    115                     n = new Relation(r);
    116                     n.setMembers(null);
    117                 }
    118                 for (Way w : p.ways) {
    119                     n.addMember(new RelationMember("outer", w));
    120                 }
    121                 for (JoinedPolygon i : myInnerWays) {
    122                     for (Way w : i.ways) {
    123                         n.addMember(new RelationMember("inner", w));
    124                     }
    125                 }
    126                 if (relationReused) {
     128                    n.setMembers(members);
    127129                    commands.add(new AddCommand(ds, n));
    128130                } else {
    129131                    relationReused = true;
    130                     commands.add(new ChangeCommand(r, n));
    131                 }
    132                 newSelection.add(n);
     132                    commands.add(new ChangeMembersCommand(r, members));
     133                }
    133134                continue;
    134135            }
     
    186187            result.addNode(result.firstNode());
    187188            result.setKeys(tags);
    188             newSelection.add(candidateWay == null ? result : candidateWay);
    189189            commands.add(candidateWay == null ? new AddCommand(ds, result) : new ChangeCommand(candidateWay, result));
    190190        }
     
    195195            commands.add(0, relationDeleteCommand);
    196196        }
    197 
    198197        UndoRedoHandler.getInstance().add(new SequenceCommand(tr("Reconstruct polygons from relation {0}",
    199198                r.getDisplayName(DefaultNameFormatter.getInstance())), commands));
     199        Collection<? extends OsmPrimitive> newSelection = UndoRedoHandler.getInstance().getLastCommand().getParticipatingPrimitives();
     200        newSelection.removeIf(p -> p.isDeleted());
    200201        ds.setSelected(newSelection);
    201202    }
Note: See TracChangeset for help on using the changeset viewer.