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