Mejora de los NumberFormatedTextFields

This commit is contained in:
Daniel Cortes
2019-01-06 00:28:02 -03:00
parent 5497e39aee
commit f1c199487a
7 changed files with 187 additions and 374 deletions

View File

@@ -107,8 +107,8 @@ public class ArqueoController {
*/
private void fillDocumentos() {
this.documentos = this.documentosDAO.findByCaja(caja);
this.view.getTarjetasField().setText(String.valueOf(documentos.getTarjetas()));
this.view.getChequesField().setText(String.valueOf(documentos.getCheques()));
this.view.getTarjetasField().setValue(documentos.getTarjetas());
this.view.getChequesField().setValue(documentos.getCheques());
}
/**
@@ -197,6 +197,7 @@ public class ArqueoController {
this.view.getCienField().getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("ENTER"),"nextField");
this.view.getCincuentaField().getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("ENTER"),"nextField");
this.view.getDiezField().getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("ENTER"),"save");
this.view.getGuardarEfectivoButton().getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("ENTER"),"save");
this.view.getVeinteMilField().getActionMap().put("nextField", new NextAction(this.view.getDiezMilField()));
this.view.getDiezMilField().getActionMap().put("nextField", new NextAction(this.view.getCincoMilField()));
@@ -207,16 +208,15 @@ public class ArqueoController {
this.view.getCienField().getActionMap().put("nextField", new NextAction(this.view.getCincuentaField()));
this.view.getCincuentaField().getActionMap().put("nextField", new NextAction(this.view.getDiezField()));
this.view.getDiezField().getActionMap().put("save", new GuardarEfectivoAction(this));
this.view.getGuardarEfectivoButton().getActionMap().put("save", new GuardarEfectivoAction(this));
this.view.getChequesField().getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("ENTER"),"nextField");
this.view.getTarjetasField().getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("ENTER"),"save");
this.view.getChequesField().getActionMap().put("nextField", new NextAction(this.view.getTarjetasField()));
this.view.getTarjetasField().getActionMap().put("save", new GuardarDocumentosAction(this));
this.view.getGuardarDocumentosButton().getActionMap().put("save", new GuardarDocumentosAction(this));
this.view.getGuardarEfectivoButton().addActionListener(e -> guardarEfectivoActionListener());
this.view.getGuardarDocumentosButton().addActionListener(e -> guardarDocumentosActionListener());
}
/**
@@ -233,8 +233,7 @@ public class ArqueoController {
* Primero llama a normalizar el input, luego a esconder los mensajes de error y finalmente a guardar los documentos
*/
private void guardarDocumentosActionListener(){
this.normalizeDocumentosInput();
this.hiddeDocumentosErrorMessages();
this.view.getGuardarDocumentosButton().requestFocus();
this.guardarDocumentos();
}
@@ -273,87 +272,20 @@ public class ArqueoController {
* llama a updateResumenDocumentos y updateResumenArqueo para actualizar los datos de documentosField y arqueoField
*/
private void guardarDocumentos() {
String tarjetas = this.view.getTarjetasField().getText();
String cheques = this.view.getChequesField().getText();
int tarjetas = this.view.getTarjetasField().getValue();
int cheques = this.view.getChequesField().getValue();
if (this.validateDocumentosInput(tarjetas, cheques)) {
this.documentos.setTarjetas(Integer.valueOf(tarjetas));
this.documentos.setCheques(Integer.valueOf(cheques));
this.documentosDAO.updateDocumentos(documentos);
this.documentos.setTarjetas(tarjetas);
this.documentos.setCheques(cheques);
this.documentosDAO.updateDocumentos(documentos);
this.updateResumenDocumentos();
this.updateResumenArqueo();
}
this.updateResumenDocumentos();
this.updateResumenArqueo();
}
/**
* Llama a las validaciones necesarias el input de Documentos
* @return si es que todas las validaciones fueron correctas
*/
private boolean validateDocumentosInput(String tarjetas, String cheques) {
boolean tarjetasValidation = validateDocumentosValor(tarjetas, this.view.getErrorTarjetas());
boolean chequesValidation = validateDocumentosValor(cheques, this.view.getErrorCheques());
return tarjetasValidation && chequesValidation;
}
/**
* Valida el valor entregado contra las siguientes pruebas
* - Es null
* - Esta vacio
* - Los caracteres no son solamente digitos
* - El largo es mayor que diez
* Setea un mensaje de error correspondiente en el errorLabel entregado.
* @return cuando cualquiera de los casos anteriores sea true se retorna falso, si no retorna true.
*/
private boolean validateDocumentosValor(String valor, JLabel errorLabel) {
if (valor == null) {
errorLabel.setText("Hubo un problema con los datos");
errorLabel.setVisible(true);
return false;
}
if (valor.isEmpty()) {
errorLabel.setText("El campo esta vacio");
errorLabel.setVisible(true);
return false;
}
if (!valor.chars().allMatch(Character::isDigit)) {
errorLabel.setText("Deben ser numeros");
errorLabel.setVisible(true);
return false;
}
if (valor.length() > 10) {
errorLabel.setText("El numero ingresado es demasiado largo");
errorLabel.setVisible(true);
return false;
}
return true;
}
/**
* Esconde los mensajes de error en los campos de documentos
*/
private void hiddeDocumentosErrorMessages(){
this.view.getErrorTarjetas().setVisible(false);
this.view.getErrorCheques().setVisible(false);
}
/**
* Ejecuta trim sobre todos los campos de documentos
*/
private void normalizeDocumentosInput() {
this.view.getChequesField().setText(this.view.getChequesField().getText().trim());
this.view.getTarjetasField().setText(this.view.getTarjetasField().getText().trim());
}
private class GuardarEfectivoAction extends AbstractAction{
private class GuardarEfectivoAction extends AbstractAction {
ArqueoController controller;
GuardarEfectivoAction(ArqueoController controller){
this.controller = controller;
}
@@ -364,8 +296,9 @@ public class ArqueoController {
}
}
private class GuardarDocumentosAction extends AbstractAction{
private class GuardarDocumentosAction extends AbstractAction {
ArqueoController controller;
GuardarDocumentosAction(ArqueoController controller){
this.controller = controller;
}

View File

@@ -335,7 +335,7 @@
</component>
</children>
</grid>
<grid id="1ca11" layout-manager="GridLayoutManager" row-count="5" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="1ca11" layout-manager="GridLayoutManager" row-count="3" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
@@ -345,52 +345,42 @@
<font/>
</border>
<children>
<component id="1b69f" class="javax.swing.JTextField" binding="chequesField">
<component id="65787" class="danielcortes.xyz.views.components.NumberFormatedTextField" binding="chequesField">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties>
<text value="0"/>
<text value=""/>
</properties>
</component>
<component id="d49a7" class="javax.swing.JLabel">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Tarjetas de Credito"/>
</properties>
</component>
<component id="7049f" class="javax.swing.JTextField" binding="tarjetasField">
<component id="8b1b9" class="danielcortes.xyz.views.components.NumberFormatedTextField" binding="tarjetasField">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties>
<text value="0"/>
<text value=""/>
</properties>
</component>
<component id="1681b" class="javax.swing.JButton" binding="guardarDocumentosButton">
<constraints>
<grid row="4" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="2" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Guardar"/>
</properties>
</component>
<component id="a438" class="javax.swing.JLabel" binding="errorCheques">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<foreground color="-65536"/>
<text value="Error"/>
<visible value="false"/>
</properties>
</component>
<component id="b855f" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="4" fill="0" indent="0" use-parent-layout="false"/>
@@ -399,16 +389,6 @@
<text value="Cheques al Dia"/>
</properties>
</component>
<component id="4f33f" class="javax.swing.JLabel" binding="errorTarjetas">
<constraints>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<foreground color="-65536"/>
<text value="Error"/>
<visible value="false"/>
</properties>
</component>
</children>
</grid>
</children>

View File

@@ -45,16 +45,14 @@ public class ArqueoView {
private NumberFormatedTextField cienField;
private NumberFormatedTextField cincuentaField;
private NumberFormatedTextField diezField;
private JTextField chequesField;
private JTextField tarjetasField;
private NumberFormatedTextField chequesField;
private NumberFormatedTextField tarjetasField;
private NumberFormatedTextField efectivoField;
private NumberFormatedTextField documentosField;
private NumberFormatedTextField egresosField;
private NumberFormatedTextField rendidoField;
private JButton guardarEfectivoButton;
private JButton guardarDocumentosButton;
private JLabel errorCheques;
private JLabel errorTarjetas;
private NumberFormatedTextField diferenciaField;
private NumberFormatedTextField debeRendirField;
@@ -98,11 +96,11 @@ public class ArqueoView {
return diezField;
}
public JTextField getChequesField() {
public NumberFormatedTextField getChequesField() {
return chequesField;
}
public JTextField getTarjetasField() {
public NumberFormatedTextField getTarjetasField() {
return tarjetasField;
}
@@ -130,14 +128,6 @@ public class ArqueoView {
return guardarDocumentosButton;
}
public JLabel getErrorCheques() {
return errorCheques;
}
public JLabel getErrorTarjetas() {
return errorTarjetas;
}
public NumberFormatedTextField getDiferenciaField() {
return diferenciaField;
}
@@ -282,34 +272,24 @@ public class ArqueoView {
guardarEfectivoButton.setText("Guardar");
panel3.add(guardarEfectivoButton, new GridConstraints(9, 0, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(200, -1), null, 0, false));
final JPanel panel4 = new JPanel();
panel4.setLayout(new GridLayoutManager(5, 2, new Insets(10, 10, 10, 10), -1, -1));
panel4.setLayout(new GridLayoutManager(3, 2, new Insets(10, 10, 10, 10), -1, -1));
panel2.add(panel4, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
panel4.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Detalle Documentos", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, this.$$$getFont$$$(null, -1, -1, panel4.getFont())));
chequesField = new JTextField();
chequesField.setText("0");
chequesField = new NumberFormatedTextField();
chequesField.setText("");
panel4.add(chequesField, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
final JLabel label16 = new JLabel();
label16.setText("Tarjetas de Credito");
panel4.add(label16, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
tarjetasField = new JTextField();
tarjetasField.setText("0");
panel4.add(tarjetasField, new GridConstraints(2, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
panel4.add(label16, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
tarjetasField = new NumberFormatedTextField();
tarjetasField.setText("");
panel4.add(tarjetasField, new GridConstraints(1, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
guardarDocumentosButton = new JButton();
guardarDocumentosButton.setText("Guardar");
panel4.add(guardarDocumentosButton, new GridConstraints(4, 0, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
errorCheques = new JLabel();
errorCheques.setForeground(new Color(-65536));
errorCheques.setText("Error");
errorCheques.setVisible(false);
panel4.add(errorCheques, new GridConstraints(1, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
panel4.add(guardarDocumentosButton, new GridConstraints(2, 0, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
final JLabel label17 = new JLabel();
label17.setText("Cheques al Dia");
panel4.add(label17, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
errorTarjetas = new JLabel();
errorTarjetas.setForeground(new Color(-65536));
errorTarjetas.setText("Error");
errorTarjetas.setVisible(false);
panel4.add(errorTarjetas, new GridConstraints(3, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
final Spacer spacer1 = new Spacer();
contentPanel.add(spacer1, new GridConstraints(2, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL, 1, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
}

View File

@@ -32,10 +32,20 @@ import java.awt.event.KeyEvent;
import java.text.NumberFormat;
import java.text.ParseException;
/**
* Crea un JTextField que formatea automaticamente su texto como un integer el cual se puede obtener
* con el metodo getValue. Ademas de tener comportamientos especiales:
* - Al ganar foco se selecciona todo el texto.
* - Al perder foco formatea el texto como numero.
* - Solo se pueden ingresar 9 digitos.
*/
public class NumberFormatedTextField extends JTextField {
private int value;
private NumberFormat nf;
/**
* Crea una instacia del objeto
*/
public NumberFormatedTextField() {
super();
@@ -45,15 +55,29 @@ public class NumberFormatedTextField extends JTextField {
this.addKeyListener(new FieldKeyAdapter());
}
/**
* Guarda el integer entregado y lo muestra en el campo.
*/
public void setValue(int value){
this.value = value;
this.setText(nf.format(value));
}
/**
* Llama a readValue por un bug seguramente relacionado con el focus listener:
* - No actualizaba el valor al momento de hacer requestfocus a otro componente, probablemente porque no alcanza
* a realizarse la accion antes que ocurra la siguiente
*
* Fuerza a que se lea el valor en el textfield antes de retornarlo
*/
public int getValue(){
this.readValue();
return this.value;
}
/**
* Lee el valor en el texto y lo almacena en la instancia
*/
private void readValue(){
try {
String currentText = this.getText();
@@ -65,6 +89,9 @@ public class NumberFormatedTextField extends JTextField {
}
}
/**
* Formatea el value y lo muestra en el field
*/
private void formatText(){
this.setText(nf.format(this.value));
}