3

对于绑定字符串和组合,我可以使用以下代码:

IObservableValue comboTypeObserveTextObserveWidget = SWTObservables.observeText(comboType);
IObservableValue typeObserveValue = PojoObservables.observeValue(router.getParameters(), "type.data");
bindingContext.bindValue(comboTypeObserveTextObserveWidget, typeObserveValue, updateStrategy, null);

其中“type.data”是字符串。

但我想将组合的 selectionIndex 与整数值绑定。我怎样才能做到这一点?

4

1 回答 1

5

您可以org.eclipse.jface.databinding.swt.SWTObservables.observeSingleSelectionIndex(Control)用于此目的...

package test123;

import java.beans.PropertyChangeListener;

import org.eclipse.core.databinding.AggregateValidationStatus;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class Test123 extends Shell {

    private static class Pojo<T> {
        private T data;

        public T getData() {
            return data;
        }

        public void setData(T data) {
            this.data = data;
        }

        public void addPropertyChangeListener(PropertyChangeListener l) {
        }

        public void removePropertyChangeListener(PropertyChangeListener l) {
        }
    }

    /**
     * Launch the application.
     * 
     * @param args
     */
    public static void main(String args[]) {
        try {
            final Display display = Display.getDefault();
            Realm.runWithDefault(SWTObservables.getRealm(display),
                    new Runnable() {

                        @Override
                        public void run() {
                            Test123 shell = new Test123(display);
                            shell.open();
                            shell.layout();
                            while (!shell.isDisposed()) {
                                if (!display.readAndDispatch()) {
                                    display.sleep();
                                }
                            }

                        }
                    });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the shell.
     * 
     * @param display
     */
    public Test123(Display display) {
        super(display, SWT.SHELL_TRIM);
        setLayout(new GridLayout(1, false));

        Combo combo = new Combo(this, SWT.READ_ONLY);
        combo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
                1));
        combo.setItems(new String[] { "Test 1", "Test 2", "Test 3" });
        createContents();
        final Pojo<Integer> pojo = new Pojo<Integer>();
        ISWTObservableValue swtObs = SWTObservables
                .observeSingleSelectionIndex(combo);

        Label lblNewLabel = new Label(this, SWT.NONE);
        lblNewLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
                false, 1, 1));
        IObservableValue modelObs = BeansObservables.observeValue(pojo, "data");
        final DataBindingContext dataBindingContext = new DataBindingContext();
        dataBindingContext.bindValue(swtObs, modelObs, new UpdateValueStrategy(
                UpdateValueStrategy.POLICY_CONVERT)
                .setAfterConvertValidator(new IValidator() {
                    @Override
                    public IStatus validate(Object value) {
                        if ((Integer) value == 1) {
                            return ValidationStatus
                                    .error("Test 2 is not allowed");
                        }
                        return ValidationStatus.ok();
                    }
                }), null);
        addDisposeListener(new DisposeListener() {
            @Override
            public void widgetDisposed(DisposeEvent e) {
                // this is neccessary since POLICY_CONVERT does not
                // automatically set the value to the model.
                dataBindingContext.updateModels();
                System.out.println(pojo.getData());
            }
        });
        ISWTObservableValue valiObs = SWTObservables.observeText(lblNewLabel);
        dataBindingContext.bindValue(valiObs, new AggregateValidationStatus(
                dataBindingContext.getBindings(),
                AggregateValidationStatus.MAX_SEVERITY));

    }

    /**
     * Create contents of the shell.
     */
    protected void createContents() {
        setText("SWT Application");
        setSize(450, 300);

    }

    @Override
    protected void checkSubclass() {
        // Disable the check that prevents subclassing of SWT components
    }
}
于 2012-05-29T09:16:17.007 回答