2014-09-23 46 views
0

我在使用DataBinding進行驗證時必須實現更改背景行爲,我該如何執行此操作? 我有兩個DateChooserCombo(星雲),我想防止重疊,並改變顏色爲紅色,例如當dateBegin> dateEnd,這是我迄今爲止所做的。 謝謝SWT/JFace數據綁定更改背景

IObservableValue textObservable = new DateChooserComboObservableValue(
      dateChooser, SWT.Modify); 

    UpdateValueStrategy strategy = new UpdateValueStrategy(); 
    strategy.setBeforeSetValidator(new IValidator() { 
     @Override 
     public IStatus validate(Object value) { 
        //for testing purpose make it fail 
      return ValidationStatus.error("this is not permitted"); 
     } 
    }); 
    Realm realm = SWTObservables.getRealm(dateChooser.getDisplay()); 
    DataBindingContext context = new DataBindingContext(realm); 
    org.eclipse.core.databinding.Binding binding = context.bindValue(
      textObservable, PojoProperties.value(Model.class, "dateEnd") 
        .observe(realm, model.dateEnd), strategy, 
      strategy); 
     //didn't show the control decoration as expected 
    ControlDecorationSupport.create(binding, SWT.TOP | SWT.LEFT); 

回答

1

我認爲這樣的事情會起作用。

new IValidator() { 
    @Override 
    public IStatus validate(Object value) { 
       // change background goes could here 
       //myControl.setBackground (new Color (display, new RGB (230,230,230)); 
       //for testing purpose make it fail 
     return ValidationStatus.error("this is not permitted"); 
    } 
} 
+0

如果您創建了一個新的'顏色'對象,您必須**安排在完成它時處理它。 – 2014-09-24 07:22:55

+0

是的你是對的,這只是一個解決方案的例子,如何改變顏色,但是每個swt資源都需要處理或需要使用像ColorRegistry o這樣的資源註冊表。 – 2014-09-24 07:33:39