removed applet code
[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: https://sites.google.com/site/cmzmasek/home/software/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.AptxUtil;
38 import org.forester.archaeopteryx.Configuration;
39 import org.forester.archaeopteryx.TreePanel;
40 import org.forester.phylogeny.data.PhylogenyData;
41 import org.forester.util.DescriptiveStatistics;
42 import org.forester.util.ForesterUtil;
43
44 public final class RenderableVector implements RenderablePhylogenyData {
45
46     final static int                VECTOR_DEFAULT_HEIGHT   = 12;
47     public final static int         VECTOR_DEFAULT_WIDTH    = 120;
48     private double                  _rendering_factor_width = 1.0;
49     private List<Double>            _values;
50     private final Rectangle2D       _rectangle              = new Rectangle2D.Float();
51     private double                  _height                 = VECTOR_DEFAULT_HEIGHT;
52     private double                  _min;
53     private double                  _max;
54     private double                  _mean;
55     private Color                   _min_color              = Color.BLUE;
56     private Color                   _max_color              = Color.YELLOW;
57     private Color                   _mean_color             = Color.WHITE;
58     private int                     _width                  = VECTOR_DEFAULT_WIDTH;
59     private static RenderableVector _instance               = null;
60
61     private RenderableVector() {
62         _values = null;
63     }
64
65     @Override
66     public StringBuffer asSimpleText() {
67         return new StringBuffer( _values.toString() );
68     }
69
70     @Override
71     public StringBuffer asText() {
72         return asSimpleText();
73     }
74
75     @Override
76     public Object clone() {
77         throw new NoSuchMethodError();
78     }
79
80     @Override
81     public PhylogenyData copy() {
82         throw new NoSuchMethodError();
83     }
84
85     @Override
86     public Dimension getOriginalSize() {
87         return new Dimension( getTotalLength(), ( int ) getRenderingHeight() );
88     }
89
90     @Override
91     public Object getParameter() {
92         return null;
93     }
94
95     public double getRenderingFactorWidth() {
96         return _rendering_factor_width;
97     }
98
99     @Override
100     public Dimension getRenderingSize() {
101         return getOriginalSize();
102     }
103
104     public int getTotalLength() {
105         return ( int ) ( _values.size() * getRenderingHeight() );
106     }
107
108     @Override
109     public boolean isEqual( final PhylogenyData data ) {
110         throw new NoSuchMethodError();
111     }
112
113     @Override
114     public void render( final float x1,
115                         final float y1,
116                         final Graphics2D g,
117                         final TreePanel tree_panel,
118                         final boolean to_pdf ) {
119         final double y = y1;
120         final double start = x1 + 20.0;
121         final double width = ( double ) _width / _values.size();
122         for( int i = 0; i < _values.size(); ++i ) {
123             g.setColor( calculateColor( _values.get( i ) ) );
124             _rectangle.setFrame( start + ( i * width ), y - 0.5, width + 1, getRenderingHeight() );
125             g.fill( _rectangle );
126         }
127     }
128
129     @Override
130     public void setParameter( final double parameter ) {
131         throw new NoSuchMethodError();
132     }
133
134     public void setRenderingFactorWidth( final double rendering_factor_width ) {
135         _rendering_factor_width = rendering_factor_width;
136     }
137
138     @Override
139     public void setRenderingHeight( final float height ) {
140         _height = height;
141     }
142
143     @Override
144     public StringBuffer toNHX() {
145         throw new NoSuchMethodError();
146     }
147
148     @Override
149     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
150         throw new NoSuchMethodError();
151     }
152
153     private Color calculateColor( final double v ) {
154         return ForesterUtil.calcColor( v, _min, _max, _mean, _min_color, _max_color, _mean_color );
155     }
156
157     private double getRenderingHeight() {
158         return _height;
159     }
160
161     public static RenderableVector createInstance( final List<Double> values,
162                                                    final DescriptiveStatistics stats,
163                                                    final Configuration configuration ) {
164         if ( _instance == null ) {
165             _instance = new RenderableVector();
166         }
167         _instance._values = values;
168         if ( configuration != null ) {
169             _instance._min_color = configuration.getVectorDataMinColor();
170             _instance._max_color = configuration.getVectorDataMaxColor();
171             _instance._mean_color = configuration.getVectorDataMeanColor();
172             _instance._width = configuration.getVectorDataWidth();
173             _instance._height = configuration.getVectorDataHeight();
174         }
175         if ( stats.getN() > 0 ) {
176             _instance._min = stats.getMin();
177             _instance._max = stats.getMax();
178             _instance._mean = stats.arithmeticMean();
179         }
180         else {
181             _instance._min = 0;
182             _instance._max = 0;
183             _instance._mean = 0;
184             AptxUtil.printWarningMessage( "Archaeopteryx", "creating renderable vector with empty statistics" );
185         }
186         return _instance;
187     }
188 }