【问题标题】:How can I binding combo's selectionIndex and Integer in Eclipse RCP application (SWT)?如何在 Eclipse RCP 应用程序 (SWT) 中绑定组合的 selectionIndex 和 Integer?
【发布时间】:2012-06-03 08:57:03
【问题描述】:

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

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

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

但我想将组合的 selectionIndex 与整数值绑定。我该怎么做?

【问题讨论】:

    标签: java eclipse data-binding jface rcp


    【解决方案1】:

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

    【讨论】:

    • 当我使用这段代码时,我得到BindingException: "converter does not convert from type int Expected: int, actual: class java.lang.String"。但是type.data 是 Java 整数。
    • 谢谢你,汤姆!问题出在UpdateValueStrategy.POLICY_CONVERT。不知何故,POLICY_CONVERT 不支持 observeSingleSelectionIndex。在这种情况下如何替换POLICY_CONVERT 功能?
    • 是的,它有效。问题出在type.datadata 字段的类型由 Java 泛型参数定义,绑定后神奇地从整数切换到字符串。删除泛型参数后,它工作正常。你知道为什么会这样吗?
    • 不,这不可能是原因。我在 sn-p 中引入了一个通用参数。它有效......
    猜你喜欢
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-01
    • 1970-01-01
    • 2013-11-27
    相关资源
    最近更新 更多