/******************************************************************************* * Copyright (c) 2006 Tom Schindl 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 *******************************************************************************/ package org.eclipse.jface.snippets.viewers; import org.eclipse.core.runtime.Assert; import org.eclipse.jface.viewers.ColumnViewer; import org.eclipse.jface.viewers.ColumnViewerEditor; import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent; import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy; import org.eclipse.jface.viewers.FocusCellHighlighter; import org.eclipse.jface.viewers.IStructuredContentProvider; import org.eclipse.jface.viewers.OwnerDrawLabelProvider; import org.eclipse.jface.viewers.TableViewer; import org.eclipse.jface.viewers.TableViewerEditor; import org.eclipse.jface.viewers.TableViewerFocusCellManager; import org.eclipse.jface.viewers.Viewer; import org.eclipse.jface.viewers.ViewerCell; import org.eclipse.jface.viewers.ViewerRow; 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.layout.FillLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.TableColumn; public class WorkingSnippetOwnerDrawThings { private class MyContentProvider implements IStructuredContentProvider { /* * (non-Javadoc) * * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object) */ public Object[] getElements(Object inputElement) { return (MyModel[]) inputElement; } /* * (non-Javadoc) * * @see org.eclipse.jface.viewers.IContentProvider#dispose() */ public void dispose() { } /* * (non-Javadoc) * * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, * java.lang.Object, java.lang.Object) */ public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { } } public static boolean flag = true; public class MyModel { public int counter; public int numberOfModels; public MyModel(int counter, int numberOfModels) { this.counter = counter; this.numberOfModels =numberOfModels; } public String toString() { return "Item " + this.counter; } public Color getColor(){ return new Color(Display.getCurrent(), counter * 255/numberOfModels, 0, 0); } } public class MyLabelProvider extends OwnerDrawLabelProvider{ private TableViewer _tableViewer; public MyLabelProvider(TableViewer v) { super(); _tableViewer = v; } @Override protected void measure(Event event, Object element) { } @Override protected void paint(Event event, Object element) { if ((event.detail & SWT.SELECTED) > 0) return; Color color = ((MyModel) element).getColor(); Color colorByColumns = new Color(Display.getCurrent(), color.getRed(), event.index*255/_tableViewer.getTable().getColumnCount(), color.getBlue()); event.gc.setBackground(colorByColumns); Rectangle bounds = event.getBounds(); bounds.width = _tableViewer.getTable().getColumn(event.index).getWidth(); event.gc.fillRectangle(bounds); } @Override protected void erase(Event event, Object element) { //super.erase(event, element); paint(event,element); } } /* * (non-Javadoc) * * @see org.eclipse.jface.viewers.FocusCellOwnerDrawHighlighter */ public class MyFocusCellHighlighter extends FocusCellHighlighter { private ViewerCell oldCell; // Needed to work-around problem in bug 183850 private final boolean WIN_32 = "win32".equals(SWT.getPlatform()); //$NON-NLS-1$ /** * @param viewer * the viewer */ public MyFocusCellHighlighter(TableViewer viewer) { super(viewer); hookListener(viewer); } private void markFocusedCell(Event event, ViewerCell cell) { Color background = getSelectedCellBackgroundColor(cell); Color foreground = getSelectedCellForegroundColor(cell); 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); Rectangle rect = event.getBounds(); gc.setAdvanced(true); gc.setAlpha(128); gc.fillRectangle(rect); gc.setLineWidth(5); gc.setLineStyle(SWT.LINE_DOT); gc.drawRectangle(rect); // 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) { GC gc = event.gc; gc.setBackground(cell.getViewerRow().getBackground( cell.getColumnIndex())); gc.setForeground(cell.getViewerRow().getForeground( cell.getColumnIndex())); 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(final ColumnViewer viewer) { Listener listener = new Listener() { public void handleEvent(Event event) { if ((event.detail & SWT.SELECTED) > 0) { ViewerCell focusCell = getFocusCell(); ViewerRow row = focusCell.getViewerRow(); 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); } } } }; viewer.getControl().addListener(SWT.EraseItem, listener); } /** * @param cell * the cell which is colored * @return the color */ protected Color getSelectedCellBackgroundColor(ViewerCell cell) { return new Color(Display.getCurrent(),255,255,255 ); } /** * @param cell * the cell which is colored * @return the color */ protected Color getSelectedCellForegroundColor(ViewerCell cell) { return new Color(Display.getCurrent(),0,0,0 ); } /* * (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) { 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); } if (oldCell != null) { Rectangle rect = oldCell.getBounds(); int x = oldCell.getColumnIndex() == 0 ? 0 : rect.x; int width = oldCell.getColumnIndex() == 0 ? rect.x + rect.width : rect.width; // 1 is a fix for Linux-GTK oldCell.getControl().redraw(x, rect.y-1, width, rect.height+1, true); } this.oldCell = cell; } } public WorkingSnippetOwnerDrawThings(Shell shell) { final TableViewer v = new TableViewer(shell, SWT.BORDER|SWT.HIDE_SELECTION|SWT.FULL_SELECTION); v.setContentProvider(new MyContentProvider()); TableViewerFocusCellManager focusCellManager = new TableViewerFocusCellManager(v,new MyFocusCellHighlighter(v)); ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(v) { protected boolean isEditorActivationEvent( ColumnViewerEditorActivationEvent event) { return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL || event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION || (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED && event.keyCode == SWT.CR) || event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC; } }; TableViewerEditor.create(v, focusCellManager, actSupport, ColumnViewerEditor.TABBING_HORIZONTAL | ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR | ColumnViewerEditor.TABBING_VERTICAL | ColumnViewerEditor.KEYBOARD_ACTIVATION); v.setLabelProvider(new MyLabelProvider(v)); MyLabelProvider.setUpOwnerDraw(v); TableColumn column; for (int i = 0; i < 3; i++) { column = new TableColumn(v.getTable(), SWT.NONE); column.setWidth(200); column.setText("Column "+i); } MyModel[] model = createModel(); v.setInput(model); v.getTable().setLinesVisible(true); v.getTable().setHeaderVisible(true); } private MyModel[] createModel() { int numberOfModels = 10; MyModel[] elements = new MyModel[numberOfModels]; for (int i = 0; i < numberOfModels; i++) { elements[i] = new MyModel(i, numberOfModels); } return elements; } /** * @param args */ public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); new WorkingSnippetOwnerDrawThings(shell); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } }