import java.util.ArrayList; import com.smartgwt.client.types.Alignment; import com.smartgwt.client.widgets.form.fields.TextItem; import com.smartgwt.client.widgets.form.fields.events.BlurEvent; import com.smartgwt.client.widgets.form.fields.events.BlurHandler; import com.smartgwt.client.widgets.form.fields.events.KeyUpEvent; import com.smartgwt.client.widgets.form.fields.events.KeyUpHandler; /** * Classe que cria campos de entrada de valores com SmartGWT e estende TextItem * Data criação: 01/11/2013 * @author Fernando Paiva * @version 0.1 * */ public class MoneyField extends TextItem { private ArrayList keys; public MoneyField(String title){ setTitle(title); setTextAlign(Alignment.RIGHT); setWidth(150); setRequired(true); setSelectOnFocus(true); setValue("0,00"); addKeysToCollection(); /** * perda de foco se for vazio seta o valor default 0,00 */ this.addBlurHandler(new BlurHandler() { @Override public void onBlur(BlurEvent event) { if(getValueAsString() == null){ setValue("0,00"); } } }); /** * ao soltar a tecla verifica se alguma das permissões da Collection key foi atendida * e formata os valores informados */ this.addKeyUpHandler(new KeyUpHandler() { @Override public void onKeyUp(KeyUpEvent event) { if(keys.contains(event.getKeyName()) ){ setValue(formataValores(getValueAsString())); }else{ setValue("0,00"); } } }); } /** * método que cria um array das teclas numericas permitidas e adiciona a uma Collection ArrayList */ private final void addKeysToCollection(){ keys = new ArrayList(); String[] teclas = {"0","1","2","3","4","5","6","7","8","9"}; for(int x = 0; x < teclas.length; x++){ keys.add(teclas[x]); } } /** * Método que formata os valores e retorna os valores formatados * @param str = string com numero a ser formatado * @return = retorna a string formatada */ private final String formataValores(String str){ if (str.isEmpty() || str.equalsIgnoreCase("0") ||str.equalsIgnoreCase("0.00")) { return "0,00"; } String decimal = "", inteiro = ""; int i, count; str = tirarZerosEsquerda(str); if (str.length() == 1) { inteiro = "0"; decimal = "0" + str; } else { if (str.length() == 2) { inteiro = "0"; decimal = str; } else { if(str.length() > 0){ decimal = str.substring(str.length() - 2, str.length()); i = 3; count = 0; while (i <= str.length()) { if (count == 3) { inteiro = "." + inteiro; count = 0; } inteiro = str.charAt(str.length() - i) + inteiro; count++; i++; } } } } if (inteiro == "") { inteiro = "0"; } if (decimal == "") { decimal = "00"; } return inteiro + "," + decimal; } /** * Metodo que recebe o valor formatado e formata tirando os zeros a esquerda * @param str = recebe o valor do metodo formataValores * @return retorna o valor formatado */ private final String tirarZerosEsquerda(String str) { String aux = ""; int i = 0; while (i < str.length()) { if ((str.charAt(i) != '.') && (str.charAt(i) != ',')) { aux += str.charAt(i); } i++; } str = aux; aux = ""; i = 0; while (i < str.length()) { if (str.charAt(i) != '0') { aux = str.substring(i, str.length()); i = str.length(); } i++; } return aux; } }