26
26
27
27
/** Caches {@link Color}s, and automatically manages their disposal. */
28
28
public class ColorPool {
29
- private final HashMap <RGB , Color > colorTable = Maps .newHashMap ();
30
- private final Display display ;
29
+ private final HashMap <RGB , Color > colorTable = new HashMap <>();
31
30
32
- private ColorPool (Widget parent ) {
33
- display = parent .getDisplay ();
34
- parent .addListener (SWT .Dispose , e -> colorTable .values ().forEach (Color ::dispose ));
35
- }
31
+ private ColorPool () {}
36
32
37
33
/** Returns a Color for the given RGB value. */
38
34
public Color getColor (RGB rgb ) {
39
- Color color = colorTable .get (rgb );
40
- if (color == null ) {
41
- color = new Color (display , rgb );
42
- colorTable .put (rgb , color );
43
- }
44
- return color ;
45
- }
46
-
47
- /** Returns a Color for the SWT.COLOR_xxx. */
48
- public Color getSystemColor (int systemColor ) {
49
- return display .getSystemColor (systemColor );
35
+ return colorTable .computeIfAbsent (rgb , raw -> new Color (raw ));
50
36
}
51
37
52
38
/** Returns a ColorPool for the given Widget, creating one if necessary. */
@@ -59,5 +45,5 @@ public static ColorPool forWidget(ControlWrapper wrapper) {
59
45
return onePerWidget .forWidget (wrapper .getRootControl ());
60
46
}
61
47
62
- private static final OnePerWidget <Widget , ColorPool > onePerWidget = OnePerWidget .from (ColorPool :: new );
48
+ private static final OnePerWidget <Widget , ColorPool > onePerWidget = OnePerWidget .from (unused -> new ColorPool () );
63
49
}
0 commit comments