KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > ui > common > converter > ByteSizeConverter


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.ui.common.converter;
18
19 import java.text.DecimalFormat JavaDoc;
20
21 import javax.faces.component.UIComponent;
22 import javax.faces.context.FacesContext;
23 import javax.faces.convert.Converter;
24
25 import org.alfresco.web.app.Application;
26
27 /**
28  * Converter class to convert the size of an item in bytes into a readable KB/MB form.
29  *
30  * @author Kevin Roast
31  */

32 public class ByteSizeConverter implements Converter
33 {
34    /**
35     * <p>The standard converter id for this converter.</p>
36     */

37    public static final String JavaDoc CONVERTER_ID = "org.alfresco.faces.ByteSizeConverter";
38
39    private static final String JavaDoc MSG_POSTFIX_KB = "kilobyte";
40    private static final String JavaDoc MSG_POSTFIX_MB = "megabyte";
41    private static final String JavaDoc MSG_POSTFIX_GB = "gigabyte";
42    
43    private static final String JavaDoc NUMBER_PATTERN = "###,###.##";
44    
45    /**
46     * @see javax.faces.convert.Converter#getAsObject(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.String)
47     */

48    public Object JavaDoc getAsObject(FacesContext context, UIComponent component, String JavaDoc value)
49    {
50       return Long.parseLong(value);
51    }
52
53    /**
54     * @see javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.Object)
55     */

56    public String JavaDoc getAsString(FacesContext context, UIComponent component, Object JavaDoc value)
57    {
58       long size;
59       if (value instanceof Long JavaDoc)
60       {
61          size = (Long JavaDoc)value;
62       }
63       else if (value instanceof String JavaDoc)
64       {
65          try
66          {
67             size = Long.parseLong((String JavaDoc)value);
68          }
69          catch (NumberFormatException JavaDoc ne)
70          {
71             return (String JavaDoc)value;
72          }
73       }
74       else
75       {
76          return "";
77       }
78       
79       // get formatter
80
// TODO: can we cache this instance...? DecimalFormat is not threadsafe! Need threadlocal instance.
81
DecimalFormat JavaDoc formatter = new DecimalFormat JavaDoc(NUMBER_PATTERN);
82       
83       StringBuilder JavaDoc buf = new StringBuilder JavaDoc();
84       
85       if (size < 999999)
86       {
87          double val = ((double)size) / 1024.0;
88          buf.append(formatter.format(val))
89             .append(' ')
90             .append(Application.getMessage(context, MSG_POSTFIX_KB));
91       }
92       else if (size < 999999999)
93       {
94          double val = ((double)size) / 1048576.0;
95          buf.append(formatter.format(val))
96             .append(' ')
97             .append(Application.getMessage(context, MSG_POSTFIX_MB));
98       }
99       else
100       {
101          double val = ((double)size) / 1073741824.0;
102          buf.append(formatter.format(val))
103             .append(' ')
104             .append(Application.getMessage(context, MSG_POSTFIX_GB));
105       }
106       
107       return buf.toString();
108    }
109 }
110
Popular Tags