How to add ValueChangeHandler to the customize DateBox?

135 Views Asked by At

I want to add the functionality of value change handler to my customize class that extends the DateBox. I had already added the key handler but I also want to implement the value change handler to the same class.

I want my DateBox to work properly when I take input from DatePicker or Keyboard.

Any other suggestion will also be helpful.

public class ValidationDateBoxInternal extends DateBox implements KeyUpHandler{
    
    private List<String> regexList;
    
    public ValidationDateBoxInternal(){
        super();
        this.getTextBox().addKeyUpHandler(this);
        regexList = new ArrayList<String>();
    }
    
    
    public void setAsNotEmpty() {
          regexList.add(DateValidation.isNotEmpty);
          isValid();
       }
    
    
    public void setAsValidDate(){
        regexList.add(DateValidation.isValidDate);
        isValid();
    }
       
    
    public void onKeyUp(KeyUpEvent event) {
          isValid();
       }
    
    
    public void setText(String text) {
          super.getTextBox().setText(text);
          isValid();
       }
    
    
    
    
    
   public void clearRegexList() {
      regexList.clear();
   }
   
   public String getText() {
          if (regexList.size() == 0) {
             return super.getTextBox().getText();
          }
          if (isValid()) {
             for (String regex: regexList) {
                if (regex.equals(DateValidation.isValidDate)) {
                   return super.getTextBox().getText().toLowerCase().trim();
                }
             }
             return super.getTextBox().getText().trim();
          }
          else {
             return null;
          }
       }
   
   public boolean isValid() {
      for (String regex: regexList) {
         if (!DateValidation.isValid(super.getTextBox().getText(), regex)) {
            this.addStyleName("invalid");
            return false;
         }
      }
      this.removeStyleName("invalid");
      return true;
   }
}


1

There are 1 best solutions below

2
On

Your class extends DateBox, which implements the HasValueChangeHandlers interface. So you can call:

DateBox.addValueChangeHandler(ValueChangeHandler<java.util.Date> handler)

For example:

public class MyDateBox extends DateBox implements ValueChangeHandler<Date> {
    void foo() {
        addValueChangeHandler(this);
    }

    void onValueChange(ValueChangeEvent<Date> event) {
         // validate the value of the DateBox
    }
}