25
25
import org .eclipse .swt .widgets .Table ;
26
26
import org .eclipse .swt .widgets .TableItem ;
27
27
28
+ import org .eclipse .jface .resource .ColorRegistry ;
29
+ import org .eclipse .jface .resource .JFaceResources ;
30
+
28
31
29
32
/**
30
33
* Adds owner draw support for tables.
@@ -42,6 +45,10 @@ public class TableOwnerDrawSupport implements Listener {
42
45
43
46
public static void install (Table table ) {
44
47
TableOwnerDrawSupport listener = new TableOwnerDrawSupport (table );
48
+ installListener (table , listener );
49
+ }
50
+
51
+ protected static void installListener (Table table , Listener listener ) {
45
52
table .addListener (SWT .Dispose , listener );
46
53
table .addListener (SWT .MeasureItem , listener );
47
54
table .addListener (SWT .EraseItem , listener );
@@ -70,7 +77,7 @@ private static StyleRange[] getStyledRanges(TableItem item, int column) {
70
77
return (StyleRange [])item .getData (STYLED_RANGES_KEY + column );
71
78
}
72
79
73
- private TableOwnerDrawSupport (Table table ) {
80
+ protected TableOwnerDrawSupport (Table table ) {
74
81
int orientation = table .getStyle () & (SWT .LEFT_TO_RIGHT | SWT .RIGHT_TO_LEFT );
75
82
fSharedLayout = new TextLayout (table .getDisplay ());
76
83
fSharedLayout .setOrientation (orientation );
@@ -147,7 +154,28 @@ private void performPaint(Event event) {
147
154
Color oldForeground = gc .getForeground ();
148
155
Color oldBackground = gc .getBackground ();
149
156
150
- if (!isSelected ) {
157
+ if (isSelected ) {
158
+ Color background = item .getParent ().isFocusControl ()
159
+ ? getSelectedRowBackgroundColor ()
160
+ : getSelectedRowBackgroundColorNoFocus ();
161
+ Color foreground = item .getParent ().isFocusControl ()
162
+ ? getSelectedRowForegroundColor ()
163
+ : getSelectedRowForegroundColorNoFocus ();
164
+
165
+ if (background == null ) {
166
+ background = item .getDisplay ().getSystemColor (
167
+ SWT .COLOR_LIST_SELECTION );
168
+ }
169
+
170
+ if (foreground == null ) {
171
+ foreground = item .getDisplay ().getSystemColor (
172
+ SWT .COLOR_LIST_SELECTION_TEXT );
173
+ }
174
+
175
+ gc .setBackground (background );
176
+ gc .setForeground (foreground );
177
+ gc .fillRectangle (0 , event .y , item .getParent ().getBounds ().width , event .height );
178
+ } else {
151
179
Color foreground = item .getForeground (index );
152
180
gc .setForeground (foreground );
153
181
@@ -178,10 +206,54 @@ private void performPaint(Event event) {
178
206
gc .drawFocus (focusBounds .x , focusBounds .y , focusBounds .width + fDeltaOfLastMeasure , focusBounds .height );
179
207
}
180
208
181
- if (!isSelected ) {
182
- gc .setForeground (oldForeground );
183
- gc .setBackground (oldBackground );
184
- }
209
+ gc .setForeground (oldForeground );
210
+ gc .setBackground (oldBackground );
211
+ }
212
+
213
+ /**
214
+ * The color to use when rendering the background of the selected row when the control has the
215
+ * input focus
216
+ *
217
+ * @return the color or <code>null</code> to use the default
218
+ */
219
+ protected Color getSelectedRowBackgroundColor () {
220
+ ColorRegistry colorRegistry = JFaceResources .getColorRegistry ();
221
+ return colorRegistry .get ("org.eclipse.ui.workbench.SELECTED_CELL_BACKGROUND" ); //$NON-NLS-1$
222
+ }
223
+
224
+ /**
225
+ * The color to use when rendering the foreground (=text) of the selected row when the control
226
+ * has the input focus
227
+ *
228
+ * @return the color or <code>null</code> to use the default
229
+ */
230
+ protected Color getSelectedRowForegroundColor () {
231
+ ColorRegistry colorRegistry = JFaceResources .getColorRegistry ();
232
+ return colorRegistry .get ("org.eclipse.ui.workbench.SELECTED_CELL_FOREGROUND" ); //$NON-NLS-1$
233
+ }
234
+
235
+ /**
236
+ * The color to use when rendering the foreground (=text) of the selected row when the control
237
+ * has <b>no</b> input focus
238
+ *
239
+ * @return the color or <code>null</code> to use the same used when control has focus
240
+ * @since 3.4
241
+ */
242
+ protected Color getSelectedRowForegroundColorNoFocus () {
243
+ ColorRegistry colorRegistry = JFaceResources .getColorRegistry ();
244
+ return colorRegistry .get ("org.eclipse.ui.workbench.SELECTED_CELL_FOREGROUND_NO_FOCUS" ); //$NON-NLS-1$
245
+ }
246
+
247
+ /**
248
+ * The color to use when rendering the background of the selected row when the control has
249
+ * <b>no</b> input focus
250
+ *
251
+ * @return the color or <code>null</code> to use the same used when control has focus
252
+ * @since 3.4
253
+ */
254
+ protected Color getSelectedRowBackgroundColorNoFocus () {
255
+ ColorRegistry colorRegistry = JFaceResources .getColorRegistry ();
256
+ return colorRegistry .get ("org.eclipse.ui.workbench.SELECTED_CELL_BACKGROUND_NO_FOCUS" ); //$NON-NLS-1$
185
257
}
186
258
187
259
private void widgetDisposed () {
0 commit comments