1 11 12 package org.eclipse.jface.viewers; 13 14 import org.eclipse.swt.SWT; 15 import org.eclipse.swt.graphics.Color; 16 import org.eclipse.swt.graphics.Rectangle; 17 import org.eclipse.swt.widgets.Event; 18 import org.eclipse.swt.widgets.Listener; 19 20 31 public abstract class OwnerDrawLabelProvider extends CellLabelProvider { 32 33 39 public static void setUpOwnerDraw(final ColumnViewer viewer) { 40 viewer.getControl().addListener(SWT.MeasureItem, new Listener() { 41 46 public void handleEvent(Event event) { 47 CellLabelProvider provider = viewer 48 .getViewerColumn(event.index).getLabelProvider(); 49 Object element = event.item.getData(); 50 51 if (provider instanceof OwnerDrawLabelProvider) 52 ((OwnerDrawLabelProvider) provider).measure(event, element); 53 } 54 }); 55 56 viewer.getControl().addListener(SWT.PaintItem, new Listener() { 57 62 public void handleEvent(Event event) { 63 CellLabelProvider provider = viewer 64 .getViewerColumn(event.index).getLabelProvider(); 65 Object element = event.item.getData(); 66 67 if (provider instanceof OwnerDrawLabelProvider) 68 ((OwnerDrawLabelProvider) provider).paint(event, element); 69 } 70 }); 71 72 viewer.getControl().addListener(SWT.EraseItem, new Listener() { 73 78 public void handleEvent(Event event) { 79 80 CellLabelProvider provider = getLabelProvider(viewer, event); 81 Object element = getElement(event); 82 83 if (provider instanceof OwnerDrawLabelProvider) 84 ((OwnerDrawLabelProvider) provider).erase(event, element); 85 86 } 87 88 94 private Object getElement(Event event) { 95 return event.item.getData(); 96 } 97 98 105 private CellLabelProvider getLabelProvider( 106 final ColumnViewer viewer, Event event) { 107 return viewer.getViewerColumn(event.index).getLabelProvider(); 108 } 109 }); 110 } 111 112 125 protected void erase(Event event, Object element) { 126 127 Rectangle bounds = event.getBounds(); 128 if ((event.detail & SWT.SELECTED) != 0) { 129 130 Color oldForeground = event.gc.getForeground(); 131 Color oldBackground = event.gc.getBackground(); 132 133 event.gc.setBackground(event.item.getDisplay().getSystemColor( 134 SWT.COLOR_LIST_SELECTION)); 135 event.gc.setForeground(event.item.getDisplay().getSystemColor( 136 SWT.COLOR_LIST_SELECTION_TEXT)); 137 event.gc.fillRectangle(bounds); 138 139 event.gc.setForeground(oldForeground); 140 event.gc.setBackground(oldBackground); 141 142 event.detail &= ~SWT.SELECTED; 143 144 } 145 146 } 147 148 157 protected abstract void paint(Event event, Object element); 158 159 168 protected abstract void measure(Event event, Object element); 169 170 174 public OwnerDrawLabelProvider() { 175 176 } 177 178 183 public void update(ViewerCell cell) { 184 Rectangle cellBounds = cell.getBounds(); 186 cell.getControl().redraw(cellBounds.x, cellBounds.y, cellBounds.width, 187 cellBounds.height, true); 188 189 } 190 191 } 192 | Popular Tags |