1 /*
2 * Copyright 2006-2016 The JGUIraffe Team.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License")
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.jguiraffe.examples.tutorial.mainwnd;
17
18 import java.text.NumberFormat;
19
20 import net.sf.jguiraffe.transform.Transformer;
21 import net.sf.jguiraffe.transform.TransformerContext;
22
23 /**
24 * <p>
25 * A specialized transformer for formatting file sizes.
26 * </p>
27 * <p>
28 * This transformer obtains the size of a file in kilobytes.
29 * </p>
30 *
31 * @author Oliver Heger
32 * @version $Id: FileSizeTransformer.java 205 2012-01-29 18:29:57Z oheger $
33 */
34 public class FileSizeTransformer implements Transformer
35 {
36 /** Constant for the resource ID for the KB constant. */
37 private static final String RES_KB = "trans_kb";
38
39 /** The size of a kilobyte. */
40 private static final long KB_SIZE = 1024;
41
42 /**
43 * Performs the transformation.
44 *
45 * @param o the object to transform
46 * @param ctx the transformer context
47 * @throws Exception if an error occurs
48 */
49 @Override
50 public Object transform(Object o, TransformerContext ctx) throws Exception
51 {
52 NumberFormat fmt = NumberFormat.getIntegerInstance(ctx.getLocale());
53 fmt.setGroupingUsed(true);
54 long sizeKb = (((Number) o).longValue() + KB_SIZE - 1) / KB_SIZE;
55 return fmt.format(sizeKb)
56 + ctx.getResourceManager().getText(ctx.getLocale(), null,
57 RES_KB);
58 }
59 }