1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.dishevelled.piccolo.venn;
25
26 import java.util.Arrays;
27 import java.util.List;
28 import java.util.Set;
29
30 import org.piccolo2d.nodes.PText;
31 import org.dishevelled.observable.event.SetChangeEvent;
32 import org.dishevelled.observable.event.SetChangeListener;
33 import org.dishevelled.venn.BinaryVennModel;
34 import org.dishevelled.venn.model.BinaryVennModelImpl;
35
36
37
38
39
40
41
42
43 public abstract class AbstractBinaryVennNode<E>
44 extends AbstractVennNode<E>
45 {
46
47 private BinaryVennModel<E> model;
48
49
50 private String firstLabelText = DEFAULT_FIRST_LABEL_TEXT;
51
52
53 private String secondLabelText = DEFAULT_SECOND_LABEL_TEXT;
54
55
56 private String firstOnlyLabelText = DEFAULT_FIRST_ONLY_LABEL_TEXT;
57
58
59 private String secondOnlyLabelText = DEFAULT_SECOND_ONLY_LABEL_TEXT;
60
61
62 private String intersectionLabelText = DEFAULT_INTERSECTION_LABEL_TEXT;
63
64
65 private String unionLabelText = DEFAULT_UNION_LABEL_TEXT;
66
67
68 private final PText firstLabel = new PText();
69
70
71 private final PText secondLabel = new PText();
72
73
74 private final PText firstOnlyLabel = new PText();
75
76
77 private final PText secondOnlyLabel = new PText();
78
79
80 private final PText intersectionLabel = new PText();
81
82
83 private final PText unionLabel = new PText();
84
85
86 private final List<PText> labels = Arrays.asList(new PText[] { firstLabel, secondLabel, firstOnlyLabel,
87 secondOnlyLabel, intersectionLabel, unionLabel });
88
89
90 private final SetChangeListener<E> update = new SetChangeListener<E>()
91 {
92 @Override
93 public void setChanged(final SetChangeEvent<E> event)
94 {
95 updateLabels();
96 updateContents();
97 }
98 };
99
100
101 public static final String DEFAULT_FIRST_LABEL_TEXT = "First set";
102
103
104 public static final String DEFAULT_SECOND_LABEL_TEXT = "Second set";
105
106
107 public static final String DEFAULT_FIRST_ONLY_LABEL_TEXT = "First only";
108
109
110 public static final String DEFAULT_SECOND_ONLY_LABEL_TEXT = "Second only";
111
112
113 public static final String DEFAULT_INTERSECTION_LABEL_TEXT = "Intersection";
114
115
116 public static final String DEFAULT_UNION_LABEL_TEXT = "Union";
117
118
119
120
121
122 protected AbstractBinaryVennNode()
123 {
124 super();
125 model = new BinaryVennModelImpl<E>();
126
127 installListeners();
128 updateLabels();
129 }
130
131
132
133
134
135
136
137
138
139 protected AbstractBinaryVennNode(final String firstLabelText, final Set<? extends E> first,
140 final String secondLabelText, final Set<? extends E> second)
141 {
142 super();
143 model = new BinaryVennModelImpl<E>(first, second);
144 this.firstLabelText = firstLabelText;
145 this.secondLabelText = secondLabelText;
146 this.firstOnlyLabelText = firstLabelText + " only";
147 this.secondOnlyLabelText = secondLabelText + " only";
148
149 installListeners();
150 updateLabels();
151 }
152
153
154
155
156
157
158 protected AbstractBinaryVennNode(final BinaryVennModel<E> model)
159 {
160 super();
161 if (model == null)
162 {
163 throw new IllegalArgumentException("model must not be null");
164 }
165 this.model = model;
166
167 installListeners();
168 updateLabels();
169 }
170
171
172
173
174
175 private void installListeners()
176 {
177 model.first().addSetChangeListener(update);
178 model.second().addSetChangeListener(update);
179 }
180
181
182
183
184 private void uninstallListeners()
185 {
186 model.first().removeSetChangeListener(update);
187 model.second().removeSetChangeListener(update);
188 }
189
190 @Override
191 protected void updateLabels()
192 {
193 firstLabel.setText(buildLabel(firstLabelText, model.first().size()));
194 secondLabel.setText(buildLabel(secondLabelText, model.second().size()));
195 firstOnlyLabel.setText(buildLabel(firstOnlyLabelText, model.firstOnly().size()));
196 secondOnlyLabel.setText(buildLabel(secondOnlyLabelText, model.secondOnly().size()));
197 intersectionLabel.setText(buildLabel(intersectionLabelText, model.intersection().size()));
198 unionLabel.setText(buildLabel(unionLabelText, model.union().size()));
199
200 firstLabel.setVisible(getDisplayLabels());
201 secondLabel.setVisible(getDisplayLabels());
202 firstOnlyLabel.setVisible(getDisplayLabels());
203 secondOnlyLabel.setVisible(getDisplayLabels());
204 intersectionLabel.setVisible(getDisplayLabels());
205 unionLabel.setVisible(getDisplayLabels());
206 }
207
208
209
210
211 protected abstract void updateContents();
212
213
214 @Override
215 public final Iterable<PText> labels()
216 {
217 return labels;
218 }
219
220
221
222
223
224
225 public final BinaryVennModel<E> getModel()
226 {
227 return model;
228 }
229
230
231
232
233
234
235
236
237 public final void setModel(final BinaryVennModel<E> model)
238 {
239 if (model == null)
240 {
241 throw new IllegalArgumentException("model must not be null");
242 }
243 BinaryVennModel<E> oldModel = this.model;
244 uninstallListeners();
245 this.model = model;
246 installListeners();
247 updateLabels();
248 firePropertyChange(-1, "model", oldModel, this.model);
249 }
250
251
252
253
254
255
256 public final String getFirstLabelText()
257 {
258 return firstLabelText;
259 }
260
261
262
263
264
265
266
267
268 public final void setFirstLabelText(final String firstLabelText)
269 {
270 String oldFirstLabelText = this.firstLabelText;
271 this.firstLabelText = firstLabelText;
272 firstLabel.setText(buildLabel(this.firstLabelText, model.first().size()));
273 firePropertyChange(-1, "firstLabelText", this.firstLabelText, oldFirstLabelText);
274 }
275
276
277
278
279
280
281 public final String getSecondLabelText()
282 {
283 return secondLabelText;
284 }
285
286
287
288
289
290
291
292
293 public final void setSecondLabelText(final String secondLabelText)
294 {
295 String oldSecondLabelText = this.secondLabelText;
296 this.secondLabelText = secondLabelText;
297 secondLabel.setText(buildLabel(this.secondLabelText, model.second().size()));
298 firePropertyChange(-1, "secondLabelText", this.secondLabelText, oldSecondLabelText);
299 }
300
301
302
303
304
305
306 public final String getFirstOnlyLabelText()
307 {
308 return firstOnlyLabelText;
309 }
310
311
312
313
314
315
316
317
318 public final void setFirstOnlyLabelText(final String firstOnlyLabelText)
319 {
320 String oldFirstOnlyLabelText = this.firstOnlyLabelText;
321 this.firstOnlyLabelText = firstOnlyLabelText;
322 firstOnlyLabel.setText(buildLabel(this.firstOnlyLabelText, model.firstOnly().size()));
323 firePropertyChange(-1, "firstOnlyLabelText", this.firstOnlyLabelText, oldFirstOnlyLabelText);
324 }
325
326
327
328
329
330
331 public final String getSecondOnlyLabelText()
332 {
333 return secondOnlyLabelText;
334 }
335
336
337
338
339
340
341
342
343 public final void setSecondOnlyLabelText(final String secondOnlyLabelText)
344 {
345 String oldSecondOnlyLabelText = this.secondOnlyLabelText;
346 this.secondOnlyLabelText = secondOnlyLabelText;
347 secondOnlyLabel.setText(buildLabel(this.secondOnlyLabelText, model.secondOnly().size()));
348 firePropertyChange(-1, "secondOnlyLabelText", this.secondOnlyLabelText, oldSecondOnlyLabelText);
349 }
350
351
352
353
354
355
356 public final String getIntersectionLabelText()
357 {
358 return intersectionLabelText;
359 }
360
361
362
363
364
365
366
367
368 public final void setIntersectionLabelText(final String intersectionLabelText)
369 {
370 String oldIntersectionLabelText = this.intersectionLabelText;
371 this.intersectionLabelText = intersectionLabelText;
372 intersectionLabel.setText(buildLabel(this.intersectionLabelText, model.intersection().size()));
373 firePropertyChange(-1, "intersectionLabelText", this.intersectionLabelText, oldIntersectionLabelText);
374 }
375
376
377
378
379
380
381 public final String getUnionLabelText()
382 {
383 return unionLabelText;
384 }
385
386
387
388
389
390
391
392
393 public final void setUnionLabelText(final String unionLabelText)
394 {
395 String oldUnionLabelText = this.unionLabelText;
396 this.unionLabelText = unionLabelText;
397 unionLabel.setText(buildLabel(this.unionLabelText, model.union().size()));
398 firePropertyChange(-1, "unionLabelText", this.unionLabelText, oldUnionLabelText);
399 }
400
401
402
403
404
405
406
407
408
409
410 public final PText getFirstLabel()
411 {
412 return firstLabel;
413 }
414
415
416
417
418
419
420
421
422
423
424 public final PText getSecondLabel()
425 {
426 return secondLabel;
427 }
428
429
430
431
432
433
434
435
436
437
438 public final PText getFirstOnlyLabel()
439 {
440 return firstOnlyLabel;
441 }
442
443
444
445
446
447
448
449
450
451
452 public final PText getSecondOnlyLabel()
453 {
454 return secondOnlyLabel;
455 }
456
457
458
459
460
461
462
463
464
465
466 public final PText getIntersectionLabel()
467 {
468 return intersectionLabel;
469 }
470
471
472
473
474
475
476
477
478
479
480 public final PText getUnionLabel()
481 {
482 return unionLabel;
483 }
484 }