]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.charts/src/org/simantics/charts/ui/ChartItemImageRule.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / ui / ChartItemImageRule.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.charts.ui;
13
14 import java.awt.Color;
15 import java.util.Map;
16
17 import org.eclipse.jface.resource.ImageDescriptor;
18 import org.simantics.browsing.ui.model.images.ImageRule;
19 import org.simantics.charts.ontology.ChartResource;
20 import org.simantics.databoard.Bindings;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.diagram.stubs.G2DResource;
25 import org.simantics.trend.impl.JarisPaints;
26 import org.simantics.utils.datastructures.ArrayMap;
27
28 public enum ChartItemImageRule implements ImageRule {
29
30     INSTANCE;
31     public static ChartItemImageRule get() { return INSTANCE; }
32
33     public static final String COLUMN_KEY         = "single";
34     public static final String[] COLUMN_KEYS      = { COLUMN_KEY };
35
36     ChartItemIcon chartIcons = new ChartItemIcon();
37
38     @Override
39     public boolean isCompatible(Class<?> contentType) {
40         return contentType.equals(Resource.class);
41     }
42
43     @Override
44     public Map<String, ImageDescriptor> getImage(ReadGraph graph, Object content) throws DatabaseException {
45         Resource item = (Resource) content;
46         ChartResource CHART = ChartResource.getInstance(graph);
47         G2DResource G2D = G2DResource.getInstance(graph);
48
49         Color color = null;
50         float[] customColor = graph.getPossibleRelatedValue(item, G2D.HasColor, Bindings.FLOAT_ARRAY);
51         if (customColor != null && customColor.length >= 3)
52             color = new Color(customColor[0], customColor[1], customColor[2]);
53         if (color == null) {
54             Integer index = graph.getPossibleRelatedValue(item, CHART.Chart_Item_Index);
55             if ( index == null ) index = 1;
56             color = JarisPaints.getColor( index );
57         }
58
59         Resource rend = graph.getPossibleObject(item, CHART.Chart_Item_Renderer);
60         boolean binary = CHART.Renderer_Binary.equals(rend); 
61
62         ImageDescriptor icon = chartIcons.createIcon(!binary, color, false);
63
64         return ArrayMap.make(COLUMN_KEYS, new ImageDescriptor[] { icon });
65     }
66
67 }