4
* Copyright (C) 2012 Andrew Rambaut
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
package figtree.treeviewer.decorators;
23
import jebl.evolution.graphs.Node;
24
import jebl.util.Attributable;
27
import java.awt.geom.Point2D;
29
import java.util.List;
32
* This decorator takes an attribute name and a set of attibutable Objects.
33
* Colours are given to each individual value.
35
* If the data take more values than colors, then they will wrap around
37
* @author Andrew Rambaut
38
* @version $Id: DiscreteColourDecorator.java 639 2007-02-15 10:05:28Z rambaut $
40
public abstract class ColourDecorator implements Decorator {
42
public ColourDecorator(String attributeName) {
43
this.attributeName = attributeName;
46
public ColourDecorator(String attributeName, Set<? extends Attributable> items) {
47
setAttributes(attributeName, items);
50
public abstract void setup(String settings);
53
public boolean allowsGradient() {
57
public void setAttributes(String attributeName, Set<? extends Attributable> items) {
58
this.attributeName = attributeName;
61
// Decorator INTERFACE
62
public Paint getPaint(Paint paint) {
63
if (this.paint == null) return paint;
67
public Paint getFillPaint(Paint paint) {
68
if (this.fillPaint == null) return paint;
72
public Paint getPaint(Paint paint, Point2D point1, Point2D point2) {
73
if (colour1 != null && colour2 != null) {
74
return new GradientPaint(point1, colour1, point2, colour2, false);
76
return getPaint(paint);
80
public Paint getFillPaint(Paint paint, Point2D point1, Point2D point2) {
81
if (fillColour1 != null && fillColour2 != null) {
82
return new GradientPaint(point1, fillColour1, point2, fillColour2, false);
84
return getFillPaint(paint);
88
public String getAttributeName() {
92
public Stroke getStroke(Stroke stroke) {
96
public Font getFont(Font font) {
100
public void setItem(Object item) {
101
if (item instanceof Node) {
102
setAttributableItem((Attributable)item);
103
} if (item instanceof Attributable) {
104
setAttributableItem((Attributable)item);
106
paint = getColourForValue(item);
110
public void setItems(Object item1, Object item2) {
124
Object value1 = item1;
125
if (item1 instanceof Attributable) {
126
value1 = ((Attributable)item1).getAttribute(getAttributeName());
128
if (value1 != null) {
129
colour1 = getColourForValue(value1);
133
Object value2 = item2;
134
if (item2 instanceof Attributable) {
135
value2 = ((Attributable)item2).getAttribute(getAttributeName());
137
if (value2 != null) {
138
colour2 = getColourForValue(value2);
143
if (colour1 != null) {
144
fillColour1 = getLighterColour(colour1);
149
if (colour2 != null) {
150
fillColour2 = getLighterColour(colour2);
156
public static boolean isDiscrete(String attributeName, Set<? extends Attributable> items) {
157
// First collect the set of all attribute values
158
Set<Object> values = new HashSet<Object>();
159
for (Attributable item : items) {
160
Object value = item.getAttribute(attributeName);
166
boolean isNumber = true;
167
boolean isInteger = true;
169
for (Object value : values) {
170
if (value instanceof Number) {
171
if (((Number)value).doubleValue() != ((Number)value).intValue()) {
179
if (isNumber && !isInteger) return false;
184
public static boolean isNumerical(String attributeName, Set<? extends Attributable> items) {
185
// First collect the set of all attribute values
186
Set<Object> values = new HashSet<Object>();
187
for (Attributable item : items) {
188
Object value = item.getAttribute(attributeName);
194
boolean isNumber = true;
196
for (Object value : values) {
197
if (!(value instanceof Number)) {
206
private void setAttributableItem(Attributable item) {
208
Object value = item.getAttribute(attributeName);
210
Color colour = getColourForValue(value);
211
if (colour != null) {
213
fillPaint = getLighterColour(colour);
220
protected abstract Color getColourForValue(Object value);
222
protected Color getLighterColour(Color color) {
223
return new Color(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha() / 2);
226
private String attributeName = null;
228
protected Paint paint = null;
229
protected Paint fillPaint = null;
230
protected Color colour1 = null;
231
protected Color colour2 = null;
232
protected Color fillColour1 = null;
233
protected Color fillColour2 = null;