2 package org.forester.phylogeny.data;
\r
4 import java.awt.Color;
\r
5 import java.awt.Font;
\r
6 import java.io.IOException;
\r
7 import java.io.Writer;
\r
8 import java.util.ArrayList;
\r
9 import java.util.List;
\r
11 import org.forester.phylogeny.data.Property.AppliesTo;
\r
12 import org.forester.util.ForesterUtil;
\r
14 public final class NodeVisualData implements PhylogenyData {
\r
16 public static final String APTX_VISUALIZATION_REF = "aptx_visualization:";
\r
17 public static final String FONT_REF = APTX_VISUALIZATION_REF + "node_font";
\r
18 public static final String FONT_SIZE_REF = APTX_VISUALIZATION_REF + "node_font_size";
\r
19 public static final String FONT_SIZE_TYPE = "xsd:unsignedByte";
\r
20 public static final String FONT_STYLE_BOLD = "bold";
\r
21 public static final String FONT_STYLE_BOLD_ITALIC = "bold_italic";
\r
22 public static final String FONT_STYLE_ITALIC = "italic";
\r
23 public static final String FONT_STYLE_PLAIN = "plain";
\r
24 public static final String FONT_STYLE_REF = APTX_VISUALIZATION_REF + "node_font_style";
\r
25 public static final String FONT_STYLE_TYPE = "xsd:token";
\r
26 public static final String FONT_TYPE = "xsd:token";
\r
27 private static final byte DEFAULT_FONT_SIZE = -1;
\r
28 private static final int DEFAULT_SIZE = -1;
\r
29 private static final int DEFAULT_TRANSPARANCY = -1;
\r
30 private Color _border_color;
\r
31 private Color _fill_color;
\r
32 private NodeFill _fill_type;
\r
34 private Color _font_color;
\r
35 private String _font_name;
\r
36 private byte _font_size;
\r
37 private FontType _font_style;
\r
38 private NodeShape _shape;
\r
39 private float _size;
\r
40 private float _transparancy;
\r
42 public NodeVisualData() {
\r
46 public NodeVisualData( final String font_name,
\r
47 final FontType font_style,
\r
48 final byte font_size,
\r
49 final Color font_color,
\r
50 final NodeShape shape,
\r
51 final NodeFill fill_type,
\r
52 final Color border_color,
\r
53 final Color fill_color,
\r
55 final float transparancy ) {
\r
56 setFontName( font_name );
\r
57 setFontStyle( font_style );
\r
58 setFontSize( font_size );
\r
59 setFontColor( font_color );
\r
61 setFillType( fill_type );
\r
62 setBorderColor( border_color );
\r
63 setFillColor( fill_color );
\r
65 setTransparancy( transparancy );
\r
69 public final StringBuffer asSimpleText() {
\r
74 public final StringBuffer asText() {
\r
75 final StringBuffer sb = new StringBuffer();
\r
80 public final PhylogenyData copy() {
\r
81 return new NodeVisualData( !ForesterUtil.isEmpty( getFontName() ) ? new String( getFontName() ) : null,
\r
84 getFontColor() != null ? new Color( getFontColor().getRed(), getFontColor()
\r
85 .getGreen(), getFontColor().getBlue() ) : null,
\r
88 getBorderColor() != null ? new Color( getBorderColor().getRed(), getBorderColor()
\r
89 .getGreen(), getBorderColor().getBlue() ) : null,
\r
90 getFillColor() != null ? new Color( getFillColor().getRed(), getFillColor()
\r
91 .getGreen(), getFillColor().getBlue() ) : null,
\r
93 getTransparancy() );
\r
96 public final Color getBorderColor() {
\r
97 return _border_color;
\r
100 public final Color getFillColor() {
\r
101 return _fill_color;
\r
104 public final NodeFill getFillType() {
\r
108 public final Font getFont() {
\r
109 if ( _font != null ) {
\r
112 else if ( !ForesterUtil.isEmpty( getFontName() ) ) {
\r
113 _font = new Font( getFontName(), getFontStyleInt(), getFontSize() );
\r
119 public final Color getFontColor() {
\r
120 return _font_color;
\r
123 public final String getFontName() {
\r
127 public final byte getFontSize() {
\r
131 public final FontType getFontStyle() {
\r
132 return _font_style;
\r
135 public final int getFontStyleInt() {
\r
136 if ( getFontStyle() == FontType.BOLD ) {
\r
139 else if ( getFontStyle() == FontType.ITALIC ) {
\r
140 return Font.ITALIC;
\r
142 else if ( getFontStyle() == FontType.BOLD_ITALIC ) {
\r
143 return Font.BOLD + Font.ITALIC;
\r
148 public final NodeShape getShape() {
\r
152 public final float getSize() {
\r
156 public final float getTransparancy() {
\r
157 return _transparancy;
\r
160 public final boolean isEmpty() {
\r
161 return ( ForesterUtil.isEmpty( getFontName() ) && ( getFontStyle() == FontType.PLAIN )
\r
162 && ( getFontSize() == DEFAULT_FONT_SIZE ) && ( getFontColor() == null )
\r
163 && ( getShape() == NodeShape.DEFAULT ) && ( getFillType() == NodeFill.DEFAULT )
\r
164 && ( getBorderColor() == null ) && ( getFillColor() == null ) && ( getSize() == DEFAULT_SIZE ) && ( getTransparancy() == DEFAULT_TRANSPARANCY ) );
\r
168 public final boolean isEqual( final PhylogenyData data ) {
\r
169 throw new UnsupportedOperationException();
\r
172 public void parseProperty( final Property prop ) {
\r
173 if ( prop.getRef().equals( FONT_REF ) ) {
\r
174 setFontName( prop.getValue().trim() );
\r
176 else if ( prop.getRef().equals( FONT_SIZE_REF ) ) {
\r
179 s = Integer.parseInt( prop.getValue() );
\r
181 catch ( final NumberFormatException e ) {
\r
184 if ( ( s >= 0 ) && ( s < Byte.MAX_VALUE ) ) {
\r
188 else if ( prop.getRef().equals( FONT_STYLE_REF ) ) {
\r
189 setFontStyle( prop.getValue() );
\r
193 public final void setBorderColor( final Color border_color ) {
\r
194 _border_color = border_color;
\r
197 public final void setFillColor( final Color fill_color ) {
\r
198 _fill_color = fill_color;
\r
201 public final void setFillType( final NodeFill fill_type ) {
\r
202 _fill_type = fill_type;
\r
205 public final void setFontColor( final Color font_color ) {
\r
206 _font_color = font_color;
\r
209 public final void setFontName( final String font_name ) {
\r
210 if ( !ForesterUtil.isEmpty( font_name ) ) {
\r
211 _font_name = font_name;
\r
219 public final void setFontSize( final int font_size ) {
\r
220 if ( ( font_size != DEFAULT_FONT_SIZE ) && ( font_size < 0 ) ) {
\r
221 throw new IllegalArgumentException( "negative font size: " + font_size );
\r
223 else if ( font_size > Byte.MAX_VALUE ) {
\r
224 throw new IllegalArgumentException( "font size is too large: " + font_size );
\r
226 _font_size = ( byte ) font_size;
\r
230 public final void setFontStyle( final FontType font_style ) {
\r
231 _font_style = font_style;
\r
235 public final void setFontStyle( final int font_style ) {
\r
236 if ( ( font_style == ( Font.BOLD + Font.ITALIC ) ) ) {
\r
237 setFontStyle( FontType.BOLD_ITALIC );
\r
239 else if ( font_style == Font.ITALIC ) {
\r
240 setFontStyle( FontType.ITALIC );
\r
242 else if ( font_style == Font.BOLD ) {
\r
243 setFontStyle( FontType.BOLD );
\r
246 setFontStyle( FontType.PLAIN );
\r
250 public final void setFontStyle( final String font_style ) {
\r
251 if ( font_style.equalsIgnoreCase( FONT_STYLE_BOLD ) ) {
\r
252 setFontStyle( FontType.BOLD );
\r
254 else if ( font_style.equalsIgnoreCase( FONT_STYLE_ITALIC ) ) {
\r
255 setFontStyle( FontType.ITALIC );
\r
257 else if ( font_style.equalsIgnoreCase( FONT_STYLE_BOLD_ITALIC ) ) {
\r
258 setFontStyle( FontType.BOLD_ITALIC );
\r
261 setFontStyle( FontType.PLAIN );
\r
265 public final void setShape( final NodeShape shape ) {
\r
269 public final void setSize( final float size ) {
\r
273 public final void setTransparancy( final float transparancy ) {
\r
274 _transparancy = transparancy;
\r
278 public final StringBuffer toNHX() {
\r
279 throw new UnsupportedOperationException();
\r
283 public final void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
\r
284 for( final Property p : toProperties() ) {
\r
285 p.toPhyloXML( writer, level, indentation );
\r
290 public final String toString() {
\r
291 return asText().toString();
\r
294 private final void init() {
\r
295 setFontName( null );
\r
296 setFontStyle( FontType.PLAIN );
\r
297 setFontSize( DEFAULT_FONT_SIZE );
\r
298 setFontColor( null );
\r
299 setShape( NodeShape.DEFAULT );
\r
300 setFillType( NodeFill.DEFAULT );
\r
301 setBorderColor( null );
\r
302 setFillColor( null );
\r
303 setSize( DEFAULT_SIZE );
\r
304 setTransparancy( DEFAULT_TRANSPARANCY );
\r
308 private final List<Property> toProperties() {
\r
309 final List<Property> properties = new ArrayList<Property>();
\r
310 if ( !ForesterUtil.isEmpty( getFontName() ) ) {
\r
311 properties.add( new Property( FONT_REF, getFontName(), "", FONT_TYPE, AppliesTo.NODE ) );
\r
313 if ( getFontSize() != DEFAULT_FONT_SIZE ) {
\r
314 properties.add( new Property( FONT_SIZE_REF,
\r
315 String.valueOf( getFontSize() ),
\r
318 AppliesTo.NODE ) );
\r
320 if ( getFontStyle() != FontType.PLAIN ) {
\r
321 String font_style = FONT_STYLE_PLAIN;
\r
322 if ( getFontStyle() == FontType.ITALIC ) {
\r
323 font_style = FONT_STYLE_ITALIC;
\r
325 else if ( getFontStyle() == FontType.BOLD ) {
\r
326 font_style = FONT_STYLE_BOLD;
\r
328 else if ( getFontStyle() == FontType.BOLD_ITALIC ) {
\r
329 font_style = FONT_STYLE_BOLD_ITALIC;
\r
331 properties.add( new Property( FONT_STYLE_REF, font_style, "", FONT_STYLE_TYPE, AppliesTo.NODE ) );
\r
336 public enum FontType {
\r
337 BOLD, BOLD_ITALIC, ITALIC, PLAIN
\r
340 public enum NodeFill {
\r
341 DEFAULT, GRADIENT, NONE, SOLID
\r
344 public enum NodeShape {
\r
345 CIRCLE, DEFAULT, RECTANGLE
\r