Skip to content

Package: ExtendedColorRegistry

ExtendedColorRegistry

nameinstructionbranchcomplexitylinemethod
ExtendedColorRegistry()
M: 16 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
ExtendedColorRegistry(Display)
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getColor(Color, Color, Object)
M: 190 C: 0
0%
M: 24 C: 0
0%
M: 13 C: 0
0%
M: 39 C: 0
0%
M: 1 C: 0
0%
handleDisplayDispose()
M: 19 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
hookDisplayDispose(Display)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /**
2: * Copyright (c) 2008-2010 IBM Corporation and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * IBM - Initial API and implementation
13: */
14: // REUSED CLASS
15: package org.eclipse.emf.edit.ui.provider;
16:
17: import java.util.ArrayList;
18: import java.util.Collection;
19: import java.util.HashMap;
20:
21: import org.eclipse.emf.common.util.URI;
22: import org.eclipse.emf.edit.provider.IItemColorProvider;
23: import org.eclipse.jface.resource.ColorDescriptor;
24: import org.eclipse.jface.resource.DeviceResourceException;
25: import org.eclipse.swt.graphics.Color;
26: import org.eclipse.swt.graphics.RGB;
27: import org.eclipse.swt.widgets.Display;
28:
29: /**
30: * A color registry for converting a color description into an actual color.
31: *
32: * @see IItemColorProvider
33: */
34: public class ExtendedColorRegistry {
35:         public static final ExtendedColorRegistry INSTANCE = new ExtendedColorRegistry() {
36:                 @Override
37:                 public Color getColor(Color foregroundColor, Color backgroundColor, Object object) {
38:                         return ExtendedImageRegistry.getInstance(ExtendedColorRegistry.class).getColor(foregroundColor,
39:                                 backgroundColor, object);
40:                 }
41:         };
42:
43:         protected Display display;
44:         protected HashMap<Collection<?>, Color> table = new HashMap<Collection<?>, Color>(10);
45:
46:         public ExtendedColorRegistry() {
47:                 display = Display.getCurrent();
48:                 hookDisplayDispose(display);
49:         }
50:
51:         public ExtendedColorRegistry(Display display) {
52:                 this.display = display;
53:                 hookDisplayDispose(display);
54:         }
55:
56:         public Color getColor(Color foregroundColor, Color backgroundColor, Object object) {
57:•                if (object instanceof Color) {
58:                         return (Color) object;
59:                 } else {
60:                         final Collection<Object> key = new ArrayList<Object>(2);
61:                         key.add(foregroundColor);
62:                         key.add(backgroundColor);
63:                         key.add(object);
64:
65:                         Color result = table.get(key);
66:•                        if (result == null) {
67:•                                if (object instanceof ColorDescriptor) {
68:                                         final ColorDescriptor colorDescriptor = (ColorDescriptor) object;
69:                                         try {
70:                                                 result = colorDescriptor.createColor(display);
71:                                         } catch (final DeviceResourceException exception) {
72:                                                 Activator.log(exception);
73:                                         }
74:•                                } else if (object instanceof URI) {
75:                                         final URI colorURI = (URI) object;
76:•                                        if (!"color".equals(colorURI.scheme())) {
77:                                                 throw new IllegalArgumentException("Only 'color' scheme is recognized" + colorURI);
78:                                         }
79:
80:                                         RGB colorData;
81:•                                        if ("rgb".equals(colorURI.authority())) {
82:                                                 final int red = Integer.parseInt(colorURI.segment(0));
83:                                                 final int green = Integer.parseInt(colorURI.segment(1));
84:                                                 final int blue = Integer.parseInt(colorURI.segment(2));
85:                                                 colorData = new RGB(red, green, blue);
86:•                                        } else if ("hsb".equals(colorURI.authority())) {
87:                                                 final float[] hsb = new float[3];
88:•                                                for (int i = 0; i < 3; ++i) {
89:                                                         final String segment = colorURI.segment(i);
90:•                                                        hsb[i] = "".equals(segment) || "foreground".equals(segment)
91:                                                                 ? foregroundColor.getRGB().getHSB()[i]
92:•                                                                : "background".equals(segment) ? backgroundColor.getRGB().getHSB()[i]
93:                                                                         : Float.parseFloat(segment);
94:                                                 }
95:                                                 colorData = new RGB(hsb[0], hsb[1], hsb[2]);
96:                                         } else {
97:                                                 throw new IllegalArgumentException("Only 'rgb' and 'hsb' authority are recognized" + colorURI);
98:                                         }
99:
100:                                         try {
101:                                                 result = ColorDescriptor.createFrom(colorData).createColor(display);
102:                                         } catch (final DeviceResourceException exception) {
103:                                                 Activator.log(exception);
104:                                         }
105:                                 }
106:
107:•                                if (result != null) {
108:                                         table.put(key, result);
109:                                 }
110:                         }
111:                         return result;
112:                 }
113:         }
114:
115:         protected void handleDisplayDispose() {
116:•                for (final Color color : table.values()) {
117:                         color.dispose();
118:                 }
119:                 table = null;
120:         }
121:
122:         protected void hookDisplayDispose(Display display) {
123:                 display.disposeExec(new Runnable() {
124:                         @Override
125:                         public void run() {
126:                                 handleDisplayDispose();
127:                         }
128:                 });
129:         }
130: }