KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > viewers > FocusCellOwnerDrawHighlighter


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
10  * - fix for bug 183850, 182652
11  * IBM Corporation - initial API and implementation
12  ******************************************************************************/

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 /**
25  * @since 3.3
26  *
27  */

28 public class FocusCellOwnerDrawHighlighter extends FocusCellHighlighter {
29
30     private ViewerCell oldCell;
31
32     // Needed to work-around problem in bug 183850
33
private static final boolean WIN_32 = "win32".equals(SWT.getPlatform()); //$NON-NLS-1$
34

35     /**
36      * @param viewer
37      * the viewer
38      */

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             // This is a workaround for an SWT-Bug on WinXP bug 169517
66
gc.drawText(" ", cell.getBounds().x, cell.getBounds().y, false); //$NON-NLS-1$
67
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         // This is a workaround for an SWT-Bug on WinXP bug 169517
79
gc.drawText(" ", cell.getBounds().x, cell.getBounds().y, false); //$NON-NLS-1$
80
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."); //$NON-NLS-1$
95

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     /**
111      * @param cell
112      * the cell which is colored
113      * @return the color
114      */

115     protected Color getSelectedCellBackgroundColor(ViewerCell cell) {
116         return null;
117     }
118
119     /**
120      * @param cell
121      * the cell which is colored
122      * @return the color
123      */

124     protected Color getSelectedCellForegroundColor(ViewerCell cell) {
125         return null;
126     }
127
128     /*
129      * (non-Javadoc)
130      *
131      * @see org.eclipse.jface.viewers.FocusCellHighlighter#focusCellChanged(org.eclipse.jface.viewers.ViewerCell)
132      */

133     protected void focusCellChanged(ViewerCell cell) {
134         super.focusCellChanged(cell);
135
136         // Redraw new area
137
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             // 1 is a fix for Linux-GTK
143
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             // 1 is a fix for Linux-GTK
152
oldCell.getControl().redraw(x, rect.y-1, width, rect.height+1, true);
153         }
154
155         this.oldCell = cell;
156     }
157 }
158
Popular Tags