Anees has posted 4 posts at DZone. View Full User Profile

Swing JComponent Validation with JGoodies Bindings Framework

10.08.2009
| 14041 views |
  • submit to reddit

For Validating the JComponents i.e JTextField, JTextArea etc. there are many validation libraries available. Being software engineers, we use different libraries for binding and different libraries for validation. Many of us use JGoodies binding framework for binding the  components and then use the Hibernate validation framework or JGoodies validation framework for validating the inputs to these components.

I am working on the JGoodies binding framework for binding my UI Swing widgets and in doing so I found one very important class i.e. AbstractVetoableValueModel. AbstractVetoableValueModel is the best class which can be used with JGoodies ValueModel for binding and validation. AbstractVetoableValueModel will veto any input that is against the validation grammar defined by the implemented class of it. The best example is to Allow Alpha or Numeric or Alpha-Numeric, size of the input data, whether special characters are allowed or not.

I want to follow with one small example that will ease the understanding

/**
*
* @author Anees
*
*/
public class MapVetoableValueModelAdaptor extends AbstractVetoableValueModel {

private static final long serialVersionUID = 1L;

private MapValueModel valueModel;

private JComponent component;

private PropertyConstraintsHolder constraintHolder;

private final String ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
private final String NUMERIC = "1234567890.";
private final String ALPHA_NUMERIC = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890.";
private final String BADCHARACTERS = "`~!@#$%^&*()_+=\\|\"':;?/>.<,";

public MapVetoableValueModelAdaptor(MapValueModel subject,
JComponent component, PropertyConstraintsHolder constraintHolder) {
super(subject);
this.valueModel = subject;
this.component = component;
this.constraintHolder = constraintHolder;
}

@Override
public boolean proposedChange(final Object oldValue, Object proposedNewValue) {
final boolean flag;
if (proposedNewValue.toString().length() <= constraintHolder
.getMaxLength()) {
if (checkForInputCharactor(proposedNewValue, constraintHolder
.getTextInputDataType())) {
flag = true;
} else
flag = false;
} else {

flag = false;
}

if (!flag) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
UpdateJComponent updateComponent = new UpdateJComponent(
component, oldValue);
updateComponent.processWithOldValue();
}
});
}
return flag;
}

/**
*
* @param proposedNewValue
* @return
*/
private boolean checkForInputCharactor(Object proposedNewValue,
TextFiledInputDataType inputDataType) {

if (TextFiledInputDataType.ONLYALPHA.equals(inputDataType)) {

return processOnlyAlpha(proposedNewValue);

} else if (TextFiledInputDataType.ONLYNUMBER.equals(inputDataType)) {

return processOnlyForNumeric(proposedNewValue);

} else
return processForAlphaNumeric(proposedNewValue);

}

/**
*
* @param proposedNewValue
* @return
*/
private boolean processForAlphaNumeric(Object proposedNewValue) {

boolean flag = true;

String str = proposedNewValue.toString();

int length = str.length();

for (int i = 0; i < length; i++) {

if (-1 == ALPHA_NUMERIC.indexOf(str.charAt(i))) {
flag = false;
}
}
return flag;

}

/**
*
* @param proposedNewValue
* @return
*/
private boolean processOnlyForNumeric(Object proposedNewValue) {

boolean flag = true;

String str = proposedNewValue.toString();

int length = str.length();

for (int i = 0; i < length; i++) {

if (-1 == NUMERIC.indexOf(str.charAt(i))) {
flag = false;
}
}
return flag;
}

/**
*
* @param proposedNewValue
* @return
*/
private boolean processOnlyAlpha(Object proposedNewValue) {

boolean flag = true;

String str = proposedNewValue.toString();

int length = str.length();

for (int i = 0; i < length; i++) {

if (-1 == ALPHA.indexOf(str.charAt(i))) {
flag = false;
}
}
return flag;
}

/**
*
* @author Anees
*
*/
private class UpdateJComponent {

private Object value;

private JComponent component;

public UpdateJComponent(JComponent component, Object oldValue) {

this.component = component;

this.value = oldValue;
}

/**
*
*/
public void processWithOldValue() {

if (component instanceof JTextComponent) {

JTextComponent txtComp = (JTextComponent) component;

if (null != value)
txtComp.setText(value.toString());
else
txtComp.setText("");
}
}
}
}

After creating this adapter we will bind it with swing JComponents rather than binding ValueModel.
Example usage is :

JTextField textField = new JTextField();

// Getting the ValueModel by using PresentationModel or creating ValueModel programmtically

ValueModel valueModel = .......

MapVetoableValueModelAdaptor vetoableAdapter =
new MapVetoableValueModelAdaptor(valueModel, textField, constraintHolder);

Bindings.bind(textField, vetoableAdapter);

By this way we can avoid the experimentation needs to bedone with other validation libraries and can save a lot of time by enjoying at beach

Published at DZone with permission of its author, Anees Ur-rehman.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)