1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.modelBrowser2.label;
14 import org.eclipse.jface.resource.ColorDescriptor;
15 import org.eclipse.jface.resource.FontDescriptor;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.graphics.RGB;
18 import org.simantics.browsing.ui.content.LabelDecorator;
19 import org.simantics.browsing.ui.model.labeldecorators.LabelDecorationRule;
20 import org.simantics.databoard.Bindings;
21 import org.simantics.databoard.Databoard;
22 import org.simantics.databoard.binding.Binding;
23 import org.simantics.databoard.type.BooleanType;
24 import org.simantics.databoard.type.Datatype;
25 import org.simantics.db.ReadGraph;
26 import org.simantics.db.Resource;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.db.layer0.request.PossibleModel;
29 import org.simantics.db.layer0.request.PossibleVariableValue;
30 import org.simantics.db.layer0.variable.RVI;
31 import org.simantics.db.layer0.variable.Variable;
32 import org.simantics.modeling.ModelingResources;
33 import org.simantics.modeling.subscription.ModelContexts;
34 import org.simantics.modeling.subscription.ModelContextsRequest;
37 * @author Tuukka Lehtonen
39 public class SubscriptionItemLabelDecorationRule implements LabelDecorationRule {
41 public static final SubscriptionItemLabelDecorationRule INSTANCE = new SubscriptionItemLabelDecorationRule();
43 private static final ColorDescriptor RED = ColorDescriptor.createFrom(new RGB(255, 0, 0));
44 private static final ColorDescriptor INVALID = ColorDescriptor.createFrom(new RGB(255, 128, 128));
45 private static final ColorDescriptor GREEN = ColorDescriptor.createFrom(new RGB(0, 160, 0));
47 static LabelDecorator RED_DECO = new LabelDecorator.Stub() {
48 @SuppressWarnings("unchecked")
49 public <C> C decorateForeground(C color, String column, int itemIndex) {
54 static LabelDecorator GREEN_DECO = new LabelDecorator.Stub() {
55 @SuppressWarnings("unchecked")
56 public <C> C decorateForeground(C color, String column, int itemIndex) {
61 static LabelDecorator UNRESOLVABLE = new LabelDecorator.Stub() {
63 public String decorateLabel(String label, String column, int itemIndex) {
64 return label + " (INVALID)";
66 @SuppressWarnings("unchecked")
67 public <C> C decorateForeground(C color, String column, int itemIndex) {
70 @SuppressWarnings("unchecked")
71 public <F> F decorateFont(F font, String column, int itemIndex) {
72 return (F) ((FontDescriptor) font).withStyle(SWT.ITALIC);
77 public boolean isCompatible(Class<?> contentType) {
78 return contentType.equals(Resource.class);
82 public LabelDecorator getLabelDecorator(ReadGraph graph, Object content) throws DatabaseException {
83 Resource item = (Resource) content;
84 ModelingResources MOD = ModelingResources.getInstance(graph);
86 Binding rviBinding = graph.getService(Databoard.class).getBindingUnchecked( RVI.class );
87 RVI rvi = graph.getPossibleRelatedValue(item, MOD.Subscription_Item_VariableId, rviBinding);
91 Resource model = graph.sync( new PossibleModel(item) );
95 ModelContexts contexts = graph.syncRequest(new ModelContextsRequest(model));
96 if (contexts.getExperimentContext() == null)
99 Variable variable = rvi.resolvePossible(graph, contexts.getExperimentContext());
100 if (variable == null)
103 Datatype datatype = graph.getPossibleRelatedValue(item, MOD.Subscription_Item_Datatype, Bindings.getBindingUnchecked(Datatype.class));
104 if (!(datatype instanceof BooleanType))
107 Object value = graph.sync( new PossibleVariableValue<Object>(variable) );
108 if (!(value instanceof Boolean))
111 Boolean v = (Boolean) value;
112 return v ? GREEN_DECO : RED_DECO;