1 /*
2
3 dsh-piccolo-venn Piccolo2D venn diagram nodes and supporting classes.
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.piccolo.venn;
25
26 import java.util.Set;
27
28 import org.piccolo2d.PNode;
29
30 import org.piccolo2d.nodes.PText;
31
32 /**
33 * Abstract venn diagram node.
34 *
35 * @param <E> value type
36 * @author Michael Heuer
37 * @version $Revision$ $Date$
38 */
39 public abstract class AbstractVennNode<E>
40 extends PNode
41 {
42 /** True if should display labels. */
43 private boolean displayLabels = true;
44
45 /** True if labels should display sizes. */
46 private boolean displaySizes = true;
47
48 /** True if should display size labels. */
49 private boolean displaySizeLabels = true;
50
51 /** True if should display size labels for empty areas. */
52 private boolean displaySizesForEmptyAreas = true;
53
54
55 /**
56 * Create a new abstract venn diagram node.
57 */
58 protected AbstractVennNode()
59 {
60 super();
61 }
62
63
64 /**
65 * Build and return label text.
66 *
67 * @param labelText label text
68 * @param size size
69 * @return label text
70 */
71 protected final String buildLabel(final String labelText, final int size)
72 {
73 StringBuilder sb = new StringBuilder();
74 sb.append(labelText);
75 if (displaySizes)
76 {
77 sb.append(" (");
78 sb.append(size);
79 sb.append(")");
80 }
81 sb.append(":");
82 return sb.toString();
83 }
84
85 /**
86 * Update labels.
87 */
88 protected abstract void updateLabels();
89
90 /**
91 * Return the nodes for this venn diagram node.
92 *
93 * @return the nodes for this venn diagram node
94 */
95 public abstract Iterable<PNode> nodes();
96
97 /**
98 * Return the labels for this venn diagram node.
99 *
100 * @return the labels for this venn diagram node
101 */
102 public abstract Iterable<PText> labels();
103
104 /**
105 * Return the size labels for this venn diagram node.
106 *
107 * @return the size labels for this venn diagram node
108 */
109 public abstract Iterable<PText> sizeLabels();
110
111 /**
112 * Return the label for the specified node, if any.
113 *
114 * @param node node
115 * @return the label for the specified node, or <code>null</code>
116 * if no such label exists
117 */
118 public abstract PText labelForNode(PNode node);
119
120 /**
121 * Return the label text for the specified node, if any.
122 *
123 * @param node node
124 * @return the label text for the specified node, or <code>null</code>
125 * if no such label exists
126 */
127 public abstract String labelTextForNode(PNode node);
128
129 /**
130 * Return the view for the specified node, if any.
131 *
132 * @param node node
133 * @return the view for the specified node, or <code>null</code>
134 * if no such view exists
135 */
136 public abstract Set<E> viewForNode(PNode node);
137
138 /**
139 * Return true if labels should display sizes. Defaults to <code>true</code>.
140 *
141 * @return true if labels should display sizes
142 */
143 public final boolean getDisplaySizes()
144 {
145 return displaySizes;
146 }
147
148 /**
149 * Set to true if labels should display sizes.
150 *
151 * <p>This is a bound property.</p>
152 *
153 * @param displaySizes true if labels should display sizes
154 */
155 public final void setDisplaySizes(final boolean displaySizes)
156 {
157 boolean oldDisplaySizes = this.displaySizes;
158 this.displaySizes = displaySizes;
159 firePropertyChange(-1, "displaySizes", oldDisplaySizes, this.displaySizes);
160 updateLabels();
161 }
162
163 /**
164 * Return true if this venn node should display set labels. Defaults to <code>true</code>.
165 *
166 * @return true if this venn node should display set labels
167 */
168 public final boolean getDisplayLabels()
169 {
170 return displayLabels;
171 }
172
173 /**
174 * Set to true if this venn node should display set labels.
175 *
176 * <p>This is a bound property.</p>
177 *
178 * @param displayLabels true if this venn node should display set labels
179 */
180 public final void setDisplayLabels(final boolean displayLabels)
181 {
182 boolean oldDisplayLabels = this.displayLabels;
183 this.displayLabels = displayLabels;
184 firePropertyChange(-1, "displayLabels", oldDisplayLabels, this.displayLabels);
185 updateLabels();
186 }
187
188 /**
189 * Return true if this venn node should display size labels. Defaults to <code>true</code>.
190 *
191 * @return true if this venn node should display size labels
192 */
193 public final boolean getDisplaySizeLabels()
194 {
195 return displaySizeLabels;
196 }
197
198 /**
199 * Set to true if this venn node should display size labels.
200 *
201 * <p>This is a bound property.</p>
202 *
203 * @param displaySizeLabels true if this venn node should display size labels
204 */
205 public final void setDisplaySizeLabels(final boolean displaySizeLabels)
206 {
207 boolean oldDisplaySizeLabels = this.displaySizeLabels;
208 this.displaySizeLabels = displaySizeLabels;
209 firePropertyChange(-1, "displaySizeLabels", oldDisplaySizeLabels, this.displaySizeLabels);
210 updateLabels();
211 }
212
213 /**
214 * Return true if this venn node should display sizes for empty areas. Defaults to <code>true</code>.
215 *
216 * @return true if this venn node should display sizes for empty areas
217 */
218 public final boolean getDisplaySizesForEmptyAreas()
219 {
220 return displaySizesForEmptyAreas;
221 }
222
223 /**
224 * Set to true if this venn node should display sizes for empty areas.
225 *
226 * <p>This is a bound property.</p>
227 *
228 * @param displaySizesForEmptyAreas true if this venn node should display sizes for empty areas
229 */
230 public final void setDisplaySizesForEmptyAreas(final boolean displaySizesForEmptyAreas)
231 {
232 boolean oldDisplaySizesForEmptyAreas = this.displaySizesForEmptyAreas;
233 this.displaySizesForEmptyAreas = displaySizesForEmptyAreas;
234 firePropertyChange(-1, "displaySizesForEmptyAreas", oldDisplaySizesForEmptyAreas, this.displaySizesForEmptyAreas);
235 updateLabels();
236 }
237 }