]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/PropertyLabelDecoratorFactory.java
Merge remote-tracking branch 'origin/svn' commit 'ccc1271c9d6657fb9dcf4cf3cb115fa0c8c...
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / PropertyLabelDecoratorFactory.java
1 /*******************************************************************************\r
2  *  Copyright (c) 2013 Association for Decentralized Information Management in\r
3  *  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  *      Semantum Oy - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.browsing.ui.swt;\r
13 \r
14 import org.eclipse.jface.resource.ColorDescriptor;\r
15 import org.eclipse.swt.graphics.RGB;\r
16 import org.simantics.browsing.ui.BuiltinKeys;\r
17 import org.simantics.browsing.ui.BuiltinKeys.LabelDecoratorKey;\r
18 import org.simantics.browsing.ui.NodeContext;\r
19 import org.simantics.browsing.ui.PrimitiveQueryUpdater;\r
20 import org.simantics.browsing.ui.common.ColumnKeys;\r
21 import org.simantics.browsing.ui.common.property.IArrayProperty;\r
22 import org.simantics.browsing.ui.common.property.IProperty;\r
23 import org.simantics.browsing.ui.content.LabelDecorator;\r
24 import org.simantics.browsing.ui.content.LabelDecoratorFactory;\r
25 import org.simantics.browsing.ui.graph.impl.contribution.LabelDecoratorContributionImpl;\r
26 import org.simantics.databoard.Bindings;\r
27 import org.simantics.db.ReadGraph;\r
28 import org.simantics.db.Resource;\r
29 import org.simantics.db.exception.DatabaseException;\r
30 import org.simantics.layer0.Layer0;\r
31 \r
32 /**\r
33  * @author Tuukka Lehtonen\r
34  */\r
35 public class PropertyLabelDecoratorFactory implements LabelDecoratorFactory {\r
36 \r
37         private final static ColorDescriptor READONLY_GRAY = ColorDescriptor.createFrom(new RGB(240, 240, 240));\r
38 \r
39         @Override\r
40         public LabelDecorator create(PrimitiveQueryUpdater manager, NodeContext context, LabelDecoratorKey key) {\r
41                 Object o = context.getConstant(BuiltinKeys.INPUT);\r
42                 if (o instanceof IProperty) {\r
43                         IProperty prop = (IProperty) o;\r
44                         if (o instanceof IArrayProperty) {\r
45                                 IArrayProperty array = (IArrayProperty) o;\r
46                                 if (array.isSlice())\r
47                                         // Prevent the decorator from overworking on each and every sliced table row.\r
48                                         return null;\r
49                         }\r
50 \r
51                         final Resource[] data = prop.getData(Resource[].class);\r
52                         if (data == null)\r
53                                 return null;\r
54                         if (data.length != 3)\r
55                                 return null;\r
56 \r
57                         return new PropertyReadOnlyLabelDecorator(manager, context, key);\r
58                 }\r
59                 return null;\r
60         }\r
61 \r
62         static class PropertyReadOnlyLabelDecorator extends LabelDecoratorContributionImpl {\r
63 \r
64                 public PropertyReadOnlyLabelDecorator(PrimitiveQueryUpdater manager, NodeContext context, LabelDecoratorKey key) {\r
65                         super(manager, context, key);\r
66                 }\r
67 \r
68                 @Override\r
69                 public LabelDecorator getDecorator(ReadGraph graph, NodeContext context) throws DatabaseException {\r
70                         IProperty prop = (IProperty) context.getConstant(BuiltinKeys.INPUT);\r
71                         final Resource[] data = prop.getData(Resource[].class);\r
72                         Layer0 L0 = Layer0.getInstance(graph);\r
73                         Object o = graph.getPossibleRelatedValue2(data[1], L0.readOnly, Bindings.BOOLEAN);\r
74                         if (!Boolean.TRUE.equals(o))\r
75                                 return null;\r
76 \r
77                         return new LabelDecorator.Stub() {\r
78                                 @Override\r
79                                 public <C> C decorateForeground(C color, String column, int itemIndex) {\r
80                                         return null;\r
81                                 }\r
82                                 @SuppressWarnings("unchecked")\r
83                                 @Override\r
84                                 public <C> C decorateBackground(C color, String column, int itemIndex) {\r
85                                         if (ColumnKeys.VALUE.equals(column))\r
86                                                 return (C) READONLY_GRAY;\r
87                                         return null;\r
88                                 }\r
89                         };\r
90                 }\r
91 \r
92         }\r
93 \r
94 }\r