1 package org.simantics.sysdyn.ui.wizards.modules;
3 import java.util.Collection;
5 import org.simantics.Simantics;
6 import org.simantics.browsing.ui.common.node.AbstractNode;
7 import org.simantics.browsing.ui.common.node.DeleteException;
8 import org.simantics.browsing.ui.common.node.IDeletableNode;
9 import org.simantics.browsing.ui.common.node.IModifiableNode;
10 import org.simantics.browsing.ui.content.Labeler.Modifier;
11 import org.simantics.browsing.ui.graph.impl.LabelModifier;
12 import org.simantics.db.ReadGraph;
13 import org.simantics.db.Resource;
14 import org.simantics.db.WriteGraph;
15 import org.simantics.db.common.request.ObjectsWithType;
16 import org.simantics.db.common.request.ReadRequest;
17 import org.simantics.db.common.request.WriteRequest;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
20 import org.simantics.db.exception.ServiceException;
21 import org.simantics.db.layer0.adapter.PasteHandler;
22 import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler;
23 import org.simantics.db.procedure.Listener;
24 import org.simantics.db.request.Read;
25 import org.simantics.layer0.Layer0;
26 import org.simantics.modeling.ModelingResources;
27 import org.simantics.simulation.ontology.SimulationResource;
28 import org.simantics.structural.stubs.StructuralResource2;
30 public class ModuleComponentTypeNode extends AbstractNode<Resource> implements IDeletableNode, IModifiableNode {
33 Listener<String> configurationNameSynchronizer;
34 private boolean disposed = false;
35 private Resource configuration;
37 public ModuleComponentTypeNode(Resource resource) {
40 Simantics.getSession().asyncRequest(new ReadRequest() {
43 public void run(ReadGraph graph) throws DatabaseException {
44 StructuralResource2 sr2 = StructuralResource2.getInstance(graph);
45 configuration = graph.getPossibleObject(data, sr2.IsDefinedBy);
49 // Not the best solution for name sync
50 configurationNameSynchronizer = new Listener<String>() {
53 public void execute(final String result) {
54 Simantics.getSession().asyncRequest(new WriteRequest() {
57 public void perform(WriteGraph graph) throws DatabaseException {
58 if(configuration != null)
59 graph.claimLiteral(configuration, Layer0.getInstance(graph).HasLabel, result);
65 public void exception(Throwable t) {
70 public boolean isDisposed() {
75 Simantics.getSession().asyncRequest(new Read<String>() {
78 public String perform(ReadGraph graph) throws DatabaseException {
79 Layer0 l0 = Layer0.getInstance(graph);
80 return graph.getRelatedValue(data, l0.HasName);
83 }, configurationNameSynchronizer);
88 public Modifier getModifier(String columnId) {
89 Modifier modifier = null;
91 modifier = Simantics.getSession().syncRequest(new Read<Modifier>() {
94 public Modifier perform(ReadGraph graph) throws ManyObjectsForFunctionalRelationException, ServiceException {
95 ModelingResources mr = ModelingResources.getInstance(graph);
96 Layer0 l0 = Layer0.getInstance(graph);
97 Resource type = graph.getPossibleObject(data, mr.SymbolToComponentType);
99 LabelModifier modifier = new LabelModifier(Simantics.getSession(), type, l0.HasName) {
101 public String isValid(String label) {
103 return "Empty name not allowed";
112 } catch (DatabaseException e) {
120 public void delete() throws DeleteException {
123 Simantics.getSession().syncRequest(new WriteRequest() {
126 public void perform(WriteGraph graph) throws DatabaseException {
127 Layer0 l0 = Layer0.getInstance(graph);
128 StructuralResource2 st = StructuralResource2.getInstance(graph);
129 ModelingResources mr = ModelingResources.getInstance(graph);
131 Resource type = graph.getPossibleObject(data, mr.SymbolToComponentType);
132 Resource model = graph.getSingleObject(type, l0.PartOf);
133 Resource modelConfiguration = graph.getSingleObject(model, SimulationResource.getInstance(graph).HasConfiguration);
134 if (!graph.syncRequest(new ObjectsWithType(modelConfiguration, l0.ConsistsOf, type)).isEmpty()) {
135 System.out.println("The module is used at the model configuration");
138 Collection<Resource> moduleTypes = graph.syncRequest(new ObjectsWithType(model, l0.ConsistsOf, st.ComponentType));
139 for(Resource r : moduleTypes) {
140 Resource configuration = graph.getSingleObject(r, st.IsDefinedBy);
141 if(!graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, type)).isEmpty()) {
142 System.out.println("The module is used at another module: " + graph.getRelatedValue(r, l0.HasName));
146 graph.deny(model, l0.ConsistsOf, type);
149 } catch (DatabaseException e) {
154 @SuppressWarnings("rawtypes")
156 public Object getAdapter(Class adapter) {
157 if(PasteHandler.class == adapter && configuration != null)
158 return new DefaultPasteHandler(configuration);
159 return super.getAdapter(adapter);