01: /**********************************************************************************
02: * $URL: https://source.sakaiproject.org/svn/gradebook/tags/sakai_2-4-1/app/ui/src/java/org/sakaiproject/tool/gradebook/jsf/NontrailingDoubleConverter.java $
03: * $Id: NontrailingDoubleConverter.java 8234 2006-04-25 01:18:48Z ray@media.berkeley.edu $
04: ***********************************************************************************
05: *
06: * Copyright (c) 2006 The Regents of the University of California
07: *
08: * Licensed under the Educational Community License, Version 1.0 (the "License");
09: * you may not use this file except in compliance with the License.
10: * You may obtain a copy of the License at
11: *
12: * http://www.opensource.org/licenses/ecl1.php
13: *
14: * Unless required by applicable law or agreed to in writing, software
15: * distributed under the License is distributed on an "AS IS" BASIS,
16: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17: * See the License for the specific language governing permissions and
18: * limitations under the License.
19: *
20: **********************************************************************************/package org.sakaiproject.tool.gradebook.jsf;
21:
22: import javax.faces.component.UIComponent;
23: import javax.faces.context.FacesContext;
24: import javax.faces.convert.NumberConverter;
25:
26: import org.apache.commons.logging.Log;
27: import org.apache.commons.logging.LogFactory;
28:
29: /**
30: * The standard JSF NumberConverter handles output formatting of double
31: * numbers nicely by printing them as an integer if there's nothing past
32: * the decimal point. On input either a Long or a Double might be
33: * returned from "getAsObject". In earlier versions of MyFaces, if the
34: * input value was going to a Double bean property, conversion would
35: * happen silently. In MyFaces 1.1.1, a IllegalArgumentException is
36: * thrown. This converter emulates the old behavior by converting Long
37: * values to Double values before passing them to the backing bean.
38: */
39: public class NontrailingDoubleConverter extends NumberConverter {
40: private static final Log log = LogFactory
41: .getLog(NontrailingDoubleConverter.class);
42:
43: public NontrailingDoubleConverter() {
44: setType("number");
45: }
46:
47: /**
48: * Always returns either a null or a Double.
49: */
50: public Object getAsObject(FacesContext context,
51: UIComponent component, String value) {
52: Object number = super .getAsObject(context, component, value);
53: if (number != null) {
54: if (number instanceof Long) {
55: number = new Double(((Long) number).doubleValue());
56: }
57: }
58: return number;
59: }
60:
61: }
|