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