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.properties.widgets.arrays;
\r
14 import java.util.ArrayList;
\r
16 import org.simantics.browsing.ui.common.modifiers.EnumeratedValue;
\r
17 import org.simantics.browsing.ui.common.modifiers.Enumeration;
\r
18 import org.simantics.browsing.ui.common.node.AbstractNode;
\r
19 import org.simantics.browsing.ui.common.node.IModifiableNode;
\r
20 import org.simantics.browsing.ui.content.Labeler.Modifier;
\r
21 import org.simantics.db.ReadGraph;
\r
22 import org.simantics.db.Resource;
\r
23 import org.simantics.db.WriteGraph;
\r
24 import org.simantics.db.common.request.ObjectsWithType;
\r
25 import org.simantics.db.common.utils.NameUtils;
\r
26 import org.simantics.db.exception.DatabaseException;
\r
27 import org.simantics.db.request.Read;
\r
28 import org.simantics.layer0.Layer0;
\r
29 import org.simantics.layer0.utils.direct.GraphUtils;
\r
30 import org.simantics.sysdyn.SysdynResource;
\r
31 import org.simantics.sysdyn.ui.properties.widgets.ColumnKeys;
\r
32 import org.simantics.ui.SimanticsUI;
\r
34 public class RedeclarationNode extends AbstractNode<Resource> implements IModifiableNode {
\r
36 private Resource module;
\r
38 public RedeclarationNode(ReadGraph graph, final Resource module, Resource enumeration) {
\r
40 this.module = module;
\r
48 public Resource getReplacingEnumeration(ReadGraph graph) {
\r
49 SysdynResource sr = SysdynResource.getInstance(graph);
\r
50 Resource result = null;
\r
52 Resource redeclaration = getRedeclaration(graph);
\r
53 if(redeclaration != null) {
\r
54 result = graph.getSingleObject(redeclaration, sr.Redeclaration_replacingEnumeration);
\r
56 } catch(DatabaseException e) {
\r
57 e.printStackTrace();
\r
64 public Resource getModule() {
\r
68 public void setModule(Resource module) {
\r
69 this.module = module;
\r
72 public Resource getRedeclaration(ReadGraph graph) {
\r
74 SysdynResource sr = SysdynResource.getInstance(graph);
\r
75 for(Resource redeclaration : graph.syncRequest(new ObjectsWithType(module, sr.Module_redeclaration, sr.Redeclaration))) {
\r
76 Resource replacedEnumeration = graph.getPossibleObject(redeclaration, sr.Redeclaration_replacedEnumeration);
\r
77 if(replacedEnumeration != null && replacedEnumeration.equals(getReplacedEnumeration())) {
\r
78 return redeclaration;
\r
81 } catch (DatabaseException e) {
\r
82 e.printStackTrace();
\r
87 public void setRedeclaration(WriteGraph graph, Resource redeclaration) {
\r
89 SysdynResource sr = SysdynResource.getInstance(graph);
\r
90 Resource oldRedeclaration = getRedeclaration(graph);
\r
91 if(oldRedeclaration != null || redeclaration == null) {
\r
92 graph.deny(module, sr.Module_redeclaration, oldRedeclaration);
\r
95 if(redeclaration != null)
\r
96 graph.claim(module, sr.Module_redeclaration, redeclaration);
\r
98 } catch (DatabaseException e) {
\r
99 e.printStackTrace();
\r
103 public Resource getReplacedEnumeration() {
\r
108 public Modifier getModifier(String columnId) {
\r
110 if(!ColumnKeys.REPLACED_WITH.equals(columnId))
\r
113 ComboBoxModifier<Resource> cbm = null;
\r
116 cbm = SimanticsUI.getSession().syncRequest(new Read<ComboBoxModifier<Resource>>() {
\r
119 public ComboBoxModifier<Resource> perform(ReadGraph graph) throws DatabaseException {
\r
120 SysdynResource sr = SysdynResource.getInstance(graph);
\r
121 Layer0 l0 = Layer0.getInstance(graph);
\r
123 ArrayList<EnumeratedValue<Resource>> values = new ArrayList<EnumeratedValue<Resource>>();
\r
126 Resource configuration = graph.getSingleObject(module, l0.PartOf);
\r
128 for(Resource enumeration : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.Enumeration))) {
\r
129 String name = NameUtils.getSafeName(graph, enumeration);
\r
130 values.add(new EnumeratedValue<Resource>(name, enumeration));
\r
133 if(values.size() == 0)
\r
136 values.add(0, new EnumeratedValue<Resource>("", null));
\r
137 Enumeration<Resource> enumeration = new Enumeration<Resource>(values);
\r
139 ComboBoxModifier<Resource> cbm = new ComboBoxModifier<Resource>(graph.getSession(), enumeration, getReplacingEnumeration(graph)) {
\r
142 protected void modifyWithObject(WriteGraph graph, Resource oldEnumObject,
\r
143 Resource enumObject) throws DatabaseException {
\r
145 if(enumObject == null) {
\r
146 setRedeclaration(graph, null);
\r
147 } else if(!enumObject.equals(oldEnumObject)) {
\r
148 SysdynResource sr = SysdynResource.getInstance(graph);
\r
149 Resource redeclaration = GraphUtils.create2(graph,
\r
151 sr.Redeclaration_replacedEnumeration, getReplacedEnumeration(),
\r
152 sr.Redeclaration_replacingEnumeration, enumObject);
\r
153 setRedeclaration(graph, redeclaration);
\r
163 } catch (DatabaseException e) {
\r
164 // TODO Auto-generated catch block
\r
165 e.printStackTrace();
\r