001/** 002 * Copyright 2005-2016 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.krad.web.bind; 017 018import org.kuali.rice.core.api.util.RiceKeyConstants; 019import org.kuali.rice.core.api.util.type.KualiDecimal; 020import org.kuali.rice.core.api.util.type.KualiPercent; 021import org.kuali.rice.core.web.format.FormatException; 022 023import java.beans.PropertyEditorSupport; 024import java.io.Serializable; 025import java.math.BigDecimal; 026import java.text.DecimalFormat; 027import java.text.NumberFormat; 028import java.text.ParseException; 029 030/** 031 * PropertyEditor converts between percentage strings and 032 * <code>org.kuali.rice.core.api.util.type.KualiPercent</code> objects 033 * 034 * @author Kuali Rice Team (rice.collab@kuali.org) 035 */ 036public class UifPercentageEditor extends PropertyEditorSupport implements Serializable { 037 private static final long serialVersionUID = -3562156375311932094L; 038 039 /** The default scale for percentage values. */ 040 public final static int PERCENTAGE_SCALE = 2; 041 042 /** The default format for percentage values. */ 043 public final static String PERCENTAGE_FORMAT = "#,##0.00"; 044 045 /** 046 * This overridden method converts 047 * <code>org.kuali.rice.core.api.util.type.KualiPercent</code> objects to the 048 * display string. 049 * 050 * @see java.beans.PropertyEditorSupport#getAsText() 051 */ 052 @Override 053 public String getAsText() { 054 Object value = this.getValue(); 055 // Previously returned N/A 056 if (value == null) 057 return ""; 058 059 String stringValue = ""; 060 try { 061 if (value instanceof KualiDecimal) { 062 value = ((KualiDecimal) this.getValue()).bigDecimalValue(); 063 } 064 BigDecimal bigDecValue = (BigDecimal) value; 065 bigDecValue = bigDecValue.setScale(PERCENTAGE_SCALE, BigDecimal.ROUND_HALF_UP); 066 stringValue = NumberFormat.getInstance().format(bigDecValue.doubleValue()); 067 } catch (IllegalArgumentException iae) { 068 throw new FormatException("formatting", RiceKeyConstants.ERROR_PERCENTAGE, this.getValue().toString(), iae); 069 } 070 071 return stringValue + " percent"; 072 } 073 074 /** 075 * This overridden method converts the display string to a 076 * <code>org.kuali.rice.core.api.util.type.KualiPercent</code> object. 077 * 078 * @see java.beans.PropertyEditorSupport#setAsText(java.lang.String) 079 */ 080 @Override 081 public void setAsText(String text) throws IllegalArgumentException { 082 try { 083 DecimalFormat formatter = new DecimalFormat(PERCENTAGE_FORMAT); 084 Number parsedNumber = formatter.parse(text.trim()); 085 this.setValue(new KualiPercent(parsedNumber.doubleValue())); 086 } catch (NumberFormatException e) { 087 throw new FormatException("parsing", RiceKeyConstants.ERROR_PERCENTAGE, text, e); 088 } catch (ParseException e) { 089 throw new FormatException("parsing", RiceKeyConstants.ERROR_PERCENTAGE, text, e); 090 } 091 } 092 093}