1 13 package org.eclipse.jface.internal.databinding.internal.swt; 14 15 import org.eclipse.core.databinding.observable.Diffs; 16 import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTObservableValue; 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.widgets.Button; 19 import org.eclipse.swt.widgets.Event; 20 import org.eclipse.swt.widgets.Listener; 21 22 26 public class ButtonObservableValue extends AbstractSWTObservableValue { 27 28 private final Button button; 29 30 private boolean selectionValue; 31 32 private Listener updateListener = new Listener() { 33 public void handleEvent(Event event) { 34 boolean oldSelectionValue = selectionValue; 35 selectionValue = button.getSelection(); 36 fireValueChange(Diffs.createValueDiff(oldSelectionValue ? Boolean.TRUE : Boolean.FALSE, 37 selectionValue ? Boolean.TRUE : Boolean.FALSE)); 38 } 39 }; 40 41 44 public ButtonObservableValue(Button button) { 45 super(button); 46 this.button = button; 47 button.addListener(SWT.Selection, updateListener); 48 button.addListener(SWT.DefaultSelection, updateListener); 49 } 50 51 public void doSetValue(final Object value) { 52 boolean oldSelectionValue = selectionValue; 53 selectionValue = value == null ? false : ((Boolean ) value) 54 .booleanValue(); 55 button.setSelection(selectionValue); 56 fireValueChange(Diffs.createValueDiff(oldSelectionValue ? Boolean.TRUE : Boolean.FALSE, 57 selectionValue ? Boolean.TRUE : Boolean.FALSE)); 58 } 59 60 public Object doGetValue() { 61 return button.getSelection() ? Boolean.TRUE : Boolean.FALSE; 62 } 63 64 public Object getValueType() { 65 return Boolean .class; 66 } 67 68 73 public synchronized void dispose() { 74 super.dispose(); 75 76 if (!button.isDisposed()) { 77 button.removeListener(SWT.Selection, updateListener); 78 button.removeListener(SWT.DefaultSelection, updateListener); 79 } 80 } 81 82 } 83 | Popular Tags |