~ubuntu-branches/ubuntu/saucy/figtree/saucy

« back to all changes in this revision

Viewing changes to src/figtree/treeviewer/decorators/ColourDecorator.java

  • Committer: Package Import Robot
  • Author(s): Olivier Sallou
  • Date: 2013-07-09 14:42:53 UTC
  • mfrom: (2.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20130709144253-momqq3s7wthsu9zn
Tags: 1.4-2
Force set of JAVA_HOME in debian/rules (Closes: #715225). 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * ColourDecorator.java
 
3
 *
 
4
 * Copyright (C) 2012 Andrew Rambaut
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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.
 
19
 */
 
20
 
 
21
package figtree.treeviewer.decorators;
 
22
 
 
23
import jebl.evolution.graphs.Node;
 
24
import jebl.util.Attributable;
 
25
 
 
26
import java.awt.*;
 
27
import java.awt.geom.Point2D;
 
28
import java.util.*;
 
29
import java.util.List;
 
30
 
 
31
/**
 
32
 * This decorator takes an attribute name and a set of attibutable Objects.
 
33
 * Colours are given to each individual value.
 
34
 *
 
35
 * If the data take more values than colors, then they will wrap around
 
36
 *
 
37
 * @author Andrew Rambaut
 
38
 * @version $Id: DiscreteColourDecorator.java 639 2007-02-15 10:05:28Z rambaut $
 
39
 */
 
40
public abstract class ColourDecorator implements Decorator {
 
41
 
 
42
    public ColourDecorator(String attributeName) {
 
43
        this.attributeName = attributeName;
 
44
    }
 
45
 
 
46
    public ColourDecorator(String attributeName, Set<? extends Attributable> items) {
 
47
        setAttributes(attributeName, items);
 
48
    }
 
49
 
 
50
    public abstract void setup(String settings);
 
51
 
 
52
        @Override
 
53
    public boolean allowsGradient() {
 
54
        return true;
 
55
    }
 
56
 
 
57
    public void setAttributes(String attributeName, Set<? extends Attributable> items) {
 
58
        this.attributeName = attributeName;
 
59
    }
 
60
 
 
61
    // Decorator INTERFACE
 
62
    public Paint getPaint(Paint paint) {
 
63
        if (this.paint == null) return paint;
 
64
        return this.paint;
 
65
    }
 
66
 
 
67
    public Paint getFillPaint(Paint paint) {
 
68
        if (this.fillPaint == null) return paint;
 
69
        return fillPaint;
 
70
    }
 
71
 
 
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);
 
75
        } else {
 
76
            return getPaint(paint);
 
77
        }
 
78
    }
 
79
 
 
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);
 
83
        } else {
 
84
            return getFillPaint(paint);
 
85
        }
 
86
    }
 
87
 
 
88
    public String getAttributeName() {
 
89
        return attributeName;
 
90
    }
 
91
 
 
92
    public Stroke getStroke(Stroke stroke) {
 
93
        return stroke;
 
94
    }
 
95
 
 
96
    public Font getFont(Font font) {
 
97
        return font;
 
98
    }
 
99
 
 
100
    public void setItem(Object item) {
 
101
        if (item instanceof Node) {
 
102
            setAttributableItem((Attributable)item);
 
103
        } if (item instanceof Attributable) {
 
104
            setAttributableItem((Attributable)item);
 
105
        } else {
 
106
            paint = getColourForValue(item);
 
107
        }
 
108
    }
 
109
 
 
110
    public void setItems(Object item1, Object item2) {
 
111
        colour1 = null;
 
112
        colour2 = null;
 
113
 
 
114
        if (item2 == null) {
 
115
            setItem(item1);
 
116
            return;
 
117
        }
 
118
 
 
119
        if (item1 == null) {
 
120
            setItem(item2);
 
121
            return;
 
122
        }
 
123
 
 
124
        Object value1 = item1;
 
125
        if (item1 instanceof Attributable) {
 
126
            value1 = ((Attributable)item1).getAttribute(getAttributeName());
 
127
        }
 
128
        if (value1 != null) {
 
129
            colour1 = getColourForValue(value1);
 
130
            paint = colour1;
 
131
        }
 
132
 
 
133
        Object value2 = item2;
 
134
        if (item2 instanceof Attributable) {
 
135
            value2 = ((Attributable)item2).getAttribute(getAttributeName());
 
136
        }
 
137
        if (value2 != null) {
 
138
            colour2 = getColourForValue(value2);
 
139
            paint = colour2;
 
140
        }
 
141
 
 
142
        fillColour1 = null;
 
143
        if (colour1 != null) {
 
144
            fillColour1 = getLighterColour(colour1);
 
145
 
 
146
        }
 
147
 
 
148
        fillColour2 = null;
 
149
        if (colour2 != null) {
 
150
            fillColour2 = getLighterColour(colour2);
 
151
        }
 
152
 
 
153
 
 
154
    }
 
155
 
 
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);
 
161
            if (value != null) {
 
162
                values.add(value);
 
163
            }
 
164
        }
 
165
 
 
166
        boolean isNumber = true;
 
167
        boolean isInteger = true;
 
168
 
 
169
        for (Object value : values) {
 
170
            if (value instanceof Number) {
 
171
                if (((Number)value).doubleValue() != ((Number)value).intValue()) {
 
172
                    isInteger = false;
 
173
                }
 
174
            } else {
 
175
                isNumber = false;
 
176
            }
 
177
        }
 
178
 
 
179
        if (isNumber && !isInteger) return false;
 
180
 
 
181
        return true;
 
182
    }
 
183
 
 
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);
 
189
            if (value != null) {
 
190
                values.add(value);
 
191
            }
 
192
        }
 
193
 
 
194
        boolean isNumber = true;
 
195
 
 
196
        for (Object value : values) {
 
197
            if (!(value instanceof Number)) {
 
198
                isNumber = false;
 
199
            }
 
200
        }
 
201
 
 
202
        return isNumber;
 
203
    }
 
204
 
 
205
    // Private methods
 
206
    private void setAttributableItem(Attributable item) {
 
207
        paint = null;
 
208
        Object value = item.getAttribute(attributeName);
 
209
 
 
210
        Color colour = getColourForValue(value);
 
211
        if (colour != null) {
 
212
            paint = colour;
 
213
            fillPaint = getLighterColour(colour);
 
214
        } else {
 
215
            paint = null;
 
216
            fillPaint = null;
 
217
        }
 
218
    }
 
219
 
 
220
    protected abstract Color getColourForValue(Object value);
 
221
 
 
222
    protected Color getLighterColour(Color color) {
 
223
        return new Color(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha() / 2);
 
224
    }
 
225
 
 
226
    private String attributeName = null;
 
227
 
 
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;
 
234
 
 
235
}