01: /**********************************************************************************
02: *
03: * $Id: PointsConverter.java 9271 2006-05-10 21:52:49Z ray@media.berkeley.edu $
04: *
05: ***********************************************************************************
06: *
07: * Copyright (c) 2005 The Regents of the University of California, The MIT Corporation
08: *
09: * Licensed under the Educational Community License, Version 1.0 (the "License");
10: * you may not use this file except in compliance with the License.
11: * You may obtain a copy of the License at
12: *
13: * http://www.opensource.org/licenses/ecl1.php
14: *
15: * Unless required by applicable law or agreed to in writing, software
16: * distributed under the License is distributed on an "AS IS" BASIS,
17: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18: * See the License for the specific language governing permissions and
19: * limitations under the License.
20: *
21: **********************************************************************************/package org.sakaiproject.tool.gradebook.jsf;
22:
23: import javax.faces.component.UIComponent;
24: import javax.faces.context.FacesContext;
25: import javax.faces.convert.NumberConverter;
26:
27: import org.apache.commons.logging.Log;
28: import org.apache.commons.logging.LogFactory;
29:
30: /**
31: * The standard JSF number formatters only round values. We generally need
32: * them truncated.
33: * This converter truncates the input value (probably a double) to two
34: * decimal places, and then returns it with a maximum of two decimal places.
35: */
36: public class PointsConverter extends NumberConverter {
37: private static final Log log = LogFactory
38: .getLog(PointsConverter.class);
39:
40: public PointsConverter() {
41: setType("number");
42: setMaxFractionDigits(2);
43: }
44:
45: public String getAsString(FacesContext context,
46: UIComponent component, Object value) {
47: if (log.isDebugEnabled())
48: log.debug("getAsString(" + context + ", " + component
49: + ", " + value + ")");
50:
51: String formattedScore;
52: if (value == null) {
53: formattedScore = FacesUtil
54: .getLocalizedString("score_null_placeholder");
55: } else {
56: if (value instanceof Number) {
57: // Truncate to 2 decimal places.
58: value = new Double(FacesUtil.getRoundDown(
59: ((Number) value).doubleValue(), 2));
60: }
61: formattedScore = super.getAsString(context, component,
62: value);
63: }
64:
65: return formattedScore;
66: }
67:
68: }
|