简体   繁体   English

具有多个变量的值更改处理程序

[英]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. 我有一个小部件,该小部件扩展了复合结构并实现了HasValueChangeHandlers,其中包含3个Date对象:beg,current和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? 如何不更改并比较值就知道哪个Date对象已更改?

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? 我是否应该为每个变量创建空类并将它们作为源传递,然后在onValueChange方法中打开比较事件的源类? 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 . 我相信您最好的方法是利用日期对象本身实现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..

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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