KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > jcharts > chartText > TextTagGroup


1 /***********************************************************************************************
2  * Copyright 2002 (C) Nathaniel G. Auvil. All Rights Reserved.
3  *
4  * Redistribution and use of this software and associated documentation ("Software"), with or
5  * without modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain copyright statements and notices.
8  * Redistributions must also contain a copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
11  * conditions and the following disclaimer in the documentation and/or other materials
12  * provided with the distribution.
13  *
14  * 3. The name "jCharts" or "Nathaniel G. Auvil" must not be used to endorse or promote
15  * products derived from this Software without prior written permission of Nathaniel G.
16  * Auvil. For written permission, please contact nathaniel_auvil@users.sourceforge.net
17  *
18  * 4. Products derived from this Software may not be called "jCharts" nor may "jCharts" appear
19  * in their names without prior written permission of Nathaniel G. Auvil. jCharts is a
20  * registered trademark of Nathaniel G. Auvil.
21  *
22  * 5. Due credit should be given to the jCharts Project (http://jcharts.sourceforge.net/).
23  *
24  * THIS SOFTWARE IS PROVIDED BY Nathaniel G. Auvil AND CONTRIBUTORS ``AS IS'' AND ANY
25  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * jCharts OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
32  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
33  ************************************************************************************************/

34
35 package org.krysalis.jcharts.chartText;
36
37
38 import org.krysalis.jcharts.properties.util.ChartFont;
39 import org.krysalis.jcharts.test.HTMLGenerator;
40 import org.krysalis.jcharts.test.HTMLTestable;
41
42 import java.awt.*;
43 import java.awt.font.FontRenderContext JavaDoc;
44 import java.io.Serializable JavaDoc;
45 import java.lang.reflect.Field JavaDoc;
46 import java.util.ArrayList JavaDoc;
47
48
49 /*************************************************************************************
50  *
51  * @author Nathaniel Auvil
52  * @version $Id: TextTagGroup.java,v 1.1 2003/05/17 16:58:32 nathaniel_auvil Exp $
53  ************************************************************************************/

54 public class TextTagGroup implements HTMLTestable, Serializable JavaDoc
55 {
56     private float widestLabel = 0.0f;
57     private float tallestLabel = 0.0f;
58     private float totalLabelWidths = 0.0f;
59
60     private ArrayList JavaDoc textTags;
61
62     private ChartFont chartFont;
63     private FontRenderContext JavaDoc fontRenderContext;
64
65
66     //---Strangely, we don't need to store the text itself. (yet)
67

68 // private ArrayList group;
69
// private Font baseFont;
70
// private Font derivedFont;
71
// private Paint fontColor;
72

73 // private float tallestTextTag = 0.0f;
74
// private float widestTextTag = 0.0f;
75
// private boolean computedTallestAndWidest = false;
76

77
78     /******************************************************************************************
79      * Constructor
80      *
81      * @param chartFont
82      * @param fontRenderContext
83      *******************************************************************************************/

84     public TextTagGroup( ChartFont chartFont, FontRenderContext JavaDoc fontRenderContext )
85     {
86         this.textTags = new ArrayList JavaDoc( 30 );
87
88         this.chartFont = chartFont;
89         this.fontRenderContext = fontRenderContext;
90     }
91
92
93     /******************************************************************************************
94      *
95      * @param label
96      *******************************************************************************************/

97     public void addLabel( String JavaDoc label )
98     {
99         TextTag textTag = new TextTag( label, this.chartFont.getFont(), this.fontRenderContext );
100         this.textTags.add( textTag );
101
102         this.widestLabel = Math.max( textTag.getWidth(), this.widestLabel );
103         this.totalLabelWidths += textTag.getWidth();
104
105         this.tallestLabel = Math.max( textTag.getHeight(), this.tallestLabel );
106     }
107
108
109     /******************************************************************************************
110      * Returns the number of labels
111      *
112      * @return int
113      ******************************************************************************************/

114     public int size()
115     {
116         return this.textTags.size();
117     }
118
119
120     /*******************************************************************************************
121      * Horizontal plots render the data from top down so rendering alogorithm needs to get the
122      * labels in reverse order to make things easier.
123      *
124      *******************************************************************************************/

125     public void reverse()
126     {
127         ArrayList JavaDoc reverse = new ArrayList JavaDoc( this.textTags.size() );
128         for( int i = this.textTags.size() - 1; i >= 0; i-- )
129         {
130             reverse.add( this.textTags.get( i ) );
131         }
132
133         this.textTags = reverse;
134     }
135
136
137     /******************************************************************************************
138      * Calculates the width and height needed by the passed String when rendered
139      *
140      // * @param iDataSeries
141      / * @param font
142      // * @param fontRenderContext
143      *******************************************************************************************
144      public void processLabels( IDataSeries iDataSeries) {
145      //LOOP
146      for( int i = 0; i < iDataSeries.getNumberOfXAxisLabels(); i++ )
147      {
148      this.axisLabelProcessor.processLabel( iDataSeries.getXAxisLabel( i ), axisLabelFont, fontRenderContext );
149      }
150
151
152      TextLayout textLayout = new TextLayout( label, font, fontRenderContext );
153
154      //---WIDTH
155      this.labelWidths[ counter ] = textLayout.getAdvance();
156      this.widestLabel = Math.max( this.labelWidths[ counter ], this.widestLabel );
157      this.totalLabelWidths += this.labelWidths[ counter ];
158
159      //---HEIGHT
160      this.labelHeights[ counter ] = textLayout.getAscent() + textLayout.getDescent();
161      this.tallestLabel = Math.max( this.labelHeights[ counter ], this.tallestLabel );
162
163      //---need this to offset font rendering, as rendering is at the baseline not bottom or top,
164      this.fontDescent = textLayout.getDescent();
165
166      this.counter++;
167      }
168
169
170      /******************************************************************************************
171      *
172      *
173      ******************************************************************************************/

174     public float getWidestLabel()
175     {
176         return this.widestLabel;
177     }
178
179
180     /******************************************************************************************
181      *
182      *
183      ******************************************************************************************/

184     public float getTallestLabel()
185     {
186         return this.tallestLabel;
187     }
188
189
190     /******************************************************************************************
191      *
192      *
193      ******************************************************************************************/

194     public float getTotalLabelWidths()
195     {
196         return this.totalLabelWidths;
197     }
198
199
200     /******************************************************************************************
201      *
202      * @param index
203      * @return TextTag
204      ******************************************************************************************/

205     public TextTag getTextTag( int index )
206     {
207         return (TextTag) this.textTags.get( index );
208     }
209
210
211     /**********************************************************************************************
212      * Renders the text, at the position - renders from the top (instead of baseline)
213      *
214      * @param index
215      * @param g2d
216      * @param x
217      * @param y
218      **********************************************************************************************/

219     public void render( int index, Graphics2D g2d, float x, float y )
220     {
221         this.chartFont.setupGraphics2D( g2d );
222         this.getTextTag( index ).render( g2d, x, y );
223     }
224
225
226     /**********************************************************************************************
227      * Renders all the text in this group.
228      *
229      *
230      **********************************************************************************************
231      public void renderTag( int index, Graphics2D g2d )
232      {
233      TextTag tag;
234      //Paint p = this.fontColor;
235
236      g2d.setPaint( this. );
237
238      //g2d.setFont( (this.derivedFont == null) ? baseFont : derivedFont );
239
240      for( int i = 0; i < group.size(); i++ )
241      {
242      tag = ( TextTag ) group.get( i );
243      tag.render( g2d, p );
244      // Text tag sets the Paint every time if not null
245      // So, we might save a small bit of time by changing
246      // this to null after first time.
247      p = null;
248      }
249      }
250
251
252      /*********************************************************************************************
253      * Enables the testing routines to display the contents of this Object.
254      *
255      * @param htmlGenerator
256      **********************************************************************************************/

257     public void toHTML( HTMLGenerator htmlGenerator )
258     {
259         htmlGenerator.propertiesTableStart( this.getClass().getName() );
260
261         Field JavaDoc[] fields = this.getClass().getDeclaredFields();
262         for( int i = 0; i < fields.length; i++ )
263         {
264             try
265             {
266                 htmlGenerator.addField( fields[ i ].getName(), fields[ i ].get( this ) );
267             }
268             catch( IllegalAccessException JavaDoc illegalAccessException )
269             {
270                 illegalAccessException.printStackTrace();
271             }
272         }
273
274         htmlGenerator.propertiesTableEnd();
275     }
276
277 }
Popular Tags