1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.relation;
|
---|
3 |
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.List;
|
---|
6 | import java.util.Objects;
|
---|
7 |
|
---|
8 | import javax.swing.AbstractListModel;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
11 | import org.openstreetmap.josm.data.osm.DownloadPolicy;
|
---|
12 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
13 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * This is the model for the {@link ReferringRelationsBrowser}.
|
---|
17 | * <p>
|
---|
18 | * It holds all referrers to a given relation
|
---|
19 | */
|
---|
20 | public class ReferringRelationsBrowserModel extends AbstractListModel<Relation> {
|
---|
21 |
|
---|
22 | /** the relation */
|
---|
23 | private transient Relation relation;
|
---|
24 | private final transient List<Relation> referrers = new ArrayList<>();
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Constructs a new {@code ReferringRelationsBrowserModel}.
|
---|
28 | * @param relation relation
|
---|
29 | */
|
---|
30 | public ReferringRelationsBrowserModel(Relation relation) {
|
---|
31 | this.relation = relation;
|
---|
32 | }
|
---|
33 |
|
---|
34 | protected void fireModelUpdate() {
|
---|
35 | int upper = Math.max(0, referrers.size() -1);
|
---|
36 | fireContentsChanged(this, 0, upper);
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Sets the relation.
|
---|
41 | * @param relation the relation
|
---|
42 | */
|
---|
43 | public void setRelation(Relation relation) {
|
---|
44 | this.relation = relation;
|
---|
45 | referrers.clear();
|
---|
46 | fireModelUpdate();
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | public Relation getElementAt(int index) {
|
---|
51 | return referrers.get(index);
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public int getSize() {
|
---|
56 | return referrers.size();
|
---|
57 | }
|
---|
58 |
|
---|
59 | protected boolean isReferringRelation(Relation parent) {
|
---|
60 | return parent != null && parent.getMembers().stream()
|
---|
61 | .filter(RelationMember::isRelation)
|
---|
62 | .map(RelationMember::getRelation)
|
---|
63 | .anyMatch(child -> Objects.equals(child, relation));
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Populates the model with parent referrers.
|
---|
68 | * @param parents parent referrers
|
---|
69 | */
|
---|
70 | public void populate(List<Relation> parents) {
|
---|
71 | referrers.clear();
|
---|
72 | if (parents != null) {
|
---|
73 | for (Relation relation: parents) {
|
---|
74 | if (isReferringRelation(relation)) {
|
---|
75 | referrers.add(relation);
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 | fireModelUpdate();
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Populates the browser with the list of referring relations in the {@link DataSet} ds.
|
---|
84 | *
|
---|
85 | * @param ds the data set
|
---|
86 | */
|
---|
87 | public void populate(DataSet ds) {
|
---|
88 | referrers.clear();
|
---|
89 | if (ds == null) {
|
---|
90 | fireModelUpdate();
|
---|
91 | return;
|
---|
92 | }
|
---|
93 | for (Relation parent : ds.getRelations()) {
|
---|
94 | if (isReferringRelation(parent)) {
|
---|
95 | referrers.add(parent);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | fireModelUpdate();
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Determines if reloading the relation is possible/authorized.
|
---|
103 | * @return {@code true} if reloading the relation is possible/authorized
|
---|
104 | */
|
---|
105 | public boolean canReload() {
|
---|
106 | return relation != null && !relation.isNew()
|
---|
107 | && !relation.getDataSet().isLocked()
|
---|
108 | && DownloadPolicy.BLOCKED != relation.getDataSet().getDownloadPolicy();
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Returns the relation.
|
---|
113 | * @return the relation
|
---|
114 | */
|
---|
115 | public Relation getRelation() {
|
---|
116 | return relation;
|
---|
117 | }
|
---|
118 | }
|
---|