KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > monitorenter > gui > chart > labelformatters > ALabelFormatter


1 /*
2  * AbstractLabelFormatter.java, <enter purpose here>. Copyright (C) 2005 Achim
3  * Westermann, Achim.Westermann@gmx.de
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * If you modify or optimize the code in a useful way please let me know.
20  * Achim.Westermann@gmx.de
21  */

22 package info.monitorenter.gui.chart.labelformatters;
23
24 import info.monitorenter.gui.chart.IAxis;
25 import info.monitorenter.gui.chart.ILabelFormatter;
26 import info.monitorenter.util.Range;
27 import info.monitorenter.util.units.AUnit;
28 import info.monitorenter.util.units.UnitUnchanged;
29
30 /**
31  * A label formatter that is aware of the
32  * {@link info.monitorenter.gui.chart.AAxis} it formats label for.
33  * <p>
34  * This allows to compute the amount of fraction digits needed from the range to
35  * display.
36  * <p>
37  *
38  * @author <a HREF="mailto:Achim.Westermann@gmx.de">Achim Westermann </a>
39  *
40  * @version $Revision: 1.1 $
41  */

42 public abstract class ALabelFormatter implements ILabelFormatter {
43
44   /**
45    * The default unit with the factor 1 that is returned as the default for
46    * {@link #getUnit()}.
47    */

48   public static final AUnit UNIT_UNCHANGED = new UnitUnchanged();
49
50   /** The corresponding axis to format for. */
51   private IAxis m_axis;
52
53   /**
54    * Default constructor.
55    * <p>
56    */

57   protected ALabelFormatter() {
58     // nop
59
}
60
61   /**
62    * Intended for {@link info.monitorenter.gui.chart.AAxis} only.
63    * <p>
64    *
65    * @return Returns the axis.
66    */

67   public IAxis getAxis() {
68     return this.m_axis;
69   }
70
71   /**
72    * Returns the maximum amount of characters that will be returned from
73    * {@link #format(double)}.
74    * <p>
75    *
76    * @return the maximum amount of characters that will be returned from
77    * {@link #format(double)}.
78    */

79   public int getMaxAmountChars() {
80     // find the fractions by using range information:
81
int fractionDigits = 0;
82     Range range = this.m_axis.getRange();
83     double dRange = range.getExtent();
84     if (dRange < 1) {
85       if (dRange == 0) {
86         fractionDigits = 1;
87       } else {
88         // find the power
89
while (dRange < 1) {
90           dRange *= 10;
91           fractionDigits++;
92         }
93       }
94     } else {
95       if (dRange == 0) {
96         dRange = 1;
97       }
98       if (dRange < 10) {
99         fractionDigits = 2;
100       } else if (dRange < 100) {
101         fractionDigits = 1;
102       } else {
103         fractionDigits = 0;
104       }
105     }
106
107     // find integer digits by using longest value:
108
int integerDigits = 0;
109     double max = range.getMax();
110     double min = Math.abs(range.getMin());
111     if (max == 0 && min == 0) {
112       integerDigits = 1;
113     } else if (max < min) {
114       while (min > 1) {
115         min /= 10;
116         integerDigits++;
117       }
118     } else {
119       while (max > 1) {
120         max /= 10;
121         integerDigits++;
122       }
123     }
124
125     // <sign> integerDigits <dot> fractionDigits:
126
return 1 + integerDigits + 1 + fractionDigits;
127   }
128
129   /**
130    * Returns {@link #UNIT_UNCHANGED}.
131    * <p>
132    *
133    * @return {@link #UNIT_UNCHANGED}
134    *
135    * @see info.monitorenter.gui.chart.ILabelFormatter#getUnit()
136    */

137   public AUnit getUnit() {
138
139     return ALabelFormatter.UNIT_UNCHANGED;
140   }
141
142   /**
143    * Void adapter method implementation - optional to override.
144    * <p>
145    *
146    * @see info.monitorenter.gui.chart.ILabelFormatter#initPaintIteration()
147    */

148   public void initPaintIteration() {
149     // nop adapter
150
}
151
152   /**
153    * Intended for {@link info.monitorenter.gui.chart.AAxis} only.
154    * <p>
155    *
156    * Do never invoke this! This is only public for package sorting reasons.
157    * <p>
158    *
159    * @param axis
160    * The m_axis to set.
161    */

162   public void setAxis(final IAxis axis) {
163     this.m_axis = axis;
164   }
165
166 }
167
Popular Tags