KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > write > NumberFormats


1 /*********************************************************************
2 *
3 * Copyright (C) 2002 Andrew Khan
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ***************************************************************************/

19
20 package jxl.write;
21
22 import jxl.format.Format;
23 import jxl.biff.DisplayFormat;
24
25 /**
26  * Static class which contains the available list of built in Number formats
27  */

28 public final class NumberFormats
29 {
30   /**
31    * Inner class which holds the format index
32    */

33   private static class BuiltInFormat implements DisplayFormat, Format
34   {
35     /**
36      * The built in number format index
37      */

38     private int index;
39
40     /**
41      * The format string
42      */

43     private String JavaDoc formatString;
44
45     /**
46      * Constructor, using the predetermined index
47      *
48      * @param i the index
49      * @param s the string
50      */

51     public BuiltInFormat(int i, String JavaDoc s)
52     {
53       index = i;
54       formatString = s;
55     }
56
57     /**
58      * Accessor for the format index
59      *
60      * @return the index
61      */

62     public int getFormatIndex()
63     {
64       return index;
65     }
66     /**
67      * Accessor to determine if this format has been initialized. Since it is
68      * built in, this will always return TRUE
69      *
70      * @return TRUE, since this is a built in format
71      */

72     public boolean isInitialized()
73     {
74       return true;
75     }
76     /**
77      * Determines whether this format is a built in format
78      *
79      * @return TRUE, since this is a built in numerical format
80      */

81     public boolean isBuiltIn()
82     {
83       return true;
84     }
85     /**
86      * Initializes this format with a dynamically determined index value.
87      * Since this is a built in, and hence the index value is predetermined,
88      * this method has an empty body
89      *
90      * @param pos the pos in the number formats list
91      */

92     public void initialize(int pos)
93     {
94     }
95     /**
96      * Accesses the excel format string which is applied to the cell
97      * Note that this is the string that excel uses, and not the java
98      * equivalent
99      *
100      * @return the cell format string
101      */

102     public String JavaDoc getFormatString()
103     {
104       return formatString;
105     }
106
107     /**
108      * Standard equals method
109      *
110      * @param o the object to compare
111      * @return TRUE if the two objects are equal, FALSE otherwise
112      */

113     public boolean equals(Object JavaDoc o)
114     {
115       if (o == this)
116       {
117         return true;
118       }
119
120       if (!(o instanceof BuiltInFormat))
121       {
122         return false;
123       }
124
125       BuiltInFormat bif = (BuiltInFormat) o;
126
127       return index == bif.index;
128     }
129
130     /**
131      * Standard hash code method
132      *
133      * @return the hash code
134      */

135     public int hashCode()
136     {
137       return index;
138     }
139   }
140
141
142   // The available built in number formats
143
// First describe the fairly bog standard formats
144

145   /**
146    * The default format. This is equivalent to a number format of '#'
147    */

148   public static final DisplayFormat DEFAULT = new BuiltInFormat(0x0, "#");
149   /**
150    * Formatting for an integer number. This is equivalent to a DecimalFormat
151    * of "0"
152    */

153   public static final DisplayFormat INTEGER = new BuiltInFormat(0x1, "0");
154
155   /**
156    * Formatting for a float. This formats number to two decimal places. It
157    * is equivalent to a DecimalFormat of "0.00"
158    */

159   public static final DisplayFormat FLOAT = new BuiltInFormat(0x2, "0.00");
160
161   /**
162    * Formatting for an integer that has a thousands separator.
163    * Equivalent to a DecimalFormat of "#,##0"
164    */

165   public static final DisplayFormat THOUSANDS_INTEGER =
166                                        new BuiltInFormat(0x3, "#,##0");
167
168   /**
169    * Formatting for a float that has a thousands separator.
170    * Equivalent to a DecimalFormat of "#,##0.00"
171    */

172   public static final DisplayFormat THOUSANDS_FLOAT =
173                                         new BuiltInFormat(0x4, "#,##0.00");
174
175   /**
176    * Formatting for an integer which is presented in accounting format
177    * (ie. deficits appear in parentheses)
178    * Equivalent to a DecimalFormat of "$#,##0;($#,##0)"
179    */

180   public static final DisplayFormat ACCOUNTING_INTEGER =
181                                    new BuiltInFormat(0x5, "$#,##0;($#,##0)");
182
183   /**
184    * As ACCOUNTING_INTEGER except that deficits appear coloured red
185    */

186   public static final DisplayFormat ACCOUNTING_RED_INTEGER =
187                                     new BuiltInFormat(0x6, "$#,##0;($#,##0)");
188
189   /**
190    * Formatting for an integer which is presented in accounting format
191    * (ie. deficits appear in parentheses)
192    * Equivalent to a DecimalFormat of "$#,##0;($#,##0)"
193    */

194   public static final DisplayFormat ACCOUNTING_FLOAT =
195                                      new BuiltInFormat(0x7, "$#,##0;($#,##0)");
196
197   /**
198    * As ACCOUNTING_FLOAT except that deficits appear coloured red
199    */

200   public static final DisplayFormat ACCOUNTING_RED_FLOAT =
201                                    new BuiltInFormat(0x8, "$#,##0;($#,##0)");
202
203   /**
204    * Formatting for an integer presented as a percentage
205    * Equivalent to a DecimalFormat of "0%"
206    */

207   public static final DisplayFormat PERCENT_INTEGER =
208     new BuiltInFormat(0x9, "0%");
209
210   /**
211    * Formatting for a float percentage
212    * Equivalent to a DecimalFormat "0.00%"
213    */

214   public static final DisplayFormat PERCENT_FLOAT =
215     new BuiltInFormat(0xa, "0.00%");
216
217   /**
218    * Formatting for exponential or scientific notation
219    * Equivalent to a DecimalFormat "0.00E00"
220    */

221   public static final DisplayFormat EXPONENTIAL =
222     new BuiltInFormat(0xb, "0.00E00");
223
224   // Now describe the more obscure formats
225

226   /**
227    * Equivalent to a DecimalFormat "#,##0;(#,##0)"
228    */

229   public static final DisplayFormat FORMAT1 =
230                                       new BuiltInFormat(0x25, "#,##0;(#,##0)");
231
232   /**
233    * Equivalent to FORMAT1 except deficits are coloured red
234    */

235   public static final DisplayFormat FORMAT2 =
236                                   new BuiltInFormat(0x26, "#,##0;(#,##0)");
237
238   /**
239    * Equivalent to DecimalFormat "#,##0.00;(#,##0.00)"
240    */

241   public static final DisplayFormat FORMAT3 =
242                                new BuiltInFormat(0x27, "#,##0.00;(#,##0.00)");
243
244   /**
245    * Equivalent to FORMAT3 except deficits are coloured red
246    */

247   public static final DisplayFormat FORMAT4 =
248                                 new BuiltInFormat(0x28, "#,##0.00;(#,##0.00)");
249
250   /**
251    * Equivalent to DecimalFormat "#,##0;(#,##0)"
252    */

253   public static final DisplayFormat FORMAT5 =
254                                     new BuiltInFormat(0x29, "#,##0;(#,##0)");
255
256   /**
257    * Equivalent to FORMAT5 except deficits are coloured red
258    */

259   public static final DisplayFormat FORMAT6 =
260                                    new BuiltInFormat(0x2a, "#,##0;(#,##0)");
261
262   /**
263    * Equivalent to DecimalFormat "#,##0.00;(#,##0.00)"
264    */

265   public static final DisplayFormat FORMAT7 =
266                                new BuiltInFormat(0x2b, "#,##0.00;(#,##0.00)");
267
268   /**
269    * Equivalent to FORMAT7 except deficits are coloured red
270    */

271   public static final DisplayFormat FORMAT8 =
272                                  new BuiltInFormat(0x2c, "#,##0.00;(#,##0.00)");
273
274   /**
275    * Equivalent to FORMAT7
276    */

277   public static final DisplayFormat FORMAT9 =
278                                 new BuiltInFormat(0x2e, "#,##0.00;(#,##0.00)");
279
280   /**
281    * Equivalent to DecimalFormat "##0.0E0"
282    */

283   public static final DisplayFormat FORMAT10 =
284     new BuiltInFormat(0x30, "##0.0E0");
285
286   /**
287    * Forces numbers to be interpreted as text
288    */

289   public static final DisplayFormat TEXT = new BuiltInFormat(0x31, "@");
290 }
291
292
293
Popular Tags