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