1 13 14 package org.eclipse.jface.viewers; 15 16 import org.eclipse.core.runtime.Assert; 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.graphics.Color; 19 import org.eclipse.swt.graphics.GC; 20 import org.eclipse.swt.graphics.Rectangle; 21 import org.eclipse.swt.widgets.Event; 22 import org.eclipse.swt.widgets.Listener; 23 24 28 public class FocusCellOwnerDrawHighlighter extends FocusCellHighlighter { 29 30 private ViewerCell oldCell; 31 32 private static final boolean WIN_32 = "win32".equals(SWT.getPlatform()); 35 39 public FocusCellOwnerDrawHighlighter(ColumnViewer viewer) { 40 super(viewer); 41 hookListener(viewer); 42 } 43 44 private void markFocusedCell(Event event, ViewerCell cell) { 45 Color background = getSelectedCellBackgroundColor(cell); 46 Color foreground = getSelectedCellForegroundColor(cell); 47 48 if ( WIN_32 || foreground != null || background != null) { 49 GC gc = event.gc; 50 51 if (background == null) { 52 background = cell.getItem().getDisplay().getSystemColor( 53 SWT.COLOR_LIST_SELECTION); 54 } 55 56 if (foreground == null) { 57 foreground = cell.getItem().getDisplay().getSystemColor( 58 SWT.COLOR_LIST_SELECTION_TEXT); 59 } 60 61 gc.setBackground(background); 62 gc.setForeground(foreground); 63 gc.fillRectangle(event.getBounds()); 64 65 gc.drawText(" ", cell.getBounds().x, cell.getBounds().y, false); event.detail &= ~SWT.SELECTED; 68 } 69 } 70 71 private void removeSelectionInformation(Event event, ViewerCell cell) { 72 GC gc = event.gc; 73 gc.setBackground(cell.getViewerRow().getBackground( 74 cell.getColumnIndex())); 75 gc.setForeground(cell.getViewerRow().getForeground( 76 cell.getColumnIndex())); 77 gc.fillRectangle(cell.getBounds()); 78 gc.drawText(" ", cell.getBounds().x, cell.getBounds().y, false); event.detail &= ~SWT.SELECTED; 81 } 82 83 private void hookListener(final ColumnViewer viewer) { 84 85 Listener listener = new Listener() { 86 87 public void handleEvent(Event event) { 88 if ((event.detail & SWT.SELECTED) > 0) { 89 ViewerCell focusCell = getFocusCell(); 90 ViewerRow row = viewer.getViewerRowFromItem(event.item); 91 92 Assert 93 .isNotNull(row, 94 "Internal structure invalid. Item without associated row is not possible."); 96 ViewerCell cell = row.getCell(event.index); 97 98 if (focusCell == null || !cell.equals(focusCell)) { 99 removeSelectionInformation(event, cell); 100 } else { 101 markFocusedCell(event, cell); 102 } 103 } 104 } 105 106 }; 107 viewer.getControl().addListener(SWT.EraseItem, listener); 108 } 109 110 115 protected Color getSelectedCellBackgroundColor(ViewerCell cell) { 116 return null; 117 } 118 119 124 protected Color getSelectedCellForegroundColor(ViewerCell cell) { 125 return null; 126 } 127 128 133 protected void focusCellChanged(ViewerCell cell) { 134 super.focusCellChanged(cell); 135 136 if (cell != null) { 138 Rectangle rect = cell.getBounds(); 139 int x = cell.getColumnIndex() == 0 ? 0 : rect.x; 140 int width = cell.getColumnIndex() == 0 ? rect.x + rect.width 141 : rect.width; 142 cell.getControl().redraw(x, rect.y-1, width, rect.height+1, true); 144 } 145 146 if (oldCell != null) { 147 Rectangle rect = oldCell.getBounds(); 148 int x = oldCell.getColumnIndex() == 0 ? 0 : rect.x; 149 int width = oldCell.getColumnIndex() == 0 ? rect.x + rect.width 150 : rect.width; 151 oldCell.getControl().redraw(x, rect.y-1, width, rect.height+1, true); 153 } 154 155 this.oldCell = cell; 156 } 157 } 158 | Popular Tags |