1 13 14 package org.eclipse.jface.databinding.viewers; 15 16 import java.util.Set ; 17 18 import org.eclipse.core.databinding.observable.map.IMapChangeListener; 19 import org.eclipse.core.databinding.observable.map.IObservableMap; 20 import org.eclipse.core.databinding.observable.map.MapChangeEvent; 21 import org.eclipse.jface.viewers.ILabelProvider; 22 import org.eclipse.jface.viewers.ITableLabelProvider; 23 import org.eclipse.jface.viewers.LabelProvider; 24 import org.eclipse.jface.viewers.LabelProviderChangedEvent; 25 import org.eclipse.swt.graphics.Image; 26 27 31 public class ObservableMapLabelProvider extends LabelProvider 32 implements ILabelProvider, ITableLabelProvider { 33 34 private final IObservableMap[] attributeMaps; 35 36 private IMapChangeListener mapChangeListener = new IMapChangeListener() { 37 public void handleMapChange(MapChangeEvent event) { 38 Set affectedElements = event.diff.getChangedKeys(); 39 LabelProviderChangedEvent newEvent = new LabelProviderChangedEvent( 40 ObservableMapLabelProvider.this, affectedElements 41 .toArray()); 42 fireLabelProviderChanged(newEvent); 43 } 44 }; 45 46 49 public ObservableMapLabelProvider(IObservableMap attributeMap) { 50 this(new IObservableMap[] { attributeMap }); 51 } 52 53 56 public ObservableMapLabelProvider(IObservableMap[] attributeMaps) { 57 this.attributeMaps = attributeMaps; 58 for (int i = 0; i < attributeMaps.length; i++) { 59 attributeMaps[i].addMapChangeListener(mapChangeListener); 60 } 61 } 62 63 public void dispose() { 64 for (int i = 0; i < attributeMaps.length; i++) { 65 attributeMaps[i].removeMapChangeListener(mapChangeListener); 66 } 67 super.dispose(); 68 } 69 70 public Image getImage(Object element) { 71 return null; 72 } 73 74 public String getText(Object element) { 75 return getColumnText(element, 0); 76 } 77 78 public Image getColumnImage(Object element, int columnIndex) { 79 return null; 80 } 81 82 public String getColumnText(Object element, int columnIndex) { 83 if (columnIndex < attributeMaps.length) { 84 Object result = attributeMaps[columnIndex].get(element); 85 return result == null ? "" : result.toString(); } 87 return null; 88 } 89 90 } 91 | Popular Tags |