]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.charts/src/org/simantics/charts/ui/ChartItemImageRule.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / ui / ChartItemImageRule.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
3  * 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.charts.ui;\r
13 \r
14 import java.awt.Color;\r
15 import java.util.Map;\r
16 \r
17 import org.eclipse.jface.resource.ImageDescriptor;\r
18 import org.simantics.browsing.ui.model.images.ImageRule;\r
19 import org.simantics.charts.ontology.ChartResource;\r
20 import org.simantics.databoard.Bindings;\r
21 import org.simantics.db.ReadGraph;\r
22 import org.simantics.db.Resource;\r
23 import org.simantics.db.exception.DatabaseException;\r
24 import org.simantics.diagram.stubs.G2DResource;\r
25 import org.simantics.trend.impl.JarisPaints;\r
26 import org.simantics.utils.datastructures.ArrayMap;\r
27 \r
28 public enum ChartItemImageRule implements ImageRule {\r
29 \r
30     INSTANCE;\r
31     public static ChartItemImageRule get() { return INSTANCE; }\r
32 \r
33     public static final String COLUMN_KEY         = "single";\r
34     public static final String[] COLUMN_KEYS      = { COLUMN_KEY };\r
35 \r
36     ChartItemIcon chartIcons = new ChartItemIcon();\r
37 \r
38     @Override\r
39     public boolean isCompatible(Class<?> contentType) {\r
40         return contentType.equals(Resource.class);\r
41     }\r
42 \r
43     @Override\r
44     public Map<String, ImageDescriptor> getImage(ReadGraph graph, Object content) throws DatabaseException {\r
45         Resource item = (Resource) content;\r
46         ChartResource CHART = ChartResource.getInstance(graph);\r
47         G2DResource G2D = G2DResource.getInstance(graph);\r
48 \r
49         Color color = null;\r
50         float[] customColor = graph.getPossibleRelatedValue(item, G2D.HasColor, Bindings.FLOAT_ARRAY);\r
51         if (customColor != null && customColor.length >= 3)\r
52             color = new Color(customColor[0], customColor[1], customColor[2]);\r
53         if (color == null) {\r
54             Integer index = graph.getPossibleRelatedValue(item, CHART.Chart_Item_Index);\r
55             if ( index == null ) index = 1;\r
56             color = JarisPaints.getColor( index );\r
57         }\r
58 \r
59         Resource rend = graph.getPossibleObject(item, CHART.Chart_Item_Renderer);\r
60         boolean binary = CHART.Renderer_Binary.equals(rend); \r
61 \r
62         ImageDescriptor icon = chartIcons.createIcon(!binary, color, false);\r
63 \r
64         return ArrayMap.make(COLUMN_KEYS, new ImageDescriptor[] { icon });\r
65     }\r
66 \r
67 }\r