1 11 12 package org.eclipse.jface.internal.databinding.provisional.viewers; 13 14 import org.eclipse.jface.internal.databinding.provisional.observable.Diffs; 15 import org.eclipse.jface.internal.databinding.provisional.observable.value.AbstractObservableValue; 16 import org.eclipse.jface.viewers.ISelectionChangedListener; 17 import org.eclipse.jface.viewers.IStructuredSelection; 18 import org.eclipse.jface.viewers.SelectionChangedEvent; 19 import org.eclipse.jface.viewers.StructuredViewer; 20 21 25 public class SelectionObservableValue extends AbstractObservableValue { 26 27 private StructuredViewer viewer; 28 29 private Object currentSelection = null; 30 31 private ISelectionChangedListener selectionChangedListener = new ISelectionChangedListener() { 32 public void selectionChanged(SelectionChangedEvent event) { 33 IStructuredSelection selection = (IStructuredSelection) event 34 .getSelection(); 35 Object oldSelection = currentSelection; 36 Object newSelection = selection.getFirstElement(); 37 if (newSelection != oldSelection) { 38 currentSelection = newSelection; 39 fireValueChange(Diffs.createValueDiff(oldSelection, newSelection)); 40 } 41 } 42 }; 43 44 47 public SelectionObservableValue(StructuredViewer viewer) { 48 this.viewer = viewer; 49 viewer.addSelectionChangedListener(selectionChangedListener); 50 this.currentSelection = ((IStructuredSelection) viewer.getSelection()) 51 .getFirstElement(); 52 } 53 54 protected Object doGetValue() { 55 return currentSelection; 56 } 57 58 public void dispose() { 59 viewer.removeSelectionChangedListener(selectionChangedListener); 60 viewer = null; 61 currentSelection = null; 62 selectionChangedListener = null; 63 super.dispose(); 64 } 65 66 public Object getValueType() { 67 return Object .class; 68 } 69 70 } 71 | Popular Tags |