View Javadoc

1   /*
2   
3       dsh-venn  Lightweight components for venn diagrams.
4       Copyright (c) 2009-2013 held jointly by the individual authors.
5   
6       This library is free software; you can redistribute it and/or modify it
7       under the terms of the GNU Lesser General Public License as published
8       by the Free Software Foundation; either version 3 of the License, or (at
9       your option) any later version.
10  
11      This library is distributed in the hope that it will be useful, but WITHOUT
12      ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
13      FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14      License for more details.
15  
16      You should have received a copy of the GNU Lesser General Public License
17      along with this library;  if not, write to the Free Software Foundation,
18      Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
19  
20      > http://www.fsf.org/licensing/licenses/lgpl.html
21      > http://www.opensource.org/licenses/lgpl-license.php
22  
23  */
24  package org.dishevelled.venn.swing;
25  
26  import java.util.Iterator;
27  import java.util.Set;
28  
29  import javax.swing.JLabel;
30  
31  import org.dishevelled.venn.QuaternaryVennModel;
32  
33  /**
34   * Quaternary venn diagram label.
35   *
36   * @param <E> value type
37   * @author  Michael Heuer
38   * @version $Revision$ $Date$
39   */
40  public final class QuaternaryVennLabel<E>
41      extends AbstractQuaternaryVennDiagram<E>
42  {
43      /** Number of elements to display. */
44      private int elementsToDisplay = DEFAULT_ELEMENTS_TO_DISPLAY;
45  
46      /** Contents of the first set. */
47      private final JLabel first = new JLabel();
48  
49      /** Contents of the second set. */
50      private final JLabel second = new JLabel();
51  
52      /** Contents of the third set. */
53      private final JLabel third = new JLabel();
54  
55      /** Contents of the fourth set. */
56      private final JLabel fourth = new JLabel();
57  
58      /** Contents of the first only view. */
59      private final JLabel firstOnly = new JLabel();
60  
61      /** Contents for the second only view. */
62      private final JLabel secondOnly = new JLabel();
63  
64      /** Contents for the third only view. */
65      private final JLabel thirdOnly = new JLabel();
66  
67      /** Contents for the fourth only view. */
68      private final JLabel fourthOnly = new JLabel();
69  
70      /** Contents for the first second view. */
71      private final JLabel firstSecond = new JLabel();
72  
73      /** Contents for the first third view. */
74      private final JLabel firstThird = new JLabel();
75  
76      /** Contents for the second third view. */
77      private final JLabel secondThird = new JLabel();
78  
79      /** Contents for the first fourth view. */
80      private final JLabel firstFourth = new JLabel();
81  
82      /** Contents for the second fourth view. */
83      private final JLabel secondFourth = new JLabel();
84  
85      /** Contents for the third fourth view. */
86      private final JLabel thirdFourth = new JLabel();
87  
88      /** Contents for the first second third view. */
89      private final JLabel firstSecondThird = new JLabel();
90  
91      /** Contents for the first second fourth view. */
92      private final JLabel firstSecondFourth = new JLabel();
93  
94      /** Contents for the first third fourth view. */
95      private final JLabel firstThirdFourth = new JLabel();
96  
97      /** Contents for the second third fourth view. */
98      private final JLabel secondThirdFourth = new JLabel();
99  
100     /** Contents of the intersection view. */
101     private final JLabel intersection = new JLabel();
102 
103     /** Contents of the union view. */
104     private final JLabel union = new JLabel();
105 
106     /** Default number of set elements to display, <code>5</code>. */
107     private static final int DEFAULT_ELEMENTS_TO_DISPLAY = 5;
108 
109 
110     /**
111      * Create a new empty quaternary venn label.
112      */
113     public QuaternaryVennLabel()
114     {
115         super();
116         updateContents();
117         layoutComponents();
118     }
119 
120     /**
121      * Create a new quaternary venn label with the specified sets.
122      *
123      * @param firstLabelText label text for the first set
124      * @param first first set, must not be null
125      * @param secondLabelText label text for the second set
126      * @param second second set, must not be null
127      * @param thirdLabelText label text for the third set
128      * @param third third set, must not be null
129      * @param fourthLabelText label text for the fourth set
130      * @param fourth fourth set, must not be null
131      */
132     public QuaternaryVennLabel(final String firstLabelText, final Set<? extends E> first,
133                                 final String secondLabelText, final Set<? extends E> second,
134                                 final String thirdLabelText, final Set<? extends E> third,
135                                 final String fourthLabelText, final Set<? extends E> fourth)
136     {
137         super(firstLabelText, first, secondLabelText, second, thirdLabelText, third, fourthLabelText, fourth);
138         updateContents();
139         layoutComponents();
140     }
141 
142     /**
143      * Create a new quaternary venn label with the specified model.
144      *
145      * @param model model for this quaternary venn label, must not be null
146      */
147     public QuaternaryVennLabel(final QuaternaryVennModel<E> model)
148     {
149         super(model);
150         updateContents();
151         layoutComponents();
152     }
153 
154 
155     /** {@inheritDoc} */
156     protected void updateContents()
157     {
158         first.setText(buildContents(getModel().first()));
159         second.setText(buildContents(getModel().second()));
160         third.setText(buildContents(getModel().third()));
161         fourth.setText(buildContents(getModel().third()));
162         firstOnly.setText(buildContents(getModel().firstOnly()));
163         secondOnly.setText(buildContents(getModel().secondOnly()));
164         thirdOnly.setText(buildContents(getModel().thirdOnly()));
165         fourthOnly.setText(buildContents(getModel().fourthOnly()));
166         firstSecond.setText(buildContents(getModel().firstSecond()));
167         firstThird.setText(buildContents(getModel().firstThird()));
168         secondThird.setText(buildContents(getModel().secondThird()));
169         firstFourth.setText(buildContents(getModel().firstFourth()));
170         secondFourth.setText(buildContents(getModel().secondFourth()));
171         thirdFourth.setText(buildContents(getModel().thirdFourth()));
172         firstSecondThird.setText(buildContents(getModel().firstSecondThird()));
173         firstSecondFourth.setText(buildContents(getModel().firstSecondFourth()));
174         firstThirdFourth.setText(buildContents(getModel().firstThirdFourth()));
175         secondThirdFourth.setText(buildContents(getModel().secondThirdFourth()));
176         intersection.setText(buildContents(getModel().intersection()));
177         union.setText(buildContents(getModel().union()));
178     }
179 
180     /**
181      * Layout components.
182      */
183     private void layoutComponents()
184     {
185         addField(getFirstLabel(), first);
186         addField(getSecondLabel(), second);
187         addField(getThirdLabel(), third);
188         addField(getFourthLabel(), fourth);
189         addField(getFirstOnlyLabel(), firstOnly);
190         addField(getSecondOnlyLabel(), secondOnly);
191         addField(getThirdOnlyLabel(), thirdOnly);
192         addField(getFourthOnlyLabel(), fourthOnly);
193         addField(getFirstSecondLabel(), firstSecond);
194         addField(getFirstThirdLabel(), firstThird);
195         addField(getSecondThirdLabel(), secondThird);
196         addField(getSecondFourthLabel(), secondFourth);
197         addField(getFirstFourthLabel(), firstFourth);
198         addField(getThirdFourthLabel(), thirdFourth);
199         addField(getFirstSecondThirdLabel(), firstSecondThird);
200         addField(getFirstSecondFourthLabel(), firstSecondFourth);
201         addField(getFirstThirdFourthLabel(), firstThirdFourth);
202         addField(getSecondThirdFourthLabel(), secondThirdFourth);
203         addField(getIntersectionLabel(), intersection);
204         addField(getUnionLabel(), union);
205         addFinalSpacing();
206     }
207 
208     /**
209      * Build and return content text.
210      *
211      * @param set set
212      * @return content text
213      */
214     private String buildContents(final Set<E> set)
215     {
216         if (set.isEmpty())
217         {
218             return null;
219         }
220         StringBuilder sb = new StringBuilder();
221         Iterator<E> iterator = set.iterator();
222         sb.append(iterator.next().toString());
223         int count = 1;
224         while (iterator.hasNext())
225         {
226             sb.append(", ");
227             sb.append(iterator.next().toString());
228             if (count++ > elementsToDisplay)
229             {
230                 break;
231             }
232         }
233         if (set.size() > elementsToDisplay)
234         {
235             sb.append(", ...");
236         }
237         return sb.toString();
238     }
239 
240 
241     /**
242      * Return the number of set elements to display.  Defaults to {@link #DEFAULT_ELEMENTS_TO_DISPLAY}.
243      *
244      * @return the number of set elements to display
245      */
246     public int getElementsToDisplay()
247     {
248         return elementsToDisplay;
249     }
250 
251     /**
252      * Set the number of set elements to display to <code>elementsToDisplay</code>.
253      *
254      * <p>This is a bound property.</p>
255      *
256      * @param elementsToDisplay number of elements to display
257      */
258     public void setElementsToDisplay(final int elementsToDisplay)
259     {
260         int oldElementsToDisplay = this.elementsToDisplay;
261         this.elementsToDisplay = elementsToDisplay;
262         firePropertyChange("elementsToDisplay", oldElementsToDisplay, this.elementsToDisplay);
263     }
264 
265     /**
266      * Return the contents of the first set.  The text for the returned JLabel
267      * should not be changed, as the current text is synchronized to the
268      * quaternary venn model backing this venn diagram.
269      *
270      * @return the contents of the first set
271      */
272     public JLabel getFirst()
273     {
274         return first;
275     }
276 
277     /**
278      * Return the contents of the second set.  The text for the returned JLabel
279      * should not be changed, as the current text is synchronized to the
280      * quaternary venn model backing this venn diagram.
281      *
282      * @return the contents of the second set
283      */
284     public JLabel getSecond()
285     {
286         return second;
287     }
288 
289     /**
290      * Return the contents of the third set.  The text for the returned JLabel
291      * should not be changed, as the current text is synchronized to the
292      * quaternary venn model backing this venn diagram.
293      *
294      * @return the contents of the third set
295      */
296     public JLabel getThird()
297     {
298         return third;
299     }
300 
301     /**
302      * Return the contents of the fourth set.  The text for the returned JLabel
303      * should not be changed, as the current text is synchronized to the
304      * quaternary venn model backing this venn diagram.
305      *
306      * @return the contents of the fourth set
307      */
308     public JLabel getFourth()
309     {
310         return fourth;
311     }
312 
313     /**
314      * Return the contents of the first only view.  The text for the returned JLabel
315      * should not be changed, as the current text is synchronized to the
316      * quaternary venn model backing this venn diagram.
317      *
318      * @return the contents of the first only view
319      */
320     public JLabel getFirstOnly()
321     {
322         return firstOnly;
323     }
324 
325     /**
326      * Return the contents of the second only view.  The text for the returned JLabel
327      * should not be changed, as the current text is synchronized to the
328      * quaternary venn model backing this venn diagram.
329      *
330      * @return the contents of the second only view
331      */
332     public JLabel getSecondOnly()
333     {
334         return secondOnly;
335     }
336 
337     /**
338      * Return the contents of the third only view.  The text for the returned JLabel
339      * should not be changed, as the current text is synchronized to the
340      * quaternary venn model backing this venn diagram.
341      *
342      * @return the contents of the third only view
343      */
344     public JLabel getThirdOnly()
345     {
346         return thirdOnly;
347     }
348 
349     /**
350      * Return the contents of the fourth only view.  The text for the returned JLabel
351      * should not be changed, as the current text is synchronized to the
352      * quaternary venn model backing this venn diagram.
353      *
354      * @return the contents of the fourth only view
355      */
356     public JLabel getFourthOnly()
357     {
358         return fourthOnly;
359     }
360 
361     /**
362      * Return the contents of the first second view.  The text for the returned JLabel
363      * should not be changed, as the current text is synchronized to the
364      * quaternary venn model backing this venn diagram.
365      *
366      * @return the contents of the first second view
367      */
368     public JLabel getFirstSecond()
369     {
370         return firstSecond;
371     }
372 
373     /**
374      * Return the contents of the second third view.  The text for the returned JLabel
375      * should not be changed, as the current text is synchronized to the
376      * quaternary venn model backing this venn diagram.
377      *
378      * @return the contents of the second third view
379      */
380     public JLabel getSecondThird()
381     {
382         return secondThird;
383     }
384 
385     /**
386      * Return the contents of the first third view.  The text for the returned JLabel
387      * should not be changed, as the current text is synchronized to the
388      * quaternary venn model backing this venn diagram.
389      *
390      * @return the contents of the first third view
391      */
392     public JLabel getFirstThird()
393     {
394         return firstThird;
395     }
396 
397     /**
398      * Return the contents of the first fourth view.  The text for the returned JLabel
399      * should not be changed, as the current text is synchronized to the
400      * quaternary venn model backing this venn diagram.
401      *
402      * @return the contents of the first fourth view
403      */
404     public JLabel getFirstFourth()
405     {
406         return firstFourth;
407     }
408 
409     /**
410      * Return the contents of the second fourth view.  The text for the returned JLabel
411      * should not be changed, as the current text is synchronized to the
412      * quaternary venn model backing this venn diagram.
413      *
414      * @return the contents of the second fourth view
415      */
416     public JLabel getSecondFourth()
417     {
418         return secondFourth;
419     }
420 
421     /**
422      * Return the contents of the third fourth view.  The text for the returned JLabel
423      * should not be changed, as the current text is synchronized to the
424      * quaternary venn model backing this venn diagram.
425      *
426      * @return the contents of the third fourth view
427      */
428     public JLabel getThirdFourth()
429     {
430         return thirdFourth;
431     }
432 
433     /**
434      * Return the contents of the first second third view.  The text for the returned JLabel
435      * should not be changed, as the current text is synchronized to the
436      * quaternary venn model backing this venn diagram.
437      *
438      * @return the contents of the first second third view
439      */
440     public JLabel getFirstSecondThird()
441     {
442         return firstSecondThird;
443     }
444 
445     /**
446      * Return the contents of the first second fourth view.  The text for the returned JLabel
447      * should not be changed, as the current text is synchronized to the
448      * quaternary venn model backing this venn diagram.
449      *
450      * @return the contents of the first second fourth view
451      */
452     public JLabel getFirstSecondFourth()
453     {
454         return firstSecondFourth;
455     }
456 
457     /**
458      * Return the contents of the first third fourth view.  The text for the returned JLabel
459      * should not be changed, as the current text is synchronized to the
460      * quaternary venn model backing this venn diagram.
461      *
462      * @return the contents of the first third fourth view
463      */
464     public JLabel getFirstThirdFourth()
465     {
466         return firstThirdFourth;
467     }
468 
469     /**
470      * Return the contents of the second third fourth view.  The text for the returned JLabel
471      * should not be changed, as the current text is synchronized to the
472      * quaternary venn model backing this venn diagram.
473      *
474      * @return the contents of the second third fourth view
475      */
476     public JLabel getSecondThirdFourth()
477     {
478         return secondThirdFourth;
479     }
480 
481     /**
482      * Return the contents of the intersection view.  The text for the returned JLabel
483      * should not be changed, as the current text is synchronized to the
484      * quaternary venn model backing this venn diagram.
485      *
486      * @return the contents of the intersection view
487      */
488     public JLabel getIntersection()
489     {
490         return intersection;
491     }
492 
493     /**
494      * Return the contents of the union view.  The text for the returned JLabel
495      * should not be changed, as the current text is synchronized to the
496      * quaternary venn model backing this venn diagram.
497      *
498      * @return the contents of the union view
499      */
500     public JLabel getUnion()
501     {
502         return union;
503     }
504 }