1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.relation;
|
---|
3 |
|
---|
4 | import java.util.List;
|
---|
5 | import java.util.concurrent.CopyOnWriteArrayList;
|
---|
6 |
|
---|
7 | import javax.swing.event.TreeModelEvent;
|
---|
8 | import javax.swing.event.TreeModelListener;
|
---|
9 | import javax.swing.tree.TreeModel;
|
---|
10 | import javax.swing.tree.TreePath;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
13 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * This is a {@link TreeModel} which provides the hierarchical structure of {@link Relation}s
|
---|
17 | * to a {@link javax.swing.tree.JTree}.
|
---|
18 | *
|
---|
19 | * The model is initialized with a root relation or with a list of {@link RelationMember}s, see
|
---|
20 | * {@link #populate(Relation)} and {@link #populate(List)} respectively.
|
---|
21 | *
|
---|
22 | *
|
---|
23 | */
|
---|
24 | public class RelationTreeModel implements TreeModel {
|
---|
25 | /** the root relation */
|
---|
26 | private Relation root;
|
---|
27 |
|
---|
28 | /** the tree model listeners */
|
---|
29 | private CopyOnWriteArrayList<TreeModelListener> listeners;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * constructor
|
---|
33 | */
|
---|
34 | public RelationTreeModel() {
|
---|
35 | this.root = null;
|
---|
36 | listeners = new CopyOnWriteArrayList<TreeModelListener>();
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * constructor
|
---|
41 | * @param root the root relation
|
---|
42 | */
|
---|
43 | public RelationTreeModel(Relation root) {
|
---|
44 | this.root = root;
|
---|
45 | listeners = new CopyOnWriteArrayList<TreeModelListener>();
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * constructor
|
---|
50 | *
|
---|
51 | * @param members a list of members
|
---|
52 | */
|
---|
53 | public RelationTreeModel(List<RelationMember> members) {
|
---|
54 | if (members == null) return;
|
---|
55 | Relation root = new Relation();
|
---|
56 | root.setMembers(members);
|
---|
57 | this.root = root;
|
---|
58 | listeners = new CopyOnWriteArrayList<TreeModelListener>();
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Replies the number of children of type relation for a particular
|
---|
63 | * relation <code>parent</code>
|
---|
64 | *
|
---|
65 | * @param parent the parent relation
|
---|
66 | * @return the number of children of type relation
|
---|
67 | */
|
---|
68 | protected int getNumRelationChildren(Relation parent) {
|
---|
69 | if (parent == null) return 0;
|
---|
70 | int count = 0;
|
---|
71 | for(RelationMember member : parent.getMembers()) {
|
---|
72 | if (member.isRelation()) {
|
---|
73 | count++;
|
---|
74 | }
|
---|
75 | }
|
---|
76 | return count;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Replies the i-th child of type relation for a particular relation
|
---|
81 | * <code>parent</code>.
|
---|
82 | *
|
---|
83 | * @param parent the parent relation
|
---|
84 | * @param idx the index
|
---|
85 | * @return the i-th child of type relation for a particular relation
|
---|
86 | * <code>parent</code>; null, if no such child exists
|
---|
87 | */
|
---|
88 | protected Relation getRelationChildByIdx(Relation parent, int idx) {
|
---|
89 | if (parent == null) return null;
|
---|
90 | int count=0;
|
---|
91 | for (RelationMember member : parent.getMembers()) {
|
---|
92 | if (!(member.isRelation())) {
|
---|
93 | continue;
|
---|
94 | }
|
---|
95 | if (count == idx)
|
---|
96 | return member.getRelation();
|
---|
97 | count++;
|
---|
98 | }
|
---|
99 | return null;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Replies the index of a particular <code>child</code> with respect to its
|
---|
104 | * <code>parent</code>.
|
---|
105 | *
|
---|
106 | * @param parent the parent relation
|
---|
107 | * @param child the child relation
|
---|
108 | * @return the index of a particular <code>child</code> with respect to its
|
---|
109 | * <code>parent</code>; -1 if either parent or child are null or if <code>child</code>
|
---|
110 | * isn't a child of <code>parent</code>.
|
---|
111 | *
|
---|
112 | */
|
---|
113 | protected int getIndexForRelationChild(Relation parent, Relation child) {
|
---|
114 | if (parent == null || child == null) return -1;
|
---|
115 | int idx = 0;
|
---|
116 | for (RelationMember member : parent.getMembers()) {
|
---|
117 | if (!(member.isRelation())) {
|
---|
118 | continue;
|
---|
119 | }
|
---|
120 | if (member.getMember() == child) return idx;
|
---|
121 | idx++;
|
---|
122 | }
|
---|
123 | return -1;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Populates the model with a root relation
|
---|
128 | *
|
---|
129 | * @param root the root relation
|
---|
130 | * @see #populate(List)
|
---|
131 | *
|
---|
132 | */
|
---|
133 | public void populate(Relation root) {
|
---|
134 | if (root == null) {
|
---|
135 | root = new Relation();
|
---|
136 | }
|
---|
137 | this.root = root;
|
---|
138 | fireRootReplacedEvent();
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Populates the model with a list of relation members
|
---|
143 | *
|
---|
144 | * @param members the relation members
|
---|
145 | */
|
---|
146 | public void populate(List<RelationMember> members) {
|
---|
147 | if (members == null) return;
|
---|
148 | Relation r = new Relation();
|
---|
149 | r.setMembers(members);
|
---|
150 | this.root = r;
|
---|
151 | fireRootReplacedEvent();
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Notifies tree model listeners about a replacement of the
|
---|
156 | * root.
|
---|
157 | */
|
---|
158 | protected void fireRootReplacedEvent() {
|
---|
159 | TreeModelEvent e = new TreeModelEvent(this, new TreePath(root));
|
---|
160 | for (TreeModelListener l : listeners) {
|
---|
161 | l.treeStructureChanged(e);
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Notifies tree model listeners about an update of the
|
---|
167 | * trees nodes.
|
---|
168 | *
|
---|
169 | * @param path the tree path to the node
|
---|
170 | */
|
---|
171 | protected void fireRefreshNode(TreePath path) {
|
---|
172 | TreeModelEvent e = new TreeModelEvent(this, path);
|
---|
173 | for (TreeModelListener l : listeners) {
|
---|
174 | l.treeStructureChanged(e);
|
---|
175 | }
|
---|
176 |
|
---|
177 | }
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Invoke to notify all listeners about an update of a particular node
|
---|
181 | *
|
---|
182 | * @param pathToNode the tree path to the node
|
---|
183 | */
|
---|
184 | public void refreshNode(TreePath pathToNode) {
|
---|
185 | fireRefreshNode(pathToNode);
|
---|
186 | }
|
---|
187 |
|
---|
188 | /* ----------------------------------------------------------------------- */
|
---|
189 | /* interface TreeModel */
|
---|
190 | /* ----------------------------------------------------------------------- */
|
---|
191 | public Object getChild(Object parent, int index) {
|
---|
192 | return getRelationChildByIdx((Relation)parent, index);
|
---|
193 | }
|
---|
194 |
|
---|
195 | public int getChildCount(Object parent) {
|
---|
196 | return getNumRelationChildren((Relation)parent);
|
---|
197 | }
|
---|
198 |
|
---|
199 | public int getIndexOfChild(Object parent, Object child) {
|
---|
200 | return getIndexForRelationChild((Relation)parent, (Relation)child);
|
---|
201 | }
|
---|
202 |
|
---|
203 | public Object getRoot() {
|
---|
204 | return root;
|
---|
205 | }
|
---|
206 |
|
---|
207 | public boolean isLeaf(Object node) {
|
---|
208 | Relation r = (Relation)node;
|
---|
209 | if (r.isIncomplete()) return false;
|
---|
210 | return getNumRelationChildren(r) == 0;
|
---|
211 | }
|
---|
212 |
|
---|
213 | public void addTreeModelListener(TreeModelListener l) {
|
---|
214 | if (l != null) {
|
---|
215 | listeners.addIfAbsent(l);
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | public void removeTreeModelListener(TreeModelListener l) {
|
---|
220 | listeners.remove(l);
|
---|
221 | }
|
---|
222 |
|
---|
223 | public void valueForPathChanged(TreePath path, Object newValue) {
|
---|
224 | // do nothing
|
---|
225 | }
|
---|
226 | }
|
---|