KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * LabelFormatterDate.java jchart2d
3  * Copyright (C) Achim Westermann, created on 20.04.2005, 11:56:36
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  */

23 package info.monitorenter.gui.chart.labelformatters;
24
25 import info.monitorenter.gui.chart.ILabelFormatter;
26 import info.monitorenter.util.Range;
27 import info.monitorenter.util.SimpleDateFormatAnalyzer;
28
29 import java.text.SimpleDateFormat JavaDoc;
30 import java.util.Calendar JavaDoc;
31 import java.util.Date JavaDoc;
32
33
34 /**
35  * <p>
36  * An ILabelFormatter instance that uses a {@link java.text.DateFormat} to
37  * format the labels.
38  * </p>
39  * <p>
40  * <b>Caution: <br>
41  * It only makes sense to use this class if the data of the corresponding axis
42  * may be interpreted as long number of milliseconds since the standard base
43  * time known as "the epoch", namely January 1, 1970, 00:00:00 GMT. </b>
44  * <p>
45  * <p>
46  * <b>Caution: <br>
47  * This implentation is not completly conformat with the constraint: <code>
48  * instance.parse(instance.format(value)) == value
49  * </code>
50  * </b> This only works for subsequent call: one call to format contains the
51  * next value to return from parse to be the same as the format. That value is
52  * cached as date / time formatting produces truncation of the internal value
53  * (e.g. if no year is displayed). <br>
54  * Use: <br>
55  *
56  * <pre>
57  *
58  * Chart2D chart = new &lt;Constructor&gt;
59  * Axis axis = new AxisSimple();
60  * axis.setFormatter(new LabelFormatterDate(DateFormat.getDateInstance()));
61  * chart.setAxisX(axis);
62  *
63  * </pre>
64  *
65  * to use this class.
66  * </p>
67  *
68  * @author <a HREF="mailto:Achim.Westermann@gmx.de">Achim Westermann </a>
69  *
70  * @version $Revision: 1.1 $
71  *
72  * @see java.util.Date
73  */

74 public class LabelFormatterDate extends ALabelFormatter implements ILabelFormatter {
75   /** The cached maximum amount of characters that will be used. */
76   private int m_cachedMaxAmountChars = Integer.MAX_VALUE;
77
78   /** The date formatter that is used. */
79   private SimpleDateFormat JavaDoc m_df;
80
81   /** The last value that was formatted - needed for the parse - format contract. */
82   private double m_lastFormatted = 0;
83
84   /**
85    * Creates a formatter that uses the given date format.
86    * <p>
87    *
88    * @param dateFormat
89    * the date format to use.
90    */

91   public LabelFormatterDate(final SimpleDateFormat JavaDoc dateFormat) {
92     super();
93     this.m_df = dateFormat;
94   }
95
96   /**
97    * @see info.monitorenter.gui.chart.ILabelFormatter#format(double)
98    */

99   public String JavaDoc format(final double value) {
100     this.m_lastFormatted = value;
101     return this.m_df.format(new Date JavaDoc((long) value));
102   }
103
104   /**
105    * @see info.monitorenter.gui.chart.ILabelFormatter#getMaxAmountChars()
106    */

107   public int getMaxAmountChars() {
108     Range range = this.getAxis().getRange();
109     double dRange = range.getExtent();
110     if (this.m_cachedMaxAmountChars == Integer.MAX_VALUE) {
111       this.m_cachedMaxAmountChars = this.m_df.format(new Date JavaDoc((long) dRange)).length();
112     }
113     return this.m_cachedMaxAmountChars;
114   }
115
116   /**
117    * @see info.monitorenter.gui.chart.ILabelFormatter#getMinimumValueShiftForChange()
118    */

119   public double getMinimumValueShiftForChange() {
120     double ret = 0;
121     if (SimpleDateFormatAnalyzer.displaysMillisecond(this.m_df)) {
122       ret = 1;
123     } else if (SimpleDateFormatAnalyzer.displaysSecond(this.m_df)) {
124       ret = 1000;
125     } else if (SimpleDateFormatAnalyzer.displaysMinute(this.m_df)) {
126       ret = 60000;
127     } else if (SimpleDateFormatAnalyzer.displaysHour(this.m_df)) {
128       ret = 360000;
129     } else if (SimpleDateFormatAnalyzer.displaysDay(this.m_df)) {
130       ret = 24 * 360000;
131     } else if (SimpleDateFormatAnalyzer.displaysMonth(this.m_df)) {
132       ret = 31 * 24 * 360000;
133     } else {
134       ret = 12 * 31 * 24 * 60000;
135     }
136     return ret;
137   }
138
139   /**
140    * @see info.monitorenter.gui.chart.ILabelFormatter#getNextEvenValue(double, boolean)
141    */

142   public double getNextEvenValue(final double value, final boolean ceiling) {
143     Date JavaDoc d = new Date JavaDoc((long) value);
144     Calendar JavaDoc calendar = Calendar.getInstance();
145     calendar.setTime(d);
146     if (ceiling) {
147       if (!SimpleDateFormatAnalyzer.displaysMillisecond(this.m_df)) {
148         calendar.set(Calendar.MILLISECOND, 0);
149         if (!SimpleDateFormatAnalyzer.displaysSecond(this.m_df)) {
150           calendar.set(Calendar.SECOND, 0);
151           if (!SimpleDateFormatAnalyzer.displaysMinute(this.m_df)) {
152             calendar.set(Calendar.MINUTE, 0);
153             if (!SimpleDateFormatAnalyzer.displaysHour(this.m_df)) {
154               calendar.set(Calendar.HOUR, 0);
155               if (!SimpleDateFormatAnalyzer.displaysDay(this.m_df)) {
156                 calendar.set(Calendar.DAY_OF_YEAR, 0);
157                 if (!SimpleDateFormatAnalyzer.displaysMonth(this.m_df)) {
158                   calendar.set(Calendar.MONTH, 0);
159                   if (!SimpleDateFormatAnalyzer.displaysYear(this.m_df)) {
160                     calendar.set(Calendar.YEAR, 0);
161                   }
162                 }
163               }
164             }
165           }
166         }
167       }
168     } else {
169       if (!SimpleDateFormatAnalyzer.displaysMillisecond(this.m_df)) {
170         calendar.set(Calendar.MILLISECOND, 1000);
171         if (!SimpleDateFormatAnalyzer.displaysSecond(this.m_df)) {
172           calendar.set(Calendar.SECOND, 60);
173           if (!SimpleDateFormatAnalyzer.displaysMinute(this.m_df)) {
174             calendar.set(Calendar.MINUTE, 60);
175             if (!SimpleDateFormatAnalyzer.displaysHour(this.m_df)) {
176               calendar.set(Calendar.HOUR, 24);
177               if (!SimpleDateFormatAnalyzer.displaysDay(this.m_df)) {
178                 calendar.set(Calendar.DAY_OF_YEAR, 365);
179                 if (!SimpleDateFormatAnalyzer.displaysMonth(this.m_df)) {
180                   calendar.set(Calendar.MONTH, 12);
181                   if (!SimpleDateFormatAnalyzer.displaysYear(this.m_df)) {
182                     calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + 1);
183                   }
184                 }
185               }
186             }
187           }
188         }
189       }
190     }
191     return calendar.getTimeInMillis();
192   }
193
194   /**
195    * @see info.monitorenter.gui.chart.ILabelFormatter#parse(java.lang.String)
196    */

197   public Number JavaDoc parse(final String JavaDoc formatted) throws NumberFormatException JavaDoc {
198     return new Double JavaDoc(this.m_lastFormatted);
199   }
200 }
201
Popular Tags