Skip to content

Commit 41cf245

Browse files
committed
Deprecate old Color constructors that require a Device/Display parameter
The constructors new Color(Device, int, int, int), new Color(Device, RGB), and new Color(Device, RGBA) (and variants with alpha) are now deprecated. Modern display-free equivalents should be used instead. This reduces boilerplate, avoids NPE risks on non-UI threads, and simplifies resource management as these colors do not require manual disposal. Fixes #3232
1 parent 124ec08 commit 41cf245

10 files changed

Lines changed: 74 additions & 35 deletions

File tree

bundles/org.eclipse.swt/Eclipse SWT Accessibility/win32/org/eclipse/swt/accessibility/Accessible.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5099,7 +5099,7 @@ Color colorFromString(String rgbString) {
50995099
int r = Integer.parseInt(rgbString.substring(open + 1, comma1));
51005100
int g = Integer.parseInt(rgbString.substring(comma1 + 1, comma2));
51015101
int b = Integer.parseInt(rgbString.substring(comma2 + 1, close));
5102-
return new Color(control.getDisplay(), r, g, b);
5102+
return new Color(r, g, b);
51035103
} catch (NumberFormatException ex) {}
51045104
return null;
51055105
}

bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/ControlEditor.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@
3131
* <pre><code>
3232
* Canvas canvas = new Canvas(shell, SWT.BORDER);
3333
* canvas.setBounds(10, 10, 300, 300);
34-
* Color color = new Color(null, 255, 0, 0);
35-
* canvas.setBackground(color);
36-
* ControlEditor editor = new ControlEditor (canvas);
34+
* Color color = new Color(255, 0, 0);
35+
* canvas.setBackground(color);* ControlEditor editor = new ControlEditor (canvas);
3736
* // The editor will be a button in the bottom right corner of the canvas.
3837
* // When selected, it will launch a Color dialog that will change the background
3938
* // of the canvas.
@@ -46,9 +45,8 @@
4645
* RGB rgb = dialog.getRGB();
4746
* if (rgb != null) {
4847
* if (color != null) color.dispose();
49-
* color = new Color(null, rgb);
50-
* canvas.setBackground(color);
51-
* }
48+
* color = new Color(rgb);
49+
* canvas.setBackground(color);* }
5250
*
5351
* }
5452
* });

bundles/org.eclipse.swt/Eclipse SWT Drag and Drop/cocoa/org/eclipse/swt/dnd/DragSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ void drag(Event dragDetectEvent) {
361361
int width = 20, height = 20;
362362
Image newDragImage = new Image(Display.getCurrent(), width, height);
363363
GC imageGC = new GC(newDragImage);
364-
Color grayColor = new Color(Display.getCurrent(), 50, 50, 50);
364+
Color grayColor = new Color(50, 50, 50);
365365
imageGC.setForeground(grayColor);
366366
imageGC.drawRectangle(0, 0, 19, 19);
367367
imageGC.dispose();

bundles/org.eclipse.swt/Eclipse SWT PI/win32/org/eclipse/swt/internal/win32/OS.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2266,16 +2266,16 @@ public static final void setTheme(boolean isDarkTheme) {
22662266

22672267
display.setData("org.eclipse.swt.internal.win32.useDarkModeExplorerTheme", isDarkTheme);
22682268
display.setData("org.eclipse.swt.internal.win32.useShellTitleColoring", isDarkTheme);
2269-
display.setData("org.eclipse.swt.internal.win32.menuBarForegroundColor", isDarkTheme ? new Color(display, 0xD0, 0xD0, 0xD0) : null);
2270-
display.setData("org.eclipse.swt.internal.win32.menuBarBackgroundColor", isDarkTheme ? new Color(display, 0x30, 0x30, 0x30) : null);
2271-
display.setData("org.eclipse.swt.internal.win32.menuBarBorderColor", isDarkTheme ? new Color(display, 0x50, 0x50, 0x50) : null);
2269+
display.setData("org.eclipse.swt.internal.win32.menuBarForegroundColor", isDarkTheme ? new Color(0xD0, 0xD0, 0xD0) : null);
2270+
display.setData("org.eclipse.swt.internal.win32.menuBarBackgroundColor", isDarkTheme ? new Color(0x30, 0x30, 0x30) : null);
2271+
display.setData("org.eclipse.swt.internal.win32.menuBarBorderColor", isDarkTheme ? new Color(0x50, 0x50, 0x50) : null);
22722272
display.setData("org.eclipse.swt.internal.win32.Canvas.use_WS_BORDER", isDarkTheme);
22732273
display.setData("org.eclipse.swt.internal.win32.List.use_WS_BORDER", isDarkTheme);
22742274
display.setData("org.eclipse.swt.internal.win32.Table.use_WS_BORDER", isDarkTheme);
22752275
display.setData("org.eclipse.swt.internal.win32.Text.use_WS_BORDER", isDarkTheme);
22762276
display.setData("org.eclipse.swt.internal.win32.Tree.use_WS_BORDER", isDarkTheme);
2277-
display.setData("org.eclipse.swt.internal.win32.Table.headerLineColor", isDarkTheme ? new Color(display, 0x50, 0x50, 0x50) : null);
2278-
display.setData("org.eclipse.swt.internal.win32.Label.disabledForegroundColor", isDarkTheme ? new Color(display, 0x80, 0x80, 0x80) : null);
2277+
display.setData("org.eclipse.swt.internal.win32.Table.headerLineColor", isDarkTheme ? new Color(0x50, 0x50, 0x50) : null);
2278+
display.setData("org.eclipse.swt.internal.win32.Label.disabledForegroundColor", isDarkTheme ? new Color(0x80, 0x80, 0x80) : null);
22792279
display.setData("org.eclipse.swt.internal.win32.Combo.useDarkTheme", isDarkTheme);
22802280
display.setData("org.eclipse.swt.internal.win32.ProgressBar.useColors", isDarkTheme);
22812281
display.setData("org.eclipse.swt.internal.win32.Text.useDarkThemeIcons", isDarkTheme);

bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Color.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ public final class Color extends Resource {
6868
* </ul>
6969
*
7070
* @see #Color(int, int, int) The equivalent constructor not requiring a Device
71+
* @deprecated Use {@link #Color(int, int, int)}
7172
*/
73+
@Deprecated
7274
public Color(Device device, int red, int green, int blue) {
7375
super(device);
7476
init(red, green, blue, 255);
@@ -113,7 +115,9 @@ public Color(int red, int green, int blue) {
113115
* @see #Color(int, int, int, int) The equivalent constructor not requiring a Device
114116
*
115117
* @since 3.104
118+
* @deprecated Use {@link #Color(int, int, int, int)}
116119
*/
120+
@Deprecated
117121
public Color(Device device, int red, int green, int blue, int alpha) {
118122
super(device);
119123
init(red, green, blue, alpha);
@@ -155,7 +159,9 @@ public Color(int red, int green, int blue, int alpha) {
155159
* </ul>
156160
*
157161
* @see #Color(RGB) The equivalent constructor not requiring a Device
162+
* @deprecated Use {@link #Color(RGB)}
158163
*/
164+
@Deprecated
159165
public Color(Device device, RGB rgb) {
160166
super(device);
161167
if (rgb == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
@@ -197,7 +203,9 @@ public Color(RGB rgb) {
197203
* @see #Color(RGBA) The equivalent constructor not requiring a Device
198204
*
199205
* @since 3.104
206+
* @deprecated Use {@link #Color(RGBA)}
200207
*/
208+
@Deprecated
201209
public Color(Device device, RGBA rgba) {
202210
super(device);
203211
if (rgba == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
@@ -242,7 +250,9 @@ public Color(RGBA rgba) {
242250
* @see #Color(RGB, int) The equivalent constructor not requiring a Device
243251
*
244252
* @since 3.104
253+
* @deprecated Use {@link #Color(RGB, int)}
245254
*/
255+
@Deprecated
246256
public Color(Device device, RGB rgb, int alpha) {
247257
super(device);
248258
if (rgb == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);

bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Device.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -594,23 +594,23 @@ public boolean getWarnings () {
594594
*/
595595
protected void init () {
596596
/* Create the standard colors */
597-
COLOR_TRANSPARENT = new Color (this, 0xFF,0xFF,0xFF,0);
598-
COLOR_BLACK = new Color (this, 0,0,0);
599-
COLOR_DARK_RED = new Color (this, 0x80,0,0);
600-
COLOR_DARK_GREEN = new Color (this, 0,0x80,0);
601-
COLOR_DARK_YELLOW = new Color (this, 0x80,0x80,0);
602-
COLOR_DARK_BLUE = new Color (this, 0,0,0x80);
603-
COLOR_DARK_MAGENTA = new Color (this, 0x80,0,0x80);
604-
COLOR_DARK_CYAN = new Color (this, 0,0x80,0x80);
605-
COLOR_GRAY = new Color (this, 0xC0,0xC0,0xC0);
606-
COLOR_DARK_GRAY = new Color (this, 0x80,0x80,0x80);
607-
COLOR_RED = new Color (this, 0xFF,0,0);
608-
COLOR_GREEN = new Color (this, 0,0xFF,0);
609-
COLOR_YELLOW = new Color (this, 0xFF,0xFF,0);
610-
COLOR_BLUE = new Color (this, 0,0,0xFF);
611-
COLOR_MAGENTA = new Color (this, 0xFF,0,0xFF);
612-
COLOR_CYAN = new Color (this, 0,0xFF,0xFF);
613-
COLOR_WHITE = new Color (this, 0xFF,0xFF,0xFF);
597+
COLOR_TRANSPARENT = new Color (0xFF,0xFF,0xFF,0);
598+
COLOR_BLACK = new Color (0,0,0);
599+
COLOR_DARK_RED = new Color (0x80,0,0);
600+
COLOR_DARK_GREEN = new Color (0,0x80,0);
601+
COLOR_DARK_YELLOW = new Color (0x80,0x80,0);
602+
COLOR_DARK_BLUE = new Color (0,0,0x80);
603+
COLOR_DARK_MAGENTA = new Color (0x80,0,0x80);
604+
COLOR_DARK_CYAN = new Color (0,0x80,0x80);
605+
COLOR_GRAY = new Color (0xC0,0xC0,0xC0);
606+
COLOR_DARK_GRAY = new Color (0x80,0x80,0x80);
607+
COLOR_RED = new Color (0xFF,0,0);
608+
COLOR_GREEN = new Color (0,0xFF,0);
609+
COLOR_YELLOW = new Color (0xFF,0xFF,0);
610+
COLOR_BLUE = new Color (0,0,0xFF);
611+
COLOR_MAGENTA = new Color (0xFF,0,0xFF);
612+
COLOR_CYAN = new Color (0,0xFF,0xFF);
613+
COLOR_WHITE = new Color (0xFF,0xFF,0xFF);
614614

615615
paragraphStyle = (NSMutableParagraphStyle)new NSMutableParagraphStyle().alloc().init();
616616
paragraphStyle.setAlignment(OS.NSTextAlignmentLeft);

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Color.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ public final class Color extends Resource {
7070
* </ul>
7171
*
7272
* @see #Color(int, int, int) The equivalent constructor not requiring a Device
73+
* @deprecated Use {@link #Color(int, int, int)}
7374
*/
75+
@Deprecated
7476
public Color(Device device, int red, int green, int blue) {
7577
super(device);
7678
init(red, green, blue, 255);
@@ -115,7 +117,9 @@ public Color(int red, int green, int blue) {
115117
* @see #Color(int, int, int, int) The equivalent constructor not requiring a Device
116118
*
117119
* @since 3.104
120+
* @deprecated Use {@link #Color(int, int, int, int)}
118121
*/
122+
@Deprecated
119123
public Color(Device device, int red, int green, int blue, int alpha) {
120124
super(device);
121125
init(red, green, blue, alpha);
@@ -157,7 +161,9 @@ public Color(int red, int green, int blue, int alpha) {
157161
* </ul>
158162
*
159163
* @see #Color(RGB) The equivalent constructor not requiring a Device
164+
* @deprecated Use {@link #Color(RGB)}
160165
*/
166+
@Deprecated
161167
public Color(Device device, RGB rgb) {
162168
super(device);
163169
if (rgb == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
@@ -199,7 +205,9 @@ public Color(RGB rgb) {
199205
* @see #Color(RGBA) The equivalent constructor not requiring a Device
200206
*
201207
* @since 3.104
208+
* @deprecated Use {@link #Color(RGBA)}
202209
*/
210+
@Deprecated
203211
public Color(Device device, RGBA rgba) {
204212
super(device);
205213
if (rgba == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
@@ -244,7 +252,9 @@ public Color(RGBA rgba) {
244252
* @see #Color(RGB, int) The equivalent constructor not requiring a Device
245253
*
246254
* @since 3.104
255+
* @deprecated Use {@link #Color(RGB, int)}
247256
*/
257+
@Deprecated
248258
public Color(Device device, RGB rgb, int alpha) {
249259
super(device);
250260
if (rgb == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Color.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ public final class Color extends Resource {
7373
* </ul>
7474
*
7575
* @see #Color(int, int, int) The equivalent constructor not requiring a Device
76+
* @deprecated Use {@link #Color(int, int, int)}
7677
*/
78+
@Deprecated
7779
public Color (Device device, int red, int green, int blue) {
7880
super(device);
7981
init(red, green, blue, 255);
@@ -118,7 +120,9 @@ public Color(int red, int green, int blue) {
118120
* @see #Color(int, int, int, int) The equivalent constructor not requiring a Device
119121
*
120122
* @since 3.104
123+
* @deprecated Use {@link #Color(int, int, int, int)}
121124
*/
125+
@Deprecated
122126
public Color (Device device, int red, int green, int blue, int alpha) {
123127
super(device);
124128
init(red, green, blue, alpha);
@@ -160,7 +164,9 @@ public Color(int red, int green, int blue, int alpha) {
160164
* </ul>
161165
*
162166
* @see #Color(RGB) The equivalent constructor not requiring a Device
167+
* @deprecated Use {@link #Color(RGB)}
163168
*/
169+
@Deprecated
164170
public Color (Device device, RGB rgb) {
165171
super(device);
166172
if (rgb == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
@@ -202,7 +208,9 @@ public Color(RGB rgb) {
202208
* @see #Color(RGBA) The equivalent constructor not requiring a Device
203209
*
204210
* @since 3.104
211+
* @deprecated Use {@link #Color(RGBA)}
205212
*/
213+
@Deprecated
206214
public Color(Device device, RGBA rgba) {
207215
super(device);
208216
if (rgba == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
@@ -247,7 +255,9 @@ public Color(RGBA rgba) {
247255
* @see #Color(RGB, int) The equivalent constructor not requiring a Device
248256
*
249257
* @since 3.104
258+
* @deprecated Use {@link #Color(RGB, int)}
250259
*/
260+
@Deprecated
251261
public Color(Device device, RGB rgb, int alpha) {
252262
super(device);
253263
if (rgb == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/GC.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4858,7 +4858,7 @@ private class SetBackgroundOperation extends ReplaceableOperation {
48584858

48594859
SetBackgroundOperation(Color color) {
48604860
RGB rgb = color.getRGB();
4861-
this.color = new Color(color.getDevice(), rgb);
4861+
this.color = new Color(rgb);
48624862
registerForDisposal(this.color);
48634863
}
48644864

@@ -5207,7 +5207,7 @@ private class SetForegroundOperation extends ReplaceableOperation {
52075207

52085208
SetForegroundOperation(Color color) {
52095209
RGB rgb = color.getRGB();
5210-
this.color = new Color(color.getDevice(), rgb);
5210+
this.color = new Color(rgb);
52115211
registerForDisposal(this.color);
52125212
}
52135213

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Color.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public void test_ConstructorLorg_eclipse_swt_graphics_DeviceIII() {
9696
}
9797

9898
@Test
99-
@SuppressWarnings("unused")
99+
@SuppressWarnings({"unused", "deprecation"})
100100
public void test_ConstructorLorg_eclipse_swt_graphics_DeviceIII_with_device() {
101101
// Test new Color(Device device, int red, int green, int blue)
102102
// IllegalArgumentException if the red, green or blue argument is not between 0 and 255
@@ -203,7 +203,7 @@ public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLorg_eclipse_swt_gra
203203
}
204204

205205
@Test
206-
@SuppressWarnings("unused")
206+
@SuppressWarnings({"unused", "deprecation"})
207207
public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLorg_eclipse_swt_graphics_RGB_with_device() {
208208
// Test new Color(Device device, RGB rgb)
209209
// IllegalArgumentException if the red, green or blue argument is not between 0 and 255; or rgb is null
@@ -307,7 +307,7 @@ public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLorg_eclipse_swt_gra
307307
}
308308

309309
@Test
310-
@SuppressWarnings("unused")
310+
@SuppressWarnings({"unused", "deprecation"})
311311
public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLorg_eclipse_swt_graphics_RGBA_with_device() {
312312
// Test new Color(Device device, RGBA rgba)
313313
// IllegalArgumentException if the red, green, blue or alpha argument is not between 0 and 255; or rgba is null
@@ -400,6 +400,7 @@ public void test_equalsLjava_lang_Object() {
400400
}
401401

402402
@Test
403+
@SuppressWarnings("deprecation")
403404
public void test_equalsLjava_lang_Object_with_device() {
404405
Color color = new Color(display, 1, 2, 3);
405406
Color sameColor = new Color(display, 1, 2, 3);
@@ -432,6 +433,7 @@ public void test_equalsLjava_lang_Object_with_device() {
432433
}
433434

434435
@Test
436+
@SuppressWarnings("deprecation")
435437
public void test_equalsLjava_lang_Object_mix() {
436438
Color color = new Color(display, 1, 2, 3);
437439
Color sameColorNoDevice = new Color(1, 2, 3);
@@ -461,6 +463,7 @@ public void test_getBlue() {
461463
}
462464

463465
@Test
466+
@SuppressWarnings("deprecation")
464467
public void test_getBlue_with_device() {
465468
// Test Color.getBlue()
466469
Color color = new Color(display, 0, 0, 255);
@@ -475,6 +478,7 @@ public void test_getGreen() {
475478
}
476479

477480
@Test
481+
@SuppressWarnings("deprecation")
478482
public void test_getGreen_with_device() {
479483
// Test Color.getGreen()
480484
Color color = new Color(display, 0, 255, 0);
@@ -489,6 +493,7 @@ public void test_getRGB() {
489493
}
490494

491495
@Test
496+
@SuppressWarnings("deprecation")
492497
public void test_getRGB_with_device() {
493498
Color color = new Color(display, 255, 255, 255);
494499
assertNotNull(color.getRGB());
@@ -503,6 +508,7 @@ public void test_getRed() {
503508
}
504509

505510
@Test
511+
@SuppressWarnings("deprecation")
506512
public void test_getRed_with_device() {
507513
// Test Color.getRed()
508514
Color color = new Color(display, 255, 0, 0);
@@ -517,6 +523,7 @@ public void test_getAlpha() {
517523
}
518524

519525
@Test
526+
@SuppressWarnings("deprecation")
520527
public void test_getAlpha_with_device() {
521528
// Test Color.getRed()
522529
Color color = new Color(display, 255, 0, 0, 0);
@@ -533,6 +540,7 @@ public void test_hashCode() {
533540
}
534541

535542
@Test
543+
@SuppressWarnings("deprecation")
536544
public void test_hashCode_with_device() {
537545
Color color = new Color(display, 12, 34, 56, 0);
538546
Color otherColor = new Color(display, 12, 34, 56, 0);
@@ -556,6 +564,7 @@ public void test_isDisposed() {
556564
}
557565

558566
@Test
567+
@SuppressWarnings("deprecation")
559568
public void test_isDisposed_with_device() {
560569
// Test Color.isDisposed() false
561570
Color color = new Color(display, 34, 67, 98, 0);
@@ -574,6 +583,7 @@ public void test_toString() {
574583
}
575584

576585
@Test
586+
@SuppressWarnings("deprecation")
577587
public void test_toString_with_device() {
578588
Color color = new Color(display, 0, 0, 255, 255);
579589
assertNotNull(color.toString());
@@ -598,6 +608,7 @@ public void test_getDevice() {
598608
}
599609

600610
@Test
611+
@SuppressWarnings("deprecation")
601612
public void test_getDevice_with_device() {
602613
Color color = new Color(display, 0, 0, 255, 255);
603614
assertEquals(display, color.getDevice());

0 commit comments

Comments
 (0)