]> gerrit.simantics Code Review - simantics/platform.git/blob - modelBrowser2/label/SubscriptionItemLabelDecorationRule.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / modelBrowser2 / label / SubscriptionItemLabelDecorationRule.java
1 /*******************************************************************************
2  * Copyright (c) 2012 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.modeling.ui.modelBrowser2.label;
13
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;
35
36 /**
37  * @author Tuukka Lehtonen
38  */
39 public class SubscriptionItemLabelDecorationRule implements LabelDecorationRule {
40
41     public static final SubscriptionItemLabelDecorationRule INSTANCE = new SubscriptionItemLabelDecorationRule();
42
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));
46
47     static LabelDecorator RED_DECO = new LabelDecorator.Stub() {
48         @SuppressWarnings("unchecked")
49         public <C> C decorateForeground(C color, String column, int itemIndex) {
50             return (C) RED;
51         }
52     };
53
54     static LabelDecorator GREEN_DECO = new LabelDecorator.Stub() {
55         @SuppressWarnings("unchecked")
56         public <C> C decorateForeground(C color, String column, int itemIndex) {
57             return (C) GREEN;
58         }
59     };
60
61     static LabelDecorator UNRESOLVABLE = new LabelDecorator.Stub() {
62         @Override
63         public String decorateLabel(String label, String column, int itemIndex) {
64             return label + " (INVALID)";
65         }
66         @SuppressWarnings("unchecked")
67         public <C> C decorateForeground(C color, String column, int itemIndex) {
68             return (C) INVALID;
69         }
70         @SuppressWarnings("unchecked")
71         public <F> F decorateFont(F font, String column, int itemIndex) {
72             return (F) ((FontDescriptor) font).withStyle(SWT.ITALIC);
73         }
74     };
75
76     @Override
77     public boolean isCompatible(Class<?> contentType) {
78         return contentType.equals(Resource.class);
79     }
80
81     @Override
82     public LabelDecorator getLabelDecorator(ReadGraph graph, Object content) throws DatabaseException {
83         Resource item = (Resource) content;
84         ModelingResources MOD = ModelingResources.getInstance(graph);
85
86         Binding rviBinding = graph.getService(Databoard.class).getBindingUnchecked( RVI.class );
87         RVI rvi = graph.getPossibleRelatedValue(item, MOD.Subscription_Item_VariableId, rviBinding);
88         if (rvi == null)
89             return null;
90
91         Resource model = graph.sync( new PossibleModel(item) );
92         if (model == null)
93             return null;
94
95         ModelContexts contexts = graph.syncRequest(new ModelContextsRequest(model));
96         if (contexts.getExperimentContext() == null)
97             return null;
98
99         Variable variable = rvi.resolvePossible(graph, contexts.getExperimentContext());
100         if (variable == null)
101             return UNRESOLVABLE;
102
103         Datatype datatype = graph.getPossibleRelatedValue(item, MOD.Subscription_Item_Datatype, Bindings.getBindingUnchecked(Datatype.class));
104         if (!(datatype instanceof BooleanType))
105             return null;
106
107         Object value = graph.sync( new PossibleVariableValue<Object>(variable) );
108         if (!(value instanceof Boolean))
109             return null;
110
111         Boolean v = (Boolean) value;
112         return v ? GREEN_DECO : RED_DECO;
113     }
114
115 }