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.Relation;
|
---|
11 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
12 |
|
---|
13 | public class ReferringRelationsBrowserModel extends AbstractListModel {
|
---|
14 |
|
---|
15 | /** the relation */
|
---|
16 | private Relation relation;
|
---|
17 | private ArrayList<Relation> referrers;
|
---|
18 |
|
---|
19 | public ReferringRelationsBrowserModel() {
|
---|
20 | relation = null;
|
---|
21 | referrers = new ArrayList<Relation>();
|
---|
22 | }
|
---|
23 | public ReferringRelationsBrowserModel(Relation relation) {
|
---|
24 | this();
|
---|
25 | this.relation = relation;
|
---|
26 | }
|
---|
27 |
|
---|
28 | protected void fireModelUpdate() {
|
---|
29 | int upper = Math.max(0, referrers.size() -1);
|
---|
30 | fireContentsChanged(this, 0, upper);
|
---|
31 | }
|
---|
32 |
|
---|
33 | public void setRelation(Relation relation) {
|
---|
34 | this.relation = relation;
|
---|
35 | referrers.clear();
|
---|
36 | fireModelUpdate();
|
---|
37 | }
|
---|
38 |
|
---|
39 | public Object getElementAt(int index) {
|
---|
40 | return referrers.get(index);
|
---|
41 | }
|
---|
42 |
|
---|
43 | public int getSize() {
|
---|
44 | return referrers.size();
|
---|
45 | }
|
---|
46 |
|
---|
47 | protected boolean isReferringRelation(Relation parent) {
|
---|
48 | if (parent == null) return false;
|
---|
49 | for (RelationMember m: parent.getMembers()) {
|
---|
50 | if (m.isRelation()) {
|
---|
51 | Relation child = m.getRelation();
|
---|
52 | if (child.equals(relation)) return true;
|
---|
53 | }
|
---|
54 | }
|
---|
55 | return false;
|
---|
56 | }
|
---|
57 |
|
---|
58 | public void populate(List<Relation> parents) {
|
---|
59 | referrers.clear();
|
---|
60 | if (parents != null) {
|
---|
61 | for (Relation relation: parents) {
|
---|
62 | if (isReferringRelation(relation)) {
|
---|
63 | referrers.add(relation);
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|
67 | fireModelUpdate();
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Populates the browser with the list of referring relations in the {@see DataSet} ds.
|
---|
72 | *
|
---|
73 | * @param ds the data set
|
---|
74 | */
|
---|
75 | public void populate(DataSet ds) {
|
---|
76 | referrers.clear();
|
---|
77 | if (ds == null) {
|
---|
78 | fireModelUpdate();
|
---|
79 | return;
|
---|
80 | }
|
---|
81 | for (Relation parent : ds.getRelations()) {
|
---|
82 | if (isReferringRelation(parent)) {
|
---|
83 | referrers.add(parent);
|
---|
84 | }
|
---|
85 | }
|
---|
86 | fireModelUpdate();
|
---|
87 | }
|
---|
88 |
|
---|
89 | public boolean canReload() {
|
---|
90 | return relation != null && !relation.isNew();
|
---|
91 | }
|
---|
92 |
|
---|
93 | public Relation getRelation() {
|
---|
94 | return relation;
|
---|
95 | }
|
---|
96 |
|
---|
97 | public Relation get(int index) {
|
---|
98 | return referrers.get(index);
|
---|
99 | }
|
---|
100 | }
|
---|