2012-05-29 24 views
3

对于绑定字符串和组合,我可以使用此代码:如何在Eclipse RCP应用程序(SWT)中绑定组合的selectionIndex和Integer?

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

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

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

回答

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 
    } 
} 
+0

当我使用此代码,我得到'BindingException':“转换器不从int类型转换的预期:INT,实际:类java.lang.Object 。串”。但'type.data'是Java Integer。 – levap

+0

我已经添加了一个工作片段 –

+0

谢谢汤姆!问题出在'UpdateValueStrategy.POLICY_CONVERT'上。不知何故,'observeSingleSelectionIndex'不支持'POLICY_CONVERT'。如何在此上下文中替换POLICY_CONVERT功能? – levap

相关问题