Created
October 20, 2015 10:22
-
-
Save altitdb/4932d3847eff9c450fd8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.text.AttributeSet; | |
import javax.swing.text.BadLocationException; | |
import javax.swing.text.PlainDocument; | |
import javax.swing.text.SimpleAttributeSet; | |
/** | |
* | |
* @author Altieres de Matos | |
*/ | |
public class DocumentMoney extends PlainDocument { | |
private static final SimpleAttributeSet nullAttribute = new SimpleAttributeSet(); | |
private static final long serialVersionUID = -3802846632709128803L; | |
private int tamanho = 16; | |
public DocumentMoney() { | |
} | |
public DocumentMoney(int tamanho) { | |
this.tamanho = tamanho; | |
} | |
@Override | |
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { | |
String original = getText(0, getLength()); | |
// Permite apenas digitar até 16 caracteres (9.999.999.999,99) | |
if (original.length() < tamanho) { | |
StringBuilder mascarado = new StringBuilder(); | |
if (a != nullAttribute) { | |
//limpa o campo | |
remove(-1, getLength()); | |
mascarado.append((original + str).replaceAll("[^0-9]", "")); | |
for (int i = 0; i < mascarado.length(); i++) { | |
if (!Character.isDigit(mascarado.charAt(i))) { | |
mascarado.deleteCharAt(i); | |
} | |
} | |
Long number = new Long(mascarado.toString()); | |
mascarado.replace(0, mascarado.length(), number.toString()); | |
if (mascarado.length() < 3) { | |
if (mascarado.length() == 1) { | |
mascarado.insert(0, "0"); | |
mascarado.insert(0, ","); | |
mascarado.insert(0, "0"); | |
} else if (mascarado.length() == 2) { | |
mascarado.insert(0, ","); | |
mascarado.insert(0, "0"); | |
} | |
} else { | |
mascarado.insert(mascarado.length() - 2, ","); | |
} | |
if (mascarado.length() > 6) { | |
mascarado.insert(mascarado.length() - 6, '.'); | |
if (mascarado.length() > 10) { | |
mascarado.insert(mascarado.length() - 10, '.'); | |
if (mascarado.length() > 14) { | |
mascarado.insert(mascarado.length() - 14, '.'); | |
} | |
} | |
} | |
super.insertString(0, mascarado.toString(), a); | |
} else { | |
if (original.length() == 0) { | |
super.insertString(0, "", a); | |
} | |
} | |
} | |
} | |
@Override | |
public void remove(int offs, int len) throws BadLocationException { | |
if (len == getLength()) { | |
super.remove(0, len); | |
if (offs != -1) { | |
insertString(0, "", nullAttribute); | |
} | |
} else { | |
String original = getText(0, getLength()).substring(0, offs) + getText(0, getLength()).substring(offs + len); | |
super.remove(0, getLength()); | |
insertString(0, original, null); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment