stephenh / tessell

GWT model, presenter, view framework

Home Page:http://www.tessell.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Custom Bindings

xclayl opened this issue · comments

I want to write a custom binding to a Widget I've created. I'm not sure if I've got it right. Do you mind taking a look? The question I have is whether I need to call binder.Add() -- I can't in this code since the method is protected.

    private <T> void bind(final ListProperty<String> property, final IsMultiselectListBox listbox, Iterable<T> options, Func<T, String> nameConverter, Func<T, String> valueConverter) {

        listbox.clear();
        for(T item : options) {
            String name = nameConverter.call(item);
            String val = valueConverter.call(item);
            listbox.addItem(name, val);
        }


        final BooleanWrapper isFiring = new BooleanWrapper(false);

        binder.onChange(property).execute(new UiCommand() {

            @Override
            protected void doExecute() {
                if (!isFiring.get()) {
                    isFiring.set(true);
                    for(int i = 0; i < listbox.getItemCount(); i++) {

                        if (property.contains(listbox.getValue(i)).isTrue()) {
                            listbox.setItemSelected(i, true);
                        } else {
                            listbox.setItemSelected(i, false);
                        }
                    }
                    isFiring.set(false);
                }
            }

        });

        // Can I call binder.Add() here?
        listbox.addChangeHandler(new ChangedEventHandler() {

            @Override
            public void onChanged(ChangedEvent event) {

                if (!isFiring.get()) {
                    isFiring.set(true);
                    List<String> vals = new ArrayList<String>();
                    for(int i = 0; i < listbox.getItemCount(); i++) {
                        if (listbox.isItemSelected(i)) {
                            vals.add(listbox.getValue(i));
                        }
                    }


                    property.set(vals);
                    isFiring.set(false);
                }
            }
        });
    }