1
2
3
4
5
6
7
8
9
10
11
12 package sk.uniba.euromath.editor.xmlEditor;
13
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Set;
20
21 /***
22 * Holds relation between node's ids from source XML document to EditParts which
23 * visualize them. One id can be visualized by one EditPart, many or by none.
24 *
25 * @author Tomáš Studva 4.11.2005
26 */
27 public class IdVisualInfo {
28
29 /***
30 * Ids currently in map - not only element's ids as elementIDs.
31 */
32 protected final Set<String> idSet;
33
34 /***
35 * Ids of elements currently in map.
36 */
37 private final Set<String> elementIDs;
38
39 /***
40 * Maps id to EditParts visually representing node with given id.
41 */
42 protected final Map<String, Set<IXMLEditPart>> parts;
43
44 /***
45 * Conatructor.
46 */
47 public IdVisualInfo() {
48 super();
49 this.idSet = new HashSet<String>();
50 this.parts = new HashMap<String, Set<IXMLEditPart>>();
51 this.elementIDs = new HashSet<String>();
52 }
53
54 /***
55 * Adds vusualPart to set of visualizers of xml node with id
56 * <code>id</code>.
57 *
58 * @param id
59 * Unique identifier of a node from XML data source, not
60 * null
61 * @param visualPart
62 * An XMLEditPart visually representing node with given
63 * id, not null
64 */
65 public void add(String id, IXMLEditPart visualPart) {
66 if (id == null) {
67 throw new IllegalArgumentException("Id cannot be null.");
68 }
69 if (visualPart == null) {
70 throw new IllegalArgumentException(
71 "VisualPart cannot be null.");
72 }
73
74 if (!getIdSet().contains(id)) {
75 addNewID(id);
76 }
77
78 getParts().get(id).add(visualPart);
79 }
80
81 /***
82 * Adds id to this info. Helper method.
83 *
84 * @param id
85 * not null and not contained by info
86 */
87 private void addNewID(String id) {
88 getIdSet().add(id);
89 getParts().put(id, new HashSet<IXMLEditPart>());
90 if ((id.indexOf(';') == -1) && (id.indexOf('@') == -1)) {
91 getElementIDs().add(id);
92 }
93 }
94
95 /***
96 * Clears out holded info. Object state is same as after construction.
97 */
98 public void clear() {
99 getIdSet().clear();
100 getParts().clear();
101 getElementIDs().clear();
102 }
103
104 /***
105 * Returns true if holds some info about desired id.
106 *
107 * @param id
108 * of node not null
109 * @return true if contains the id
110 */
111 public boolean contains(String id) {
112 if (id == null) {
113 throw new IllegalArgumentException("Id cannot be null.");
114 }
115 return getIdSet().contains(id);
116 }
117
118 /***
119 * Returns how many EditParts represent id (this information is taken
120 * from object's state). If id was not yet added, zero is returned.
121 *
122 * @param id
123 * of node not null
124 * @return how many EditParts represent id
125 */
126 public int count(String id) {
127 if (id == null) {
128 throw new IllegalArgumentException("Id cannot be null.");
129 }
130 if (contains(id)) {
131 return getVisualParts(id).size();
132 }
133 return 0;
134 }
135
136 /***
137 * Returns list of XMLEditParts which visualize node with id
138 * <code>id</code>.
139 *
140 * @param id
141 * of xml node, not null
142 * @return EditParts representing id in new instance of ArrayList
143 */
144 public List<IXMLEditPart> getVisualParts(String id) {
145 if (id == null) {
146 throw new IllegalArgumentException("Id cannot be null.");
147 }
148
149 if (!contains(id)) {
150 return new ArrayList<IXMLEditPart>();
151 }
152
153 return new ArrayList<IXMLEditPart>(getParts().get(id));
154 }
155
156 /***
157 * Returns list of XMLEditParts which visualize nodes with ids.
158 *
159 * @param id
160 * of xml node, not null
161 * @return EditParts representing ids in new instance of Set
162 */
163 public Set<IXMLEditPart> getVisualParts(Set<String> ids) {
164 if (ids == null) {
165 throw new IllegalArgumentException(
166 "Ids cannot be null.");
167 }
168 Set<IXMLEditPart> result = new HashSet<IXMLEditPart>();
169
170 for (String id : ids) {
171 if (contains(id)) {
172 result.addAll(getParts().get(id));
173 }
174 }
175
176 return result;
177 }
178
179 /***
180 * Returns set containing all element ids holded by info.
181 *
182 * @return elements ids set by copy
183 */
184 public Set<String> getElementIds() {
185 return new HashSet<String>(this.elementIDs);
186 }
187
188 /***
189 * Returns set containing all ids holded by info.
190 *
191 * @return ids set by copy
192 */
193 public Set<String> getIds() {
194 return new HashSet<String>(this.idSet);
195 }
196
197 /***
198 * @return Returns the elementIDs.
199 */
200 protected Set<String> getElementIDs() {
201 return this.elementIDs;
202 }
203
204 /***
205 * @return Returns the idSet.
206 */
207 protected Set<String> getIdSet() {
208 return this.idSet;
209 }
210
211 /***
212 * @return Returns the parts.
213 */
214 protected Map<String, Set<IXMLEditPart>> getParts() {
215 return this.parts;
216 }
217 }