2785daf23f0d47cffa5dcbd01f6b28edf4505782
[jalview.git] / forester / java / src / org / forester / archaeopteryx / phylogeny / data / RenderableVector.java
1 // $Id:
2 // $
3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
5 //
6 // Copyright (C) 2008-2009 Christian M. Zmasek
7 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
8 // All rights reserved
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 //
24 // Contact: phylosoft @ gmail . com
25 // WWW: www.phylosoft.org/forester
26
27 package org.forester.archaeopteryx.phylogeny.data;
28
29 import java.awt.Color;
30 import java.awt.Dimension;
31 import java.awt.Graphics2D;
32 import java.awt.geom.Rectangle2D;
33 import java.io.IOException;
34 import java.io.Writer;
35 import java.util.List;
36
37 import org.forester.archaeopteryx.Configuration;
38 import org.forester.archaeopteryx.TreePanel;
39 import org.forester.phylogeny.data.PhylogenyData;
40 import org.forester.util.DescriptiveStatistics;
41 import org.forester.util.ForesterUtil;
42
43 public final class RenderableVector implements RenderablePhylogenyData {
44
45     final static public int         DEFAULT_HEIGHT          = 12;
46     final static public int         DEFAULT_WIDTH           = 120;
47     private double                  _rendering_factor_width = 1.0;
48     private List<Double>            _values;
49     private final Rectangle2D       _rectangle              = new Rectangle2D.Float();
50     private Configuration           _configuration;
51     private double                  _height;
52     private double                  _min;
53     private double                  _max;
54     private double                  _mean;
55     private static RenderableVector _instance               = null;
56
57     public static RenderableVector createInstance( final List<Double> values,
58                                                    final DescriptiveStatistics stats,
59                                                    final Configuration configuration ) {
60         if ( _instance == null ) {
61             _instance = new RenderableVector();
62         }
63         _instance.setRenderingHeight( DEFAULT_HEIGHT );
64         _instance._values = values;
65         _instance._configuration = configuration;
66         _instance._min = stats.getMin();
67         _instance._max = stats.getMax();
68         _instance._mean = stats.arithmeticMean();
69         return _instance;
70     }
71
72     @Override
73     public Object clone() {
74         throw new NoSuchMethodError();
75     }
76
77     private RenderableVector() {
78         _values = null;
79         _configuration = null;
80     }
81
82     @Override
83     public StringBuffer asSimpleText() {
84         return new StringBuffer( _values.toString() );
85     }
86
87     @Override
88     public StringBuffer asText() {
89         return asSimpleText();
90     }
91
92     public double getRenderingFactorWidth() {
93         return _rendering_factor_width;
94     }
95
96     public int getTotalLength() {
97         return ( int ) ( _values.size() * getRenderingHeight() );
98     }
99
100     @Override
101     public boolean isEqual( final PhylogenyData data ) {
102         throw new NoSuchMethodError();
103     }
104
105     @Override
106     public void render( final double x1,
107                         final double y1,
108                         final Graphics2D g,
109                         final TreePanel tree_panel,
110                         final boolean to_pdf ) {
111         final double y = y1;
112         final double start = x1 + 20.0;
113         final double width = ( double ) DEFAULT_WIDTH / _values.size();
114         for( int i = 0; i < _values.size(); ++i ) {
115             g.setColor( calculateColor( _values.get( i ) ) );
116             _rectangle.setFrame( start + i * width, y - 0.5, width, getRenderingHeight() );
117             g.fill( _rectangle );
118         }
119     }
120
121     private Color calculateColor( final double v ) {
122         return ForesterUtil.calcColor( v, _min, _max, _mean, Color.MAGENTA, Color.GREEN, Color.WHITE );
123     }
124
125     public void setRenderingFactorWidth( final double rendering_factor_width ) {
126         _rendering_factor_width = rendering_factor_width;
127     }
128
129     @Override
130     public StringBuffer toNHX() {
131         throw new NoSuchMethodError();
132     }
133
134     @Override
135     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
136         throw new NoSuchMethodError();
137     }
138
139     @Override
140     public PhylogenyData copy() {
141         throw new NoSuchMethodError();
142     }
143
144     @Override
145     public Dimension getOriginalSize() {
146         return new Dimension( getTotalLength(), ( int ) getRenderingHeight() );
147     }
148
149     @Override
150     public Object getParameter() {
151         return null;
152     }
153
154     @Override
155     public Dimension getRenderingSize() {
156         return getOriginalSize();
157     }
158
159     @Override
160     public void setParameter( final double parameter ) {
161         throw new NoSuchMethodError();
162     }
163
164     @Override
165     public void setRenderingHeight( final double height ) {
166         _height = height;
167     }
168
169     private double getRenderingHeight() {
170         return _height;
171     }
172 }