KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > jcharts > chartData > processors > TextProcessor


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

39
40 package org.krysalis.jcharts.chartData.processors;
41
42
43 import org.krysalis.jcharts.chartText.TextTag;
44
45 import java.awt.*;
46 import java.awt.font.FontRenderContext JavaDoc;
47 import java.util.ArrayList JavaDoc;
48
49
50 /*******************************************************************************************
51  *
52  *
53  *******************************************************************************************/

54 public class TextProcessor
55 {
56     private float widestLabel = Float.MIN_VALUE;
57     private float tallestLabel = Float.MIN_VALUE;
58     private float totalLabelWidths = 0.0f;
59
60     private ArrayList JavaDoc textTags;
61
62
63     /******************************************************************************************
64      * Constructor
65      *
66      *******************************************************************************************/

67     public TextProcessor()
68     {
69         this.textTags = new ArrayList JavaDoc( 30 );
70     }
71
72
73     /******************************************************************************************
74      *
75      * @param label
76      * @param font
77      * @param fontRenderContext
78      *******************************************************************************************/

79     public void addLabel( String JavaDoc label, Font font, FontRenderContext JavaDoc fontRenderContext )
80     {
81         TextTag textTag= new TextTag( label, font, fontRenderContext );
82         this.textTags.add( textTag );
83
84         this.widestLabel = Math.max( textTag.getWidth(), this.widestLabel );
85         this.totalLabelWidths += textTag.getWidth();
86
87         this.tallestLabel = Math.max( textTag.getHeight(), this.tallestLabel );
88     }
89
90
91     /******************************************************************************************
92      * Returns the number of labels
93      *
94      * @return int
95      ******************************************************************************************/

96     public int size()
97     {
98         return this.textTags.size();
99     }
100
101
102     /******************************************************************************************
103      * Calculates the width and height needed by the passed String when rendered
104      *
105      // * @param iDataSeries
106      / * @param font
107      // * @param fontRenderContext
108      *******************************************************************************************
109      public void processLabels( IDataSeries iDataSeries) {
110      //LOOP
111      for( int i = 0; i < iDataSeries.getNumberOfXAxisLabels(); i++ )
112      {
113      this.axisLabelProcessor.processLabel( iDataSeries.getXAxisLabel( i ), axisLabelFont, fontRenderContext );
114      }
115
116
117      TextLayout textLayout = new TextLayout( label, font, fontRenderContext );
118
119      //---WIDTH
120      this.labelWidths[ counter ] = textLayout.getAdvance();
121      this.widestLabel = Math.max( this.labelWidths[ counter ], this.widestLabel );
122      this.totalLabelWidths += this.labelWidths[ counter ];
123
124      //---HEIGHT
125      this.labelHeights[ counter ] = textLayout.getAscent() + textLayout.getDescent();
126      this.tallestLabel = Math.max( this.labelHeights[ counter ], this.tallestLabel );
127
128      //---need this to offset font rendering, as rendering is at the baseline not bottom or top,
129      this.fontDescent = textLayout.getDescent();
130
131      this.counter++;
132      }
133
134
135      /******************************************************************************************
136      *
137      *
138      ******************************************************************************************/

139     public float getWidestLabel()
140     {
141         return this.widestLabel;
142     }
143
144
145     /******************************************************************************************
146      *
147      *
148      ******************************************************************************************/

149     public float getTallestLabel()
150     {
151         return this.tallestLabel;
152     }
153
154
155     /******************************************************************************************
156      *
157      *
158      ******************************************************************************************/

159     public float getTotalLabelWidths()
160     {
161         return this.totalLabelWidths;
162     }
163
164
165     /******************************************************************************************
166      *
167      * @param index
168      * @return TextTag
169      ******************************************************************************************/

170     public TextTag getTextTag( int index )
171     {
172         return (TextTag) this.textTags.get( index );
173     }
174 }
175
Popular Tags