source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/relation/RelationTreeModel.java@ 16438

Last change on this file since 16438 was 16438, checked in by simon04, 4 years ago

see #19251 - Java 8: use Stream

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