79faa7feb43cfa2eda7c9e43ccc82e3ff1242d1f
[jalview.git] / forester / java / src / org / forester / phylogeny / data / NodeVisualData.java
1 \r
2 package org.forester.phylogeny.data;\r
3 \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
10 \r
11 import org.forester.phylogeny.data.Property.AppliesTo;\r
12 import org.forester.util.ForesterUtil;\r
13 \r
14 public final class NodeVisualData implements PhylogenyData {\r
15 \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
33     private Font               _font;\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
41 \r
42     public NodeVisualData() {\r
43         init();\r
44     }\r
45 \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
54                            final float size,\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
60         setShape( shape );\r
61         setFillType( fill_type );\r
62         setBorderColor( border_color );\r
63         setFillColor( fill_color );\r
64         setSize( size );\r
65         setTransparancy( transparancy );\r
66     }\r
67 \r
68     @Override\r
69     public final StringBuffer asSimpleText() {\r
70         return asText();\r
71     }\r
72 \r
73     @Override\r
74     public final StringBuffer asText() {\r
75         final StringBuffer sb = new StringBuffer();\r
76         return sb;\r
77     }\r
78 \r
79     @Override\r
80     public final PhylogenyData copy() {\r
81         return new NodeVisualData( !ForesterUtil.isEmpty( getFontName() ) ? new String( getFontName() ) : null,\r
82                                    getFontStyle(),\r
83                                    getFontSize(),\r
84                                    getFontColor() != null ? new Color( getFontColor().getRed(), getFontColor()\r
85                                            .getGreen(), getFontColor().getBlue() ) : null,\r
86                                    getShape(),\r
87                                    getFillType(),\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
92                                    getSize(),\r
93                                    getTransparancy() );\r
94     }\r
95 \r
96     public final Color getBorderColor() {\r
97         return _border_color;\r
98     }\r
99 \r
100     public final Color getFillColor() {\r
101         return _fill_color;\r
102     }\r
103 \r
104     public final NodeFill getFillType() {\r
105         return _fill_type;\r
106     }\r
107 \r
108     public final Font getFont() {\r
109         if ( _font != null ) {\r
110             return _font;\r
111         }\r
112         else if ( !ForesterUtil.isEmpty( getFontName() ) ) {\r
113             _font = new Font( getFontName(), getFontStyleInt(), getFontSize() );\r
114             return _font;\r
115         }\r
116         return null;\r
117     }\r
118 \r
119     public final Color getFontColor() {\r
120         return _font_color;\r
121     }\r
122 \r
123     public final String getFontName() {\r
124         return _font_name;\r
125     }\r
126 \r
127     public final byte getFontSize() {\r
128         return _font_size;\r
129     }\r
130 \r
131     public final FontType getFontStyle() {\r
132         return _font_style;\r
133     }\r
134 \r
135     public final int getFontStyleInt() {\r
136         if ( getFontStyle() == FontType.BOLD ) {\r
137             return Font.BOLD;\r
138         }\r
139         else if ( getFontStyle() == FontType.ITALIC ) {\r
140             return Font.ITALIC;\r
141         }\r
142         else if ( getFontStyle() == FontType.BOLD_ITALIC ) {\r
143             return Font.BOLD + Font.ITALIC;\r
144         }\r
145         return Font.PLAIN;\r
146     }\r
147 \r
148     public final NodeShape getShape() {\r
149         return _shape;\r
150     }\r
151 \r
152     public final float getSize() {\r
153         return _size;\r
154     }\r
155 \r
156     public final float getTransparancy() {\r
157         return _transparancy;\r
158     }\r
159 \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
165     }\r
166 \r
167     @Override\r
168     public final boolean isEqual( final PhylogenyData data ) {\r
169         throw new UnsupportedOperationException();\r
170     }\r
171 \r
172     public void parseProperty( final Property prop ) {\r
173         if ( prop.getRef().equals( FONT_REF ) ) {\r
174             setFontName( prop.getValue().trim() );\r
175         }\r
176         else if ( prop.getRef().equals( FONT_SIZE_REF ) ) {\r
177             int s = -1;\r
178             try {\r
179                 s = Integer.parseInt( prop.getValue() );\r
180             }\r
181             catch ( final NumberFormatException e ) {\r
182                 return;\r
183             }\r
184             if ( ( s >= 0 ) && ( s < Byte.MAX_VALUE ) ) {\r
185                 setFontSize( s );\r
186             }\r
187         }\r
188         else if ( prop.getRef().equals( FONT_STYLE_REF ) ) {\r
189             setFontStyle( prop.getValue() );\r
190         }\r
191     }\r
192 \r
193     public final void setBorderColor( final Color border_color ) {\r
194         _border_color = border_color;\r
195     }\r
196 \r
197     public final void setFillColor( final Color fill_color ) {\r
198         _fill_color = fill_color;\r
199     }\r
200 \r
201     public final void setFillType( final NodeFill fill_type ) {\r
202         _fill_type = fill_type;\r
203     }\r
204 \r
205     public final void setFontColor( final Color font_color ) {\r
206         _font_color = font_color;\r
207     }\r
208 \r
209     public final void setFontName( final String font_name ) {\r
210         if ( !ForesterUtil.isEmpty( font_name ) ) {\r
211             _font_name = font_name;\r
212         }\r
213         else {\r
214             _font_name = null;\r
215         }\r
216         _font = null;\r
217     }\r
218 \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
222         }\r
223         else if ( font_size > Byte.MAX_VALUE ) {\r
224             throw new IllegalArgumentException( "font size is too large: " + font_size );\r
225         }\r
226         _font_size = ( byte ) font_size;\r
227         _font = null;\r
228     }\r
229 \r
230     public final void setFontStyle( final FontType font_style ) {\r
231         _font_style = font_style;\r
232         _font = null;\r
233     }\r
234 \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
238         }\r
239         else if ( font_style == Font.ITALIC ) {\r
240             setFontStyle( FontType.ITALIC );\r
241         }\r
242         else if ( font_style == Font.BOLD ) {\r
243             setFontStyle( FontType.BOLD );\r
244         }\r
245         else {\r
246             setFontStyle( FontType.PLAIN );\r
247         }\r
248     }\r
249 \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
253         }\r
254         else if ( font_style.equalsIgnoreCase( FONT_STYLE_ITALIC ) ) {\r
255             setFontStyle( FontType.ITALIC );\r
256         }\r
257         else if ( font_style.equalsIgnoreCase( FONT_STYLE_BOLD_ITALIC ) ) {\r
258             setFontStyle( FontType.BOLD_ITALIC );\r
259         }\r
260         else {\r
261             setFontStyle( FontType.PLAIN );\r
262         }\r
263     }\r
264 \r
265     public final void setShape( final NodeShape shape ) {\r
266         _shape = shape;\r
267     }\r
268 \r
269     public final void setSize( final float size ) {\r
270         _size = size;\r
271     }\r
272 \r
273     public final void setTransparancy( final float transparancy ) {\r
274         _transparancy = transparancy;\r
275     }\r
276 \r
277     @Override\r
278     public final StringBuffer toNHX() {\r
279         throw new UnsupportedOperationException();\r
280     }\r
281 \r
282     @Override\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
286         }\r
287     }\r
288 \r
289     @Override\r
290     public final String toString() {\r
291         return asText().toString();\r
292     }\r
293 \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
305         _font = null;\r
306     }\r
307 \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
312         }\r
313         if ( getFontSize() != DEFAULT_FONT_SIZE ) {\r
314             properties.add( new Property( FONT_SIZE_REF,\r
315                                           String.valueOf( getFontSize() ),\r
316                                           "",\r
317                                           FONT_SIZE_TYPE,\r
318                                           AppliesTo.NODE ) );\r
319         }\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
324             }\r
325             else if ( getFontStyle() == FontType.BOLD ) {\r
326                 font_style = FONT_STYLE_BOLD;\r
327             }\r
328             else if ( getFontStyle() == FontType.BOLD_ITALIC ) {\r
329                 font_style = FONT_STYLE_BOLD_ITALIC;\r
330             }\r
331             properties.add( new Property( FONT_STYLE_REF, font_style, "", FONT_STYLE_TYPE, AppliesTo.NODE ) );\r
332         }\r
333         return properties;\r
334     }\r
335 \r
336     public enum FontType {\r
337         BOLD, BOLD_ITALIC, ITALIC, PLAIN\r
338     }\r
339 \r
340     public enum NodeFill {\r
341         DEFAULT, GRADIENT, NONE, SOLID\r
342     }\r
343 \r
344     public enum NodeShape {\r
345         CIRCLE, DEFAULT, RECTANGLE\r
346     }\r
347 }\r