]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/colors/RGBAdapter.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / colors / RGBAdapter.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.ui.colors;\r
13 \r
14 import org.eclipse.swt.graphics.RGB;\r
15 import org.simantics.databoard.Bindings;\r
16 import org.simantics.databoard.binding.Binding;\r
17 import org.simantics.db.AsyncReadGraph;\r
18 import org.simantics.db.Resource;\r
19 import org.simantics.db.common.adaption.ResourceAdapter;\r
20 import org.simantics.db.procedure.AsyncProcedure;\r
21 \r
22 public class RGBAdapter implements ResourceAdapter<RGB> {\r
23 \r
24     public static class RGBDouble {\r
25         public double red;\r
26         public double green;\r
27         public double blue;\r
28     }\r
29 \r
30     public static final Binding BINDING = Bindings.getBindingUnchecked(RGBDouble.class);\r
31 \r
32     @Override\r
33     public void adapt(AsyncReadGraph g, Resource source, Resource r, final AsyncProcedure<RGB> procedure) {\r
34         g.forValue(r, BINDING, new AsyncProcedure<RGBDouble>() {\r
35             @Override\r
36             public void execute(AsyncReadGraph graph, RGBDouble result) {\r
37                 procedure.execute(graph, \r
38                         new RGB(\r
39                                 (int)(result.red*255.0),\r
40                                 (int)(result.green*255.0),\r
41                                 (int)(result.blue*255.0)\r
42                         )\r
43                 );\r
44             }\r
45 \r
46             @Override\r
47             public void exception(AsyncReadGraph graph, Throwable throwable) {\r
48                 procedure.exception(graph, throwable);\r
49             }\r
50         });\r
51     }\r
52 \r
53 }\r