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

标签 java eclipse data-binding jface rcp

对于绑定(bind)字符串和组合,我可以使用此代码:

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

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

但我想将组合的 selectionIndex 与 Integer 值绑定(bind)。我该怎么做?

最佳答案

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

关于java - 如何在 Eclipse RCP 应用程序 (SWT) 中绑定(bind)组合的 selectionIndex 和 Integer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10794003/

相关文章:

android - R.java 权限被拒绝使用旧工作区时 Eclipse 中的错误

java - 如何在eclipse中设置sqoop2应用程序项目?

c# - 从 DataGridView 记录列表中的更改

WPF - 刷新数据模板的内容

java - eclipse:用 if 包围 block

wpf - 如何将 RoutedEventHandler 正确传递给 UserControl?

java - 在字符串中搜索大写字母

java - Apache POI 选择单元格多个对象

java - 为 DataInputStream 手动设置超时

java - 线程 "main"java.lang.NumberFormatException : For input string "F:/TextFiles/loginaccs.txt" 中的异常