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.TernaryVennModel;
34 import org.dishevelled.venn.model.TernaryVennModelImpl;
35
36
37
38
39
40
41
42
43 public abstract class AbstractTernaryVennNode<E>
44 extends AbstractVennNode<E>
45 {
46
47 private TernaryVennModel<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 thirdLabelText = DEFAULT_THIRD_LABEL_TEXT;
57
58
59 private String firstOnlyLabelText = DEFAULT_FIRST_ONLY_LABEL_TEXT;
60
61
62 private String secondOnlyLabelText = DEFAULT_SECOND_ONLY_LABEL_TEXT;
63
64
65 private String thirdOnlyLabelText = DEFAULT_THIRD_ONLY_LABEL_TEXT;
66
67
68 private String firstSecondLabelText = DEFAULT_FIRST_SECOND_LABEL_TEXT;
69
70
71 private String firstThirdLabelText = DEFAULT_FIRST_THIRD_LABEL_TEXT;
72
73
74 private String secondThirdLabelText = DEFAULT_SECOND_THIRD_LABEL_TEXT;
75
76
77 private String intersectionLabelText = DEFAULT_INTERSECTION_LABEL_TEXT;
78
79
80 private String unionLabelText = DEFAULT_UNION_LABEL_TEXT;
81
82
83 private final PText firstLabel = new PText();
84
85
86 private final PText secondLabel = new PText();
87
88
89 private final PText thirdLabel = new PText();
90
91
92 private final PText firstOnlyLabel = new PText();
93
94
95 private final PText secondOnlyLabel = new PText();
96
97
98 private final PText thirdOnlyLabel = new PText();
99
100
101 private final PText firstSecondLabel = new PText();
102
103
104 private final PText firstThirdLabel = new PText();
105
106
107 private final PText secondThirdLabel = new PText();
108
109
110 private final PText intersectionLabel = new PText();
111
112
113 private final PText unionLabel = new PText();
114
115
116 private final List<PText> labels = Arrays.asList(new PText[] { firstLabel, secondLabel, thirdLabel, firstOnlyLabel,
117 secondOnlyLabel, thirdOnlyLabel, firstSecondLabel, firstThirdLabel,
118 secondThirdLabel, intersectionLabel, unionLabel });
119
120
121 private final SetChangeListener<E> update = new SetChangeListener<E>()
122 {
123 @Override
124 public void setChanged(final SetChangeEvent<E> event)
125 {
126 updateLabels();
127 updateContents();
128 }
129 };
130
131
132 public static final String DEFAULT_FIRST_LABEL_TEXT = "First set";
133
134
135 public static final String DEFAULT_SECOND_LABEL_TEXT = "Second set";
136
137
138 public static final String DEFAULT_THIRD_LABEL_TEXT = "Third set";
139
140
141 public static final String DEFAULT_FIRST_ONLY_LABEL_TEXT = "First only";
142
143
144 public static final String DEFAULT_SECOND_ONLY_LABEL_TEXT = "Second only";
145
146
147 public static final String DEFAULT_THIRD_ONLY_LABEL_TEXT = "Third only";
148
149
150 public static final String DEFAULT_FIRST_SECOND_LABEL_TEXT = "First and second only";
151
152
153 public static final String DEFAULT_FIRST_THIRD_LABEL_TEXT = "First and third only";
154
155
156 public static final String DEFAULT_SECOND_THIRD_LABEL_TEXT = "Second and third only";
157
158
159 public static final String DEFAULT_INTERSECTION_LABEL_TEXT = "Intersection";
160
161
162 public static final String DEFAULT_UNION_LABEL_TEXT = "Union";
163
164
165
166
167
168 protected AbstractTernaryVennNode()
169 {
170 super();
171 model = new TernaryVennModelImpl<E>();
172
173 installListeners();
174 updateLabels();
175 }
176
177
178
179
180
181
182
183
184
185
186
187 protected AbstractTernaryVennNode(final String firstLabelText, final Set<? extends E> first,
188 final String secondLabelText, final Set<? extends E> second,
189 final String thirdLabelText, final Set<? extends E> third)
190 {
191 super();
192 model = new TernaryVennModelImpl<E>(first, second, third);
193 this.firstLabelText = firstLabelText;
194 this.secondLabelText = secondLabelText;
195 this.thirdLabelText = thirdLabelText;
196 this.firstOnlyLabelText = firstLabelText + " only";
197 this.secondOnlyLabelText = secondLabelText + " only";
198 this.thirdOnlyLabelText = thirdLabelText + " only";
199 this.firstSecondLabelText = firstLabelText + " and " + secondLabelText + " only";
200 this.firstThirdLabelText = firstLabelText + " and " + thirdLabelText + " only";
201 this.secondThirdLabelText = secondLabelText + " and " + thirdLabelText + " only";
202
203 installListeners();
204 updateLabels();
205 }
206
207
208
209
210
211
212 protected AbstractTernaryVennNode(final TernaryVennModel<E> model)
213 {
214 super();
215 if (model == null)
216 {
217 throw new IllegalArgumentException("model must not be null");
218 }
219 this.model = model;
220
221 installListeners();
222 updateLabels();
223 }
224
225
226
227
228
229 private void installListeners()
230 {
231 model.first().addSetChangeListener(update);
232 model.second().addSetChangeListener(update);
233 model.third().addSetChangeListener(update);
234 }
235
236
237
238
239 private void uninstallListeners()
240 {
241 model.first().removeSetChangeListener(update);
242 model.second().removeSetChangeListener(update);
243 model.third().removeSetChangeListener(update);
244 }
245
246 @Override
247 protected void updateLabels()
248 {
249 firstLabel.setText(buildLabel(firstLabelText, model.first().size()));
250 secondLabel.setText(buildLabel(secondLabelText, model.second().size()));
251 thirdLabel.setText(buildLabel(thirdLabelText, model.third().size()));
252 firstOnlyLabel.setText(buildLabel(firstOnlyLabelText, model.firstOnly().size()));
253 secondOnlyLabel.setText(buildLabel(secondOnlyLabelText, model.secondOnly().size()));
254 thirdOnlyLabel.setText(buildLabel(thirdOnlyLabelText, model.thirdOnly().size()));
255 firstSecondLabel.setText(buildLabel(firstSecondLabelText, model.firstSecond().size()));
256 firstThirdLabel.setText(buildLabel(firstThirdLabelText, model.firstThird().size()));
257 secondThirdLabel.setText(buildLabel(secondThirdLabelText, model.secondThird().size()));
258 intersectionLabel.setText(buildLabel(intersectionLabelText, model.intersection().size()));
259 unionLabel.setText(buildLabel(unionLabelText, model.union().size()));
260
261 firstLabel.setVisible(getDisplayLabels());
262 secondLabel.setVisible(getDisplayLabels());
263 thirdLabel.setVisible(getDisplayLabels());
264 firstOnlyLabel.setVisible(getDisplayLabels());
265 secondOnlyLabel.setVisible(getDisplayLabels());
266 thirdOnlyLabel.setVisible(getDisplayLabels());
267 firstSecondLabel.setVisible(getDisplayLabels());
268 firstThirdLabel.setVisible(getDisplayLabels());
269 secondThirdLabel.setVisible(getDisplayLabels());
270 intersectionLabel.setVisible(getDisplayLabels());
271 unionLabel.setVisible(getDisplayLabels());
272 }
273
274
275
276
277 protected abstract void updateContents();
278
279
280 @Override
281 public final Iterable<PText> labels()
282 {
283 return labels;
284 }
285
286
287
288
289
290
291 public final TernaryVennModel<E> getModel()
292 {
293 return model;
294 }
295
296
297
298
299
300
301
302
303 public final void setModel(final TernaryVennModel<E> model)
304 {
305 if (model == null)
306 {
307 throw new IllegalArgumentException("model must not be null");
308 }
309 TernaryVennModel<E> oldModel = this.model;
310 uninstallListeners();
311 this.model = model;
312 installListeners();
313 updateLabels();
314 firePropertyChange(-1, "model", oldModel, this.model);
315 }
316
317
318
319
320
321
322 public final String getFirstLabelText()
323 {
324 return firstLabelText;
325 }
326
327
328
329
330
331
332
333
334 public final void setFirstLabelText(final String firstLabelText)
335 {
336 String oldFirstLabelText = this.firstLabelText;
337 this.firstLabelText = firstLabelText;
338 firstLabel.setText(buildLabel(this.firstLabelText, model.first().size()));
339 firePropertyChange(-1, "firstLabelText", this.firstLabelText, oldFirstLabelText);
340 }
341
342
343
344
345
346
347 public final String getSecondLabelText()
348 {
349 return secondLabelText;
350 }
351
352
353
354
355
356
357
358
359 public final void setSecondLabelText(final String secondLabelText)
360 {
361 String oldSecondLabelText = this.secondLabelText;
362 this.secondLabelText = secondLabelText;
363 secondLabel.setText(buildLabel(this.secondLabelText, model.second().size()));
364 firePropertyChange(-1, "secondLabelText", this.secondLabelText, oldSecondLabelText);
365 }
366
367
368
369
370
371
372 public final String getThirdLabelText()
373 {
374 return thirdLabelText;
375 }
376
377
378
379
380
381
382
383
384 public final void setThirdLabelText(final String thirdLabelText)
385 {
386 String oldThirdLabelText = this.thirdLabelText;
387 this.thirdLabelText = thirdLabelText;
388 thirdLabel.setText(buildLabel(this.thirdLabelText, model.third().size()));
389 firePropertyChange(-1, "thirdLabelText", this.thirdLabelText, oldThirdLabelText);
390 }
391
392
393
394
395
396
397 public final String getFirstOnlyLabelText()
398 {
399 return firstOnlyLabelText;
400 }
401
402
403
404
405
406
407
408
409 public final void setFirstOnlyLabelText(final String firstOnlyLabelText)
410 {
411 String oldFirstOnlyLabelText = this.firstOnlyLabelText;
412 this.firstOnlyLabelText = firstOnlyLabelText;
413 firstOnlyLabel.setText(buildLabel(this.firstOnlyLabelText, model.firstOnly().size()));
414 firePropertyChange(-1, "firstOnlyLabelText", this.firstOnlyLabelText, oldFirstOnlyLabelText);
415 }
416
417
418
419
420
421
422 public final String getSecondOnlyLabelText()
423 {
424 return secondOnlyLabelText;
425 }
426
427
428
429
430
431
432
433
434 public final void setSecondOnlyLabelText(final String secondOnlyLabelText)
435 {
436 String oldSecondOnlyLabelText = this.secondOnlyLabelText;
437 this.secondOnlyLabelText = secondOnlyLabelText;
438 secondOnlyLabel.setText(buildLabel(this.secondOnlyLabelText, model.secondOnly().size()));
439 firePropertyChange(-1, "secondOnlyLabelText", this.secondOnlyLabelText, oldSecondOnlyLabelText);
440 }
441
442
443
444
445
446
447 public final String getThirdOnlyLabelText()
448 {
449 return thirdOnlyLabelText;
450 }
451
452
453
454
455
456
457
458
459 public final void setThirdOnlyLabelText(final String thirdOnlyLabelText)
460 {
461 String oldThirdOnlyLabelText = this.thirdOnlyLabelText;
462 this.thirdOnlyLabelText = thirdOnlyLabelText;
463 thirdOnlyLabel.setText(buildLabel(this.thirdOnlyLabelText, model.thirdOnly().size()));
464 firePropertyChange(-1, "thirdOnlyLabelText", this.thirdOnlyLabelText, oldThirdOnlyLabelText);
465 }
466
467
468
469
470
471
472 public final String getFirstSecondLabelText()
473 {
474 return firstSecondLabelText;
475 }
476
477
478
479
480
481
482
483
484 public final void setFirstSecondLabelText(final String firstSecondLabelText)
485 {
486 String oldFirstSecondLabelText = this.firstSecondLabelText;
487 this.firstSecondLabelText = firstSecondLabelText;
488 firstSecondLabel.setText(buildLabel(this.firstSecondLabelText, model.firstSecond().size()));
489 firePropertyChange(-1, "firstSecondLabelText", this.firstSecondLabelText, oldFirstSecondLabelText);
490 }
491
492
493
494
495
496
497 public final String getFirstThirdLabelText()
498 {
499 return firstThirdLabelText;
500 }
501
502
503
504
505
506
507
508
509 public final void setFirstThirdLabelText(final String firstThirdLabelText)
510 {
511 String oldFirstThirdLabelText = this.firstThirdLabelText;
512 this.firstThirdLabelText = firstThirdLabelText;
513 firstThirdLabel.setText(buildLabel(this.firstThirdLabelText, model.firstThird().size()));
514 firePropertyChange(-1, "firstThirdLabelText", this.firstThirdLabelText, oldFirstThirdLabelText);
515 }
516
517
518
519
520
521
522 public final String getSecondThirdLabelText()
523 {
524 return secondThirdLabelText;
525 }
526
527
528
529
530
531
532
533
534 public final void setSecondThirdLabelText(final String secondThirdLabelText)
535 {
536 String oldSecondThirdLabelText = this.secondThirdLabelText;
537 this.secondThirdLabelText = secondThirdLabelText;
538 secondThirdLabel.setText(buildLabel(this.secondThirdLabelText, model.secondThird().size()));
539 firePropertyChange(-1, "secondThirdLabelText", this.secondThirdLabelText, oldSecondThirdLabelText);
540 }
541
542
543
544
545
546
547 public final String getIntersectionLabelText()
548 {
549 return intersectionLabelText;
550 }
551
552
553
554
555
556
557
558
559 public final void setIntersectionLabelText(final String intersectionLabelText)
560 {
561 String oldIntersectionLabelText = this.intersectionLabelText;
562 this.intersectionLabelText = intersectionLabelText;
563 intersectionLabel.setText(buildLabel(this.intersectionLabelText, model.intersection().size()));
564 firePropertyChange(-1, "intersectionLabelText", this.intersectionLabelText, oldIntersectionLabelText);
565 }
566
567
568
569
570
571
572 public final String getUnionLabelText()
573 {
574 return unionLabelText;
575 }
576
577
578
579
580
581
582
583
584 public final void setUnionLabelText(final String unionLabelText)
585 {
586 String oldUnionLabelText = this.unionLabelText;
587 this.unionLabelText = unionLabelText;
588 unionLabel.setText(buildLabel(this.unionLabelText, model.union().size()));
589 firePropertyChange(-1, "unionLabelText", this.unionLabelText, oldUnionLabelText);
590 }
591
592
593
594
595
596
597
598
599
600
601 public final PText getFirstLabel()
602 {
603 return firstLabel;
604 }
605
606
607
608
609
610
611
612
613
614
615 public final PText getSecondLabel()
616 {
617 return secondLabel;
618 }
619
620
621
622
623
624
625
626
627
628
629 public final PText getThirdLabel()
630 {
631 return thirdLabel;
632 }
633
634
635
636
637
638
639
640
641
642
643 public final PText getFirstOnlyLabel()
644 {
645 return firstOnlyLabel;
646 }
647
648
649
650
651
652
653
654
655
656
657 public final PText getSecondOnlyLabel()
658 {
659 return secondOnlyLabel;
660 }
661
662
663
664
665
666
667
668
669
670
671 public final PText getThirdOnlyLabel()
672 {
673 return thirdOnlyLabel;
674 }
675
676
677
678
679
680
681
682
683
684
685 public final PText getFirstSecondLabel()
686 {
687 return firstSecondLabel;
688 }
689
690
691
692
693
694
695
696
697
698
699 public final PText getFirstThirdLabel()
700 {
701 return firstThirdLabel;
702 }
703
704
705
706
707
708
709
710
711
712
713 public final PText getSecondThirdLabel()
714 {
715 return secondThirdLabel;
716 }
717
718
719
720
721
722
723
724
725
726
727 public final PText getIntersectionLabel()
728 {
729 return intersectionLabel;
730 }
731
732
733
734
735
736
737
738
739
740
741 public final PText getUnionLabel()
742 {
743 return unionLabel;
744 }
745 }