package ru.jnano.swing.valid;
import java.awt.Color;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JLabel;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.JTextComponent;
import net.java.balloontip.BalloonTip;
import net.java.balloontip.BalloonTip.AttachLocation;
import net.java.balloontip.BalloonTip.Orientation;
import net.java.balloontip.styles.BalloonTipStyle;
import net.java.balloontip.styles.RoundedBalloonStyle;
import net.java.balloontip.utils.ToolTipUtils;
import com.jgoodies.validation.Severity;
import com.jgoodies.validation.ValidationResult;
import com.jgoodies.validation.ValidationResultModel;
import com.jgoodies.validation.Validator;
import com.jgoodies.validation.view.ValidationComponentUtils;
public class GroupSwingValidation {
private static final Color MANDATORY_BACKGROUND = new Color(235, 235, 255);
private static final Color WARNING_BACKGROUND = new Color(255, 235, 205);
private static final Color ERROR_BACKGROUND = new Color(255, 215, 215);
private Map<JTextComponent,Validator<JTextComponent>> mapValidators;
private ValidationResultModel validationResultModel;
public GroupSwingValidation(ValidationResultModel validationResultModel) {
super();
this.validationResultModel=validationResultModel;
this.mapValidators = new HashMap<JTextComponent,Validator<JTextComponent>>();
}
public void add(JTextComponent comp, Validator<JTextComponent> validator){
comp.setBackground(MANDATORY_BACKGROUND);
ValidationComponentUtils.setSeverity(comp, Severity.OK);
mapValidators.put(comp, validator);
comp.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void removeUpdate(DocumentEvent arg0) {
validate();
}
@Override
public void insertUpdate(DocumentEvent arg0) {
validate();
}
@Override
public void changedUpdate(DocumentEvent arg0) {
validate();
}
});
}
public ValidationResult validate(){
ValidationResult result = new ValidationResult();
for(JTextComponent tf: mapValidators.keySet()){
Validator<JTextComponent> validator = mapValidators.get(tf);
ValidationResult compResult = validator.validate(tf);
UpdateUI(tf,compResult);
result.addAllFrom(compResult);
}
validationResultModel.setResult(result);
return result;
}
public static void UpdateUI(JTextComponent comp, ValidationResult valResult){
Severity severity = ValidationComponentUtils.getSeverity(comp);
BalloonTip tip = (BalloonTip) comp.getClientProperty("Balloon");
if (tip!=null) tip.closeBalloon();
if (valResult.hasErrors()){
if (severity!=Severity.ERROR){
comp.setBackground(ERROR_BACKGROUND);
ValidationComponentUtils.setSeverity(comp, Severity.ERROR);
}
BalloonTipStyle tipStyle = new RoundedBalloonStyle(5,5,ERROR_BACKGROUND, Color.BLACK);
BalloonTip newTip = new BalloonTip(comp,new JLabel(valResult.getErrors().get(0).formattedText()),tipStyle,
Orientation.RIGHT_ABOVE, AttachLocation.WEST, 15, 15, false);
comp.putClientProperty("Balloon", newTip);
return;
}
if (valResult.hasWarnings()){
if (severity!=Severity.WARNING){
comp.setBackground(WARNING_BACKGROUND);
ValidationComponentUtils.setSeverity(comp, Severity.WARNING);
}
BalloonTipStyle tipStyle = new RoundedBalloonStyle(5,5,WARNING_BACKGROUND, Color.BLACK);
BalloonTip newTip = new BalloonTip(comp,new JLabel(valResult.getWarnings().get(0).formattedText()),tipStyle,
Orientation.RIGHT_ABOVE, AttachLocation.WEST, 15, 15, false);
ToolTipUtils.balloonToToolTip(newTip, 500, 10000);
comp.putClientProperty("Balloon", newTip);
return;
}
if (severity!=Severity.OK){
comp.setBackground(MANDATORY_BACKGROUND);
ValidationComponentUtils.setSeverity(comp, Severity.OK);
}
}
}