/******************************************************************************* * Copyright (c) 2007 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Tom Schindl - initial API and implementation * - fix for bug 183850, 182652 * IBM Corporation - initial API and implementation ******************************************************************************/ package org.eclipse.jface.viewers; import org.eclipse.core.runtime.Assert; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; /** * @since 3.3 * */ public class FocusCellMyOwnerDrawHighlighter extends FocusCellHighlighter { private ViewerCell oldCell; // Needed to work-around problem in bug 183850 private static final boolean WIN_32 = "win32".equals(SWT.getPlatform()); //$NON-NLS-1$ private ColumnViewer viewer; private int index; private Object item; /** * @param viewer * the viewer */ public FocusCellMyOwnerDrawHighlighter(ColumnViewer viewer) { super(viewer); this.viewer = viewer; hookListener(); } private void markFocusedCell(Event event, ViewerCell cell) { Color background = null; Color foreground = null; if ( WIN_32 || foreground != null || background != null) { GC gc = event.gc; if (background == null) { background = cell.getItem().getDisplay().getSystemColor( SWT.COLOR_LIST_SELECTION); } if (foreground == null) { foreground = cell.getItem().getDisplay().getSystemColor( SWT.COLOR_LIST_SELECTION_TEXT); } gc.setBackground(background); gc.setForeground(foreground); gc.fillRectangle(event.getBounds()); // This is a workaround for an SWT-Bug on WinXP bug 169517 gc.drawText(" ", cell.getBounds().x, cell.getBounds().y, false); //$NON-NLS-1$ event.detail &= ~SWT.SELECTED; } } private void removeSelectionInformation(Event event, ViewerCell cell) { Color background = getSelectedCellBackgroundColor(cell); Color foreground = getSelectedCellForegroundColor(cell); GC gc = event.gc; if(background == null) { background = cell.getViewerRow().getBackground( cell.getColumnIndex()); } if(foreground == null) { foreground = cell.getViewerRow().getForeground( cell.getColumnIndex()); } gc.setBackground(background); gc.setForeground(foreground); gc.fillRectangle(cell.getBounds()); // This is a workaround for an SWT-Bug on WinXP bug 169517 gc.drawText(" ", cell.getBounds().x, cell.getBounds().y, false); //$NON-NLS-1$ event.detail &= ~SWT.SELECTED; } private void hookListener() { Listener listener = new Listener() { public void handleEvent(Event event) { if ((event.detail & SWT.SELECTED) > 0) { ViewerCell focusCell = getFocusCell(); ViewerRow row = viewer.getViewerRowFromItem(event.item); Assert .isNotNull(row, "Internal structure invalid. Item without associated row is not possible."); //$NON-NLS-1$ ViewerCell cell = row.getCell(event.index); // if (focusCell == null || !cell.equals(focusCell)) { // removeSelectionInformation(event, cell); // } else { // markFocusedCell(event, cell); // } // if (cell.equals(focusCell)) { removeSelectionInformation(event, cell); notifyTraverse(event); } else { markFocusedCell(event, cell); } } } }; viewer.getControl().addListener(SWT.EraseItem, listener); } /** * Send event to viewer's control (SWT.Traverse) * @param event */ protected void notifyTraverse(Event event){ if(event.index != index || event.item != item){ index = event.index; item = event.item; viewer.getControl().notifyListeners(SWT.Traverse, event); } } /** * @param cell * the cell which is colored * @return the color */ protected Color getSelectedCellBackgroundColor(ViewerCell cell) { return null; } /** * @param cell * the cell which is colored * @return the color */ protected Color getSelectedCellForegroundColor(ViewerCell cell) { return null; } /* * (non-Javadoc) * * @see org.eclipse.jface.viewers.FocusCellHighlighter#focusCellChanged(org.eclipse.jface.viewers.ViewerCell) */ protected void focusCellChanged(ViewerCell cell) { super.focusCellChanged(cell); // Redraw new area if (cell != null) { redrawArea(cell); } if (oldCell != null) { redrawArea(oldCell); } this.oldCell = cell; } private void redrawArea(ViewerCell cell){ Rectangle rect = cell.getBounds(); int x = cell.getColumnIndex() == 0 ? 0 : rect.x; int width = cell.getColumnIndex() == 0 ? rect.x + rect.width : rect.width; // 1 is a fix for Linux-GTK cell.getControl().redraw(x, rect.y-1, width, rect.height+1, true); } }