source: josm/trunk/src/org/openstreetmap/josm/actions/relation/AbstractRelationAction.java@ 17458

Last change on this file since 17458 was 17458, checked in by GerdP, 3 years ago

see #17184: Memory leaks

  • clear collection rememberMovements in OrthogonalizeAction when no edit layer exits
  • clear refs to layers which may disturb GC, maybe because of the complex cyclic deps
  • Property svn:eol-style set to native
File size: 2.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions.relation;
3
4import java.util.Collection;
5import java.util.Collections;
6
7import javax.swing.AbstractAction;
8
9import org.openstreetmap.josm.actions.IPrimitiveAction;
10import org.openstreetmap.josm.data.osm.DownloadPolicy;
11import org.openstreetmap.josm.data.osm.IPrimitive;
12import org.openstreetmap.josm.data.osm.IRelation;
13import org.openstreetmap.josm.data.osm.OsmData;
14import org.openstreetmap.josm.data.osm.OsmUtils;
15import org.openstreetmap.josm.data.osm.Relation;
16import org.openstreetmap.josm.io.NetworkManager;
17import org.openstreetmap.josm.io.OnlineResource;
18import org.openstreetmap.josm.tools.Destroyable;
19import org.openstreetmap.josm.tools.SubclassFilteredCollection;
20import org.openstreetmap.josm.tools.Utils;
21
22/**
23 * Ancestor for all actions that want to work with relation collection and
24 * to be disabled if the collection is empty
25 * @since 5793
26 * @since 13957 (signature)
27 */
28public abstract class AbstractRelationAction extends AbstractAction implements IPrimitiveAction, Destroyable {
29 /** relation collection */
30 protected transient Collection<IRelation<?>> relations = Collections.<IRelation<?>>emptySet();
31
32 /**
33 * Returns the relations contained in the given collection.
34 * @param primitives collection of primitives
35 * @return the relation contained in {@code primitives}
36 */
37 protected static final Collection<IRelation<?>> getRelations(Collection<? extends IPrimitive> primitives) {
38 if (primitives == null || primitives.isEmpty()) {
39 return Collections.<IRelation<?>>emptySet();
40 } else {
41 return new SubclassFilteredCollection<>(primitives, IRelation.class::isInstance);
42 }
43 }
44
45 @Override
46 public void setPrimitives(Collection<? extends IPrimitive> primitives) {
47 this.relations = getRelations(primitives);
48 updateEnabledState();
49 }
50
51 /**
52 * Override in subclasses to update the enabled state of the action when something changes.
53 */
54 protected void updateEnabledState() {
55 setEnabled(!relations.isEmpty());
56 }
57
58 protected final boolean canModify() {
59 SubclassFilteredCollection<IRelation<?>, Relation> filteredRelations = Utils.filteredCollection(relations, Relation.class);
60 return OsmUtils.isOsmCollectionEditable(filteredRelations) && filteredRelations.parallelStream().anyMatch(r -> !r.isDeleted());
61 }
62
63 protected final boolean canDownload() {
64 if (relations.isEmpty()) {
65 return false;
66 }
67 OsmData<?, ?, ?, ?> ds = relations.iterator().next().getDataSet();
68 return !NetworkManager.isOffline(OnlineResource.OSM_API)
69 && ds != null && !ds.isLocked() && DownloadPolicy.BLOCKED != ds.getDownloadPolicy();
70 }
71
72 protected void setHelpId(String helpId) {
73 putValue("help", helpId);
74 }
75
76 @Override
77 public void destroy() {
78 relations = null;
79 }
80}
Note: See TracBrowser for help on using the repository browser.