如何在Eclipse RCP应用程序(SWT)中绑定组合的selectionIndex和Integer?

时间:2022-05-29 11:38:56

For binding String and Combo I can use this code:

对于绑定String和Combo,我可以使用以下代码:

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

Where "type.data" is String.

其中“type.data”是String。

But I want to bind combos's selectionIndex with Integer value. How can I do that?

但我想将combos的selectionIndex与Integer值绑定。我怎样才能做到这一点?

1 个解决方案

#1


5  

You can use org.eclipse.jface.databinding.swt.SWTObservables.observeSingleSelectionIndex(Control) for this purpose...

您可以为此目的使用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
    }
}

#1


5  

You can use org.eclipse.jface.databinding.swt.SWTObservables.observeSingleSelectionIndex(Control) for this purpose...

您可以为此目的使用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
    }
}