简体   繁体   中英

Value Change Handler with multiple variables

I have a widget that extends composite and implements HasValueChangeHandlers that contains 3 Date objects: beg, current, and end. I am trying to be able to add a value change handler that will be able to tell which date object changed; but it's not immediately apparent to me the best way to do that.

What is the best way to solve this? How do I know which Date object has changed without going back and comparing the values?

Should I create empty classes for each variable and pass them as the source then switch on comparing the source class of the event in the onValueChange method? It seems like there should be a better way...

Thanks for the help.

I believe your best approach is to take advantage that the date objects themselves implement HasValueChangedHandlers . In your composite class, you can add a changed handler to each of them so that you know which one changed. In fact, if it is the same behavior that you want to apply to all of them you might as well you set the same handler for all. For example:

 public MyClass extends Composite implements HasValueChangedHandler{
       private DateBox beg;
       private DateBox current;
       private DateBox end;   

       //.... rest of the code..

       public HandlerRegistration addValueChangedHanlder(ValueChangedHandler<Date> h){
                 beg.addValueChangedHandler(h);
                 current.addValueChangedHandler(h);
                 end.addValueChangedHandler(h);

       }
        //.... rest of the code..

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM