Guest User

Untitled

a guest
Nov 1st, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.11 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. import com.smartgwt.client.types.Alignment;
  4. import com.smartgwt.client.widgets.form.fields.TextItem;
  5. import com.smartgwt.client.widgets.form.fields.events.BlurEvent;
  6. import com.smartgwt.client.widgets.form.fields.events.BlurHandler;
  7. import com.smartgwt.client.widgets.form.fields.events.KeyUpEvent;
  8. import com.smartgwt.client.widgets.form.fields.events.KeyUpHandler;
  9.  
  10.  
  11. /**
  12.  * Classe que cria campos de entrada de valores com SmartGWT e estende TextItem
  13.  * Data criação: 01/11/2013
  14.  * @author Fernando Paiva
  15.  * @version 0.1
  16.  *
  17.  */
  18. public class MoneyField extends TextItem { 
  19.     private ArrayList<String> keys;
  20.        
  21.     public MoneyField(String title){
  22.         setTitle(title);
  23.         setTextAlign(Alignment.RIGHT);     
  24.         setWidth(150);
  25.         setRequired(true);
  26.         setSelectOnFocus(true);
  27.         setValue("0,00");
  28.         addKeysToCollection();
  29.    
  30.         /**
  31.          * perda de foco se for vazio seta o valor default 0,00
  32.          */
  33.         this.addBlurHandler(new BlurHandler() {        
  34.             @Override
  35.             public void onBlur(BlurEvent event) {
  36.                 if(getValueAsString() == null){
  37.                     setValue("0,00");
  38.                 }
  39.             }
  40.         });
  41.        
  42.                
  43.         /**
  44.          * ao soltar a tecla verifica se alguma das permissões da Collection key foi atendida
  45.          * e formata os valores informados
  46.          */
  47.         this.addKeyUpHandler(new KeyUpHandler() {
  48.             @Override
  49.             public void onKeyUp(KeyUpEvent event) {
  50.                 if(keys.contains(event.getKeyName()) ){                
  51.                     setValue(formataValores(getValueAsString()));
  52.                 }else{
  53.                     setValue("0,00");
  54.                 }
  55.             }          
  56.         });
  57.        
  58.     }      
  59.    
  60.     /**
  61.      * método que cria um array das teclas numericas permitidas e adiciona a uma Collection ArrayList   
  62.      */
  63.     private final void addKeysToCollection(){
  64.         keys = new ArrayList<String>();
  65.         String[] teclas = {"0","1","2","3","4","5","6","7","8","9"};
  66.         for(int x = 0; x < teclas.length; x++){
  67.             keys.add(teclas[x]);
  68.         }
  69.     }
  70.    
  71.    
  72.     /**
  73.      * Método que formata os valores e retorna os valores formatados
  74.      * @param str = string com numero a ser formatado
  75.      * @return = retorna a string formatada  
  76.      */
  77.     private final String formataValores(String str){
  78.         if (str.isEmpty() || str.equalsIgnoreCase("0") ||str.equalsIgnoreCase("0.00")) {
  79.             return "0,00";
  80.         }
  81.         String decimal = "", inteiro = "";
  82.         int i, count;
  83.         str = tirarZerosEsquerda(str);
  84.         if (str.length() == 1) {
  85.             inteiro = "0";
  86.             decimal = "0" + str;
  87.         } else {
  88.             if (str.length() == 2) {
  89.                 inteiro = "0";
  90.                 decimal = str;
  91.             } else {
  92.                 if(str.length() > 0){
  93.                     decimal = str.substring(str.length() - 2, str.length());
  94.                     i = 3;
  95.                     count = 0;
  96.                     while (i <= str.length()) {
  97.                         if (count == 3) {
  98.                             inteiro = "." + inteiro;
  99.                             count = 0;
  100.                         }
  101.                         inteiro = str.charAt(str.length() - i) + inteiro;
  102.                         count++;
  103.                         i++;
  104.                     }
  105.                 }
  106.             }
  107.         }
  108.         if (inteiro == "") {
  109.             inteiro = "0";
  110.         }
  111.         if (decimal == "") {
  112.             decimal = "00";
  113.         }
  114.         return inteiro + "," + decimal;
  115.     }
  116.    
  117.    
  118.     /**
  119.      * Metodo que recebe o valor formatado e formata tirando os zeros a esquerda
  120.      * @param str = recebe o valor do metodo formataValores
  121.      * @return retorna o valor formatado
  122.      */
  123.     private final String tirarZerosEsquerda(String str) {  
  124.         String aux = "";  
  125.         int i = 0;  
  126.         while (i < str.length()) {  
  127.             if ((str.charAt(i) != '.') && (str.charAt(i) != ',')) {  
  128.                 aux += str.charAt(i);  
  129.             }  
  130.             i++;  
  131.         }  
  132.         str = aux;  
  133.         aux = "";  
  134.         i = 0;  
  135.         while (i < str.length()) {  
  136.             if (str.charAt(i) != '0') {  
  137.                 aux = str.substring(i, str.length());  
  138.                 i = str.length();  
  139.             }  
  140.             i++;  
  141.         }  
  142.         return aux;  
  143.     }  
  144.    
  145.    
  146. }
Advertisement
Add Comment
Please, Sign In to add comment