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