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.jfreechart.chart.properties;
\r
14 import org.simantics.browsing.ui.swt.widgets.impl.SelectionListenerImpl;
\r
15 import org.simantics.databoard.Bindings;
\r
16 import org.simantics.db.Resource;
\r
17 import org.simantics.db.WriteGraph;
\r
18 import org.simantics.db.common.request.ObjectsWithType;
\r
19 import org.simantics.db.exception.DatabaseException;
\r
20 import org.simantics.db.management.ISessionContext;
\r
21 import org.simantics.layer0.Layer0;
\r
24 * Class for setting a boolean value when a selection occurs. (check box buttons)
\r
26 * @author Teemu Lempinen
\r
29 public class BooleanSelectionListener extends SelectionListenerImpl<Resource> {
\r
31 final private String propertyURI;
\r
32 final private String typeUri;
\r
33 final private boolean defaultValue;
\r
36 * Boolean selection listener for property with propertyURI
\r
38 * @param context ISessionContext
\r
39 * @param propertyURI uri of the boolean property
\r
41 public BooleanSelectionListener(ISessionContext context, String propertyURI) {
\r
42 this(context, null, propertyURI, false);
\r
46 * Boolean selection listener for property with propertyURI
\r
47 * Sets the property for all ObjectWithType(resource, L0.ConsistsOf, type)
\r
49 * @param context ISessionContext
\r
50 * @param typeUri URI for a resource (resource ConsistsOf type) (null allowed -> not used)
\r
51 * @param propertyURI uri of the boolean property
\r
52 * @param defaultValue expected value if the property does not exist
\r
54 public BooleanSelectionListener(ISessionContext context, String typeUri, String propertyURI, boolean defaultValue) {
\r
56 this.propertyURI = propertyURI;
\r
57 this.typeUri = typeUri;
\r
58 this.defaultValue = defaultValue;
\r
61 public BooleanSelectionListener(ISessionContext context, String typeUri, String propertyURI) {
\r
62 this(context, typeUri, propertyURI, false);
\r
67 public void apply(WriteGraph graph, Resource chart) throws DatabaseException {
\r
68 if(typeUri == null) {
\r
69 setValue(graph, chart);
\r
71 Resource type = graph.getResource(typeUri);
\r
72 for(Resource object : graph.syncRequest(new ObjectsWithType(chart, Layer0.getInstance(graph).ConsistsOf, type))) {
\r
73 setValue(graph, object);
\r
80 * Set boolean value for Boolean literal resource (inverts the current value).
\r
81 * @param graph ReadGraph
\r
82 * @param resource Boolean literal resource
\r
83 * @throws DatabaseException
\r
85 private void setValue(WriteGraph graph, Resource resource) throws DatabaseException {
\r
86 Resource property = graph.getResource(propertyURI);
\r
87 Boolean value = graph.getPossibleRelatedValue(resource, property, Bindings.BOOLEAN);
\r
89 graph.claimLiteral(resource, property, !defaultValue);
\r
91 graph.claimLiteral(resource, property, Boolean.FALSE.equals(value));
\r