]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.event/src/org/simantics/event/view/contribution/EventImageRule.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.event / src / org / simantics / event / view / contribution / EventImageRule.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.event.view.contribution;\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.db.ReadGraph;\r
20 import org.simantics.db.Resource;\r
21 import org.simantics.db.exception.DatabaseException;\r
22 import org.simantics.event.Activator;\r
23 import org.simantics.event.ontology.EventResource;\r
24 import org.simantics.event.view.Constants;\r
25 import org.simantics.utils.datastructures.ArrayMap;\r
26 import org.simantics.utils.ui.BundleUtils;\r
27 import org.simantics.utils.ui.gfx.AlphaAdjustmentImageDescriptor;\r
28 import org.simantics.utils.ui.gfx.CompositionImageDescriptor;\r
29 import org.simantics.utils.ui.gfx.TextImageDescriptor;\r
30 \r
31 /**\r
32  * @author Tuukka Lehtonen\r
33  */\r
34 public enum EventImageRule implements ImageRule {\r
35 \r
36     INSTANCE;\r
37 \r
38     public static EventImageRule get() {\r
39         return INSTANCE;\r
40     }\r
41 \r
42     @Override\r
43     public boolean isCompatible(Class<?> contentType) {\r
44         return contentType.equals(Resource.class);\r
45     }\r
46 \r
47     @Override\r
48     public Map<String, ImageDescriptor> getImage(ReadGraph graph, Object content) throws DatabaseException {\r
49         Resource event = (Resource) content;\r
50 \r
51         EventResource EVENT = EventResource.getInstance(graph);\r
52 \r
53         ImageDescriptor eventIcon = null;\r
54         ImageDescriptor milestoneIcon = null;\r
55         ImageDescriptor returnIcon = null;\r
56 \r
57         // Icon derived from event type\r
58         Resource eventType = graph.getPossibleObject(event, EVENT.Event_type);\r
59         if (eventType != null) {\r
60             eventIcon = graph.syncRequest(new EventTypeImageDescriptorRequest(eventType));\r
61         }\r
62 \r
63         // Milestone Diamond Icon\r
64         if (graph.hasStatement(event, EVENT.Milestone)) {\r
65             String label = graph.getPossibleRelatedValue(event, EVENT.Event_milestoneLabel);\r
66             if (label == null) label = "?";\r
67 \r
68             boolean isBasetime = graph.hasStatement(event, EVENT.EventLog_HasBaselineEvent_Inverse);\r
69             ImageDescriptor diamond = isBasetime ? Activator.DIAMOND2_ICON : Activator.DIAMOND_ICON;\r
70             int color = isBasetime ? 0 : Color.ORANGE.getRGB();\r
71             ImageDescriptor number = new TextImageDescriptor(label, 16, 16, "Tahoma", 8, 0, color);\r
72             milestoneIcon = CompositionImageDescriptor.compose( diamond, number );\r
73         }\r
74 \r
75         if (!graph.hasStatement(event, EVENT.NoReturn)) {\r
76             if (graph.hasStatement(event, EVENT.ReturnedBy)) {\r
77                 returnIcon = BundleUtils.getImageDescriptorFromPlugin("com.famfamfam.silk", "icons/tick.png");\r
78             } else if (graph.hasStatement(event, EVENT.ReturnEvent)) {\r
79                 returnIcon = BundleUtils.getImageDescriptorFromPlugin("com.famfamfam.silk", "icons/arrow_undo.png");\r
80             } else {\r
81                 returnIcon = BundleUtils.getImageDescriptorFromPlugin("com.famfamfam.silk", "icons/cross.png");\r
82             }\r
83         }\r
84 \r
85         boolean hidden = graph.hasStatement(event, EVENT.Hidden);\r
86 \r
87         eventIcon = fade(eventIcon, hidden);\r
88         milestoneIcon = fade(milestoneIcon, hidden);\r
89         returnIcon = fade(returnIcon, hidden);\r
90 \r
91         return ArrayMap.make(Constants.COLUMN_KEYS, new ImageDescriptor[] {\r
92                 null,\r
93                 null,\r
94                 null,\r
95                 milestoneIcon,\r
96                 eventIcon,\r
97                 returnIcon,\r
98                 null,\r
99                 null,\r
100                 null,\r
101                 null,\r
102                 null\r
103         });\r
104     }\r
105 \r
106     private static ImageDescriptor fade(ImageDescriptor desc, boolean doIt) {\r
107         return desc != null && doIt ? AlphaAdjustmentImageDescriptor.adjustAlpha(desc, 128) : desc;\r
108     }\r
109 \r
110 }\r