1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.trend.chart.properties;
\r
14 import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl;
\r
15 import org.simantics.databoard.Bindings;
\r
16 import org.simantics.db.ReadGraph;
\r
17 import org.simantics.db.Resource;
\r
18 import org.simantics.db.common.request.ObjectsWithType;
\r
19 import org.simantics.db.exception.DatabaseException;
\r
20 import org.simantics.layer0.Layer0;
\r
21 import org.simantics.utils.datastructures.Quad;
\r
24 * PropertyFactory for finding a boolean property. Supports also finding the
\r
25 * property from a first occurrence of resource ConsistsOf type HasProperty
\r
27 * @author Teemu Lempinen
\r
30 public class BooleanPropertyFactory extends ReadFactoryImpl<Resource, Boolean> {
\r
32 final private String propertyURI;
\r
33 final private String typeURI;
\r
34 final private Boolean inverse;
\r
35 final private Boolean defaultValue;
\r
38 * PropertyFactory for finding a boolean property with propertyURI
\r
40 * @param propertyURI URI for the boolean property
\r
42 public BooleanPropertyFactory(String propertyURI) {
\r
43 this(null, propertyURI, false);
\r
47 * PropertyFactory for finding a boolean property with propertyURI.
\r
49 * Supports inverting the result (e.g. if required information is IsHidden, but database contains IsVisible)
\r
51 * @param propertyURI URI for the boolean property
\r
52 * @param inverse Invert the result?
\r
54 public BooleanPropertyFactory(String propertyURI, boolean inverse) {
\r
55 this(null, propertyURI, inverse);
\r
59 * PropertyFactory for finding a boolean property with propertyURI.
\r
61 * Finds the property for first ObjectWithType(resource, L0.ConsistsOf, type)
\r
63 * Supports inverting the result (e.g. if required information is IsHidden, but database contains IsVisible)
\r
65 * @param typeURI URI for a resource (resource ConsistsOf type) (null allowed)
\r
66 * @param propertyURI URI for the boolean property
\r
67 * @param inverse Invert the result?
\r
69 public BooleanPropertyFactory(String typeURI, String propertyURI, boolean inverse) {
\r
70 this(typeURI, propertyURI, inverse, false);
\r
74 * PropertyFactory for finding a boolean property with propertyURI.
\r
76 * Finds the property for first ObjectWithType(resource, L0.ConsistsOf, type)
\r
78 * Supports inverting the result (e.g. if required information is IsHidden, but database contains IsVisible)
\r
80 * @param typeURI URI for a resource (resource ConsistsOf type) (null allowed -> not used)
\r
81 * @param propertyURI URI for the boolean property
\r
82 * @param inverse Invert the result?
\r
83 * @param defaultValue default value
\r
85 public BooleanPropertyFactory(String typeURI, String propertyURI, boolean inverse, boolean defaultValue) {
\r
86 this.propertyURI = propertyURI;
\r
87 this.inverse = inverse;
\r
88 this.typeURI = typeURI;
\r
89 this.defaultValue = defaultValue;
\r
93 public Object getIdentity(Object inputContents) {
\r
94 return new Quad<Resource, String, Object, Boolean>((Resource) inputContents, propertyURI, getClass(), defaultValue);
\r
98 public Boolean perform(ReadGraph graph, Resource r) throws DatabaseException {
\r
99 if(typeURI == null) {
\r
100 // if no typeUri, use the default resource r
\r
101 return getValue(graph, r);
\r
103 // typeURI was defined, find the property for the first occurence of ConsistsOf type
\r
104 Resource type = graph.getResource(typeURI);
\r
105 for(Resource o : graph.syncRequest(new ObjectsWithType(r, Layer0.getInstance(graph).ConsistsOf, type))) {
\r
106 // Returns the value for the first occurrence
\r
107 return getValue(graph, o);
\r
110 // if nothing was found with typeURI
\r
115 * Return the value for a Boolean literal possibly inverted (or default if resource != Boolean literal)
\r
117 * @param graph ReadGraph
\r
118 * @param resource Literal Boolean resource
\r
119 * @return value of the parameter (or default or inverted)
\r
120 * @throws DatabaseException
\r
122 private Boolean getValue(ReadGraph graph, Resource resource) throws DatabaseException {
\r
123 Boolean value = graph.getPossibleRelatedValue(resource, graph.getResource(propertyURI), Bindings.BOOLEAN);
\r
124 if(value != null) {
\r
125 return !inverse.equals(value);
\r
127 return defaultValue;
\r