Skip to content

Commit 5f190d9

Browse files
Ensure that colors from registry are not null
The workbench colors will not always be initialized in the JFace area, for example if the workbench is not started. To make sure there are no NPE, the system colors are used as default. Fixes eclipse-platform#1955, eclipse-platform/eclipse.platform.swt#1275
1 parent 521543f commit 5f190d9

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/OwnerDrawLabelProvider.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,11 @@ public void update(ViewerCell cell) {
170170
/**
171171
* Handle the erase event. The default implementation colors the background of
172172
* selected areas with "org.eclipse.ui.workbench.SELECTED_CELL_BACKGROUND" and
173-
* foregrounds with "org.eclipse.ui.workbench.SELECTED_CELL_FOREGROUND". Note
174-
* that this implementation causes non-native behavior on some platforms.
175-
* Subclasses should override this method and <b>not</b> call the super
176-
* implementation.
173+
* foregrounds with "org.eclipse.ui.workbench.SELECTED_CELL_FOREGROUND" if these
174+
* colors are available, if not {@link SWT#COLOR_LIST_SELECTION} and
175+
* {@link SWT#COLOR_LIST_SELECTION_TEXT} is used. Note that this implementation
176+
* causes non-native behavior on some platforms. Subclasses should override this
177+
* method and <b>not</b> call the super implementation.
177178
*
178179
* @param event the erase event
179180
* @param element the model object
@@ -189,11 +190,27 @@ protected void erase(Event event, Object element) {
189190

190191
ColorRegistry colorRegistry = JFaceResources.getColorRegistry();
191192
if (event.widget instanceof Control control && control.isFocusControl()) {
192-
event.gc.setBackground(colorRegistry.get("org.eclipse.ui.workbench.SELECTED_CELL_BACKGROUND")); //$NON-NLS-1$
193-
event.gc.setForeground(colorRegistry.get("org.eclipse.ui.workbench.SELECTED_CELL_FOREGROUND")); //$NON-NLS-1$
193+
Color background = colorRegistry.get("org.eclipse.ui.workbench.SELECTED_CELL_BACKGROUND"); //$NON-NLS-1$
194+
if (background == null) {
195+
background = event.item.getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION);
196+
}
197+
Color foreground = colorRegistry.get("org.eclipse.ui.workbench.SELECTED_CELL_FOREGROUND"); //$NON-NLS-1$
198+
if (foreground == null) {
199+
foreground = event.item.getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT);
200+
}
201+
event.gc.setBackground(background);
202+
event.gc.setForeground(foreground);
194203
} else {
195-
event.gc.setBackground(colorRegistry.get("org.eclipse.ui.workbench.SELECTED_CELL_BACKGROUND_NO_FOCUS")); //$NON-NLS-1$
196-
event.gc.setForeground(colorRegistry.get("org.eclipse.ui.workbench.SELECTED_CELL_FOREGROUND_NO_FOCUS")); //$NON-NLS-1$
204+
Color background = colorRegistry.get("org.eclipse.ui.workbench.SELECTED_CELL_BACKGROUND_NO_FOCUS"); //$NON-NLS-1$
205+
if (background == null) {
206+
background = event.item.getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION);
207+
}
208+
Color foreground = colorRegistry.get("org.eclipse.ui.workbench.SELECTED_CELL_FOREGROUND_NO_FOCUS"); //$NON-NLS-1$
209+
if (foreground == null) {
210+
foreground = event.item.getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT);
211+
}
212+
event.gc.setBackground(background);
213+
event.gc.setForeground(foreground);
197214
}
198215
event.gc.fillRectangle(bounds);
199216

bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/internal/ColumnViewerSelectionColorListener.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.eclipse.jface.viewers.FocusCellOwnerDrawHighlighter;
1919
import org.eclipse.jface.viewers.StructuredViewer;
2020
import org.eclipse.swt.SWT;
21+
import org.eclipse.swt.graphics.Color;
2122
import org.eclipse.swt.graphics.GC;
2223
import org.eclipse.swt.widgets.Control;
2324
import org.eclipse.swt.widgets.Event;
@@ -63,11 +64,27 @@ public void handleEvent(Event event) {
6364
GC gc = event.gc;
6465
ColorRegistry colorRegistry = JFaceResources.getColorRegistry();
6566
if (event.widget instanceof Control control && control.isFocusControl()) {
66-
gc.setBackground(colorRegistry.get("org.eclipse.ui.workbench.SELECTED_CELL_BACKGROUND")); //$NON-NLS-1$
67-
gc.setForeground(colorRegistry.get("org.eclipse.ui.workbench.SELECTED_CELL_FOREGROUND")); //$NON-NLS-1$
67+
Color background = colorRegistry.get("org.eclipse.ui.workbench.SELECTED_CELL_BACKGROUND"); //$NON-NLS-1$
68+
if (background == null) {
69+
background = event.item.getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION);
70+
}
71+
Color foreground = colorRegistry.get("org.eclipse.ui.workbench.SELECTED_CELL_FOREGROUND"); //$NON-NLS-1$
72+
if (foreground == null) {
73+
foreground = event.item.getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT);
74+
}
75+
event.gc.setBackground(background);
76+
event.gc.setForeground(foreground);
6877
} else {
69-
gc.setBackground(colorRegistry.get("org.eclipse.ui.workbench.SELECTED_CELL_BACKGROUND_NO_FOCUS")); //$NON-NLS-1$
70-
gc.setForeground(colorRegistry.get("org.eclipse.ui.workbench.SELECTED_CELL_FOREGROUND_NO_FOCUS")); //$NON-NLS-1$
78+
Color background = colorRegistry.get("org.eclipse.ui.workbench.SELECTED_CELL_BACKGROUND_NO_FOCUS"); //$NON-NLS-1$
79+
if (background == null) {
80+
background = event.item.getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION);
81+
}
82+
Color foreground = colorRegistry.get("org.eclipse.ui.workbench.SELECTED_CELL_FOREGROUND_NO_FOCUS"); //$NON-NLS-1$
83+
if (foreground == null) {
84+
foreground = event.item.getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT);
85+
}
86+
event.gc.setBackground(background);
87+
event.gc.setForeground(foreground);
7188
}
7289

7390
int width = event.width;

0 commit comments

Comments
 (0)