--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2012 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.ui.modelBrowser2.label;\r
+\r
+import org.eclipse.jface.resource.ColorDescriptor;\r
+import org.eclipse.jface.resource.FontDescriptor;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.graphics.RGB;\r
+import org.simantics.browsing.ui.content.LabelDecorator;\r
+import org.simantics.browsing.ui.model.labeldecorators.LabelDecorationRule;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.Databoard;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.type.BooleanType;\r
+import org.simantics.databoard.type.Datatype;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.request.PossibleModel;\r
+import org.simantics.db.layer0.request.PossibleVariableValue;\r
+import org.simantics.db.layer0.variable.RVI;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.modeling.subscription.ModelContexts;\r
+import org.simantics.modeling.subscription.ModelContextsRequest;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class SubscriptionItemLabelDecorationRule implements LabelDecorationRule {\r
+\r
+ public static final SubscriptionItemLabelDecorationRule INSTANCE = new SubscriptionItemLabelDecorationRule();\r
+\r
+ private static final ColorDescriptor RED = ColorDescriptor.createFrom(new RGB(255, 0, 0));\r
+ private static final ColorDescriptor INVALID = ColorDescriptor.createFrom(new RGB(255, 128, 128));\r
+ private static final ColorDescriptor GREEN = ColorDescriptor.createFrom(new RGB(0, 160, 0));\r
+\r
+ static LabelDecorator RED_DECO = new LabelDecorator.Stub() {\r
+ @SuppressWarnings("unchecked")\r
+ public <C> C decorateForeground(C color, String column, int itemIndex) {\r
+ return (C) RED;\r
+ }\r
+ };\r
+\r
+ static LabelDecorator GREEN_DECO = new LabelDecorator.Stub() {\r
+ @SuppressWarnings("unchecked")\r
+ public <C> C decorateForeground(C color, String column, int itemIndex) {\r
+ return (C) GREEN;\r
+ }\r
+ };\r
+\r
+ static LabelDecorator UNRESOLVABLE = new LabelDecorator.Stub() {\r
+ @Override\r
+ public String decorateLabel(String label, String column, int itemIndex) {\r
+ return label + " (INVALID)";\r
+ }\r
+ @SuppressWarnings("unchecked")\r
+ public <C> C decorateForeground(C color, String column, int itemIndex) {\r
+ return (C) INVALID;\r
+ }\r
+ @SuppressWarnings("unchecked")\r
+ public <F> F decorateFont(F font, String column, int itemIndex) {\r
+ return (F) ((FontDescriptor) font).withStyle(SWT.ITALIC);\r
+ }\r
+ };\r
+\r
+ @Override\r
+ public boolean isCompatible(Class<?> contentType) {\r
+ return contentType.equals(Resource.class);\r
+ }\r
+\r
+ @Override\r
+ public LabelDecorator getLabelDecorator(ReadGraph graph, Object content) throws DatabaseException {\r
+ Resource item = (Resource) content;\r
+ ModelingResources MOD = ModelingResources.getInstance(graph);\r
+\r
+ Binding rviBinding = graph.getService(Databoard.class).getBindingUnchecked( RVI.class );\r
+ RVI rvi = graph.getPossibleRelatedValue(item, MOD.Subscription_Item_VariableId, rviBinding);\r
+ if (rvi == null)\r
+ return null;\r
+\r
+ Resource model = graph.sync( new PossibleModel(item) );\r
+ if (model == null)\r
+ return null;\r
+\r
+ ModelContexts contexts = graph.syncRequest(new ModelContextsRequest(model));\r
+ if (contexts.getExperimentContext() == null)\r
+ return null;\r
+\r
+ Variable variable = rvi.resolvePossible(graph, contexts.getExperimentContext());\r
+ if (variable == null)\r
+ return UNRESOLVABLE;\r
+\r
+ Datatype datatype = graph.getPossibleRelatedValue(item, MOD.Subscription_Item_Datatype, Bindings.getBindingUnchecked(Datatype.class));\r
+ if (!(datatype instanceof BooleanType))\r
+ return null;\r
+\r
+ Object value = graph.sync( new PossibleVariableValue<Object>(variable) );\r
+ if (!(value instanceof Boolean))\r
+ return null;\r
+\r
+ Boolean v = (Boolean) value;\r
+ return v ? GREEN_DECO : RED_DECO;\r
+ }\r
+\r
+}\r