001/** 002 * Copyright 2005-2018 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.demo.uif.editor; 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 * /** 032 * PropertyEditor converts between percentage strings and 033 * <code>org.kuali.rice.core.api.util.type.KualiPercent</code> objects 034 * 035 * @author Kuali Rice Team (rice.collab@kuali.org) 036 */ 037 038public class CustomPercentageEditor extends PropertyEditorSupport implements Serializable { 039 040 private static final long serialVersionUID = -3562156375311932094L; 041 042 /** The default scale for percentage values. */ 043 public final static int PERCENTAGE_SCALE = 2; 044 045 /** The default format for percentage values. */ 046 public final static String PERCENTAGE_FORMAT = "#,##0.00"; 047 048 /** 049 * This overridden method converts 050 * <code>org.kuali.rice.core.api.util.type.KualiPercent</code> objects to the 051 * display string. 052 * 053 * @see java.beans.PropertyEditorSupport#getAsText() 054 */ 055 @Override 056 public String getAsText() { 057 Object value = this.getValue(); 058 // Previously returned N/A 059 if (value == null) 060 return ""; 061 062 String stringValue = ""; 063 064 try { 065 if (value instanceof KualiDecimal) { 066 067 value = ((KualiDecimal) this.getValue()).bigDecimalValue(); 068 } 069 BigDecimal bigDecValue = (BigDecimal) value; 070 bigDecValue = bigDecValue.setScale(PERCENTAGE_SCALE, BigDecimal.ROUND_HALF_UP); 071 stringValue = NumberFormat.getInstance().format(bigDecValue.doubleValue()); 072 } catch (IllegalArgumentException iae) { 073 throw new FormatException("formatting", RiceKeyConstants.ERROR_PERCENTAGE, this.getValue().toString(), iae); 074 } 075 076 return stringValue +"%"; 077 } 078 079 /** 080 * This overridden method converts the display string to a 081 * <code>org.kuali.rice.core.api.util.type.KualiPercent</code> object. 082 * 083 * @see java.beans.PropertyEditorSupport#setAsText(java.lang.String) 084 */ 085 @Override 086 public void setAsText(String text) throws IllegalArgumentException { 087 try { 088 DecimalFormat formatter = new DecimalFormat(PERCENTAGE_FORMAT); 089 090 Number parsedNumber = formatter.parse(text.trim()); 091 this.setValue(new KualiPercent(parsedNumber.doubleValue())); 092 } catch (NumberFormatException e) { 093 throw new FormatException("parsing", RiceKeyConstants.ERROR_PERCENTAGE, text, e); 094 } catch (ParseException e) { 095 throw new FormatException("parsing", RiceKeyConstants.ERROR_PERCENTAGE, text, e); 096 } 097 } 098 099} 100 101