1 package org.simantics.district.network.ui.contributions;
3 import java.util.Collection;
5 import java.util.concurrent.ForkJoinPool;
6 import java.util.concurrent.TimeUnit;
8 import javax.inject.Named;
10 import org.eclipse.e4.core.di.annotations.CanExecute;
11 import org.eclipse.e4.core.di.annotations.Execute;
12 import org.eclipse.e4.ui.services.IServiceConstants;
13 import org.eclipse.jface.viewers.ISelection;
14 import org.simantics.Simantics;
15 import org.simantics.databoard.Bindings;
16 import org.simantics.db.ReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.WriteGraph;
19 import org.simantics.db.common.request.WriteRequest;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.layer0.SelectionHints;
22 import org.simantics.db.layer0.util.RemoverUtil;
23 import org.simantics.db.request.Read;
24 import org.simantics.diagram.stubs.DiagramResource;
25 import org.simantics.district.network.ontology.DistrictNetworkResource;
26 import org.simantics.utils.threads.ThreadUtils;
27 import org.simantics.utils.ui.ISelectionUtils;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public class PasteDistrictVertexHandler {
33 private static final Logger LOGGER = LoggerFactory.getLogger(PasteDistrictVertexHandler.class);
36 public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) {
37 List<Resource> elements = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
38 if (elements.size() < 1)
41 return Simantics.getSession().syncRequest(new Read<Boolean>() {
44 public Boolean perform(ReadGraph graph) throws DatabaseException {
45 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
46 for (Resource selection : elements) {
47 if (!graph.isInstanceOf(selection, DN.Element)) {
54 } catch (DatabaseException e) {
55 LOGGER.error("Could not evaluate if mapping can be changed for selection {}", elements, e);
61 public void execute(@Named(IServiceConstants.ACTIVE_SELECTION) Object selection) {
62 final List<Resource> elements = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
63 final List<Resource> copiedElements = CopyDistrictVertexHandler.elements; // TODO: this could be implemented more nicely with clipboard ?
64 boolean cut = CopyDistrictVertexHandler.cut;
66 Resource targetElement = elements.get(0);
67 Resource sourceElement = copiedElements.get(0);
70 Simantics.getSession().syncRequest(new WriteRequest() {
73 public void perform(WriteGraph graph) throws DatabaseException {
74 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
76 Resource sourceMappedElement = graph.getPossibleObject(sourceElement, DN.MappedComponent);
77 Resource targetMappedElement = graph.getPossibleObject(targetElement, DN.MappedComponent);
78 if (sourceMappedElement != null)
79 RemoverUtil.remove(graph, sourceMappedElement);
80 if (targetMappedElement != null)
81 RemoverUtil.remove(graph, targetMappedElement);
82 ThreadUtils.getNonBlockingWorkExecutor().schedule(() -> {
83 Simantics.getSession().asyncRequest(new WriteRequest() {
86 public void perform(WriteGraph graph) throws DatabaseException {
87 //copyMapping(graph, DN, sourceElement, targetElement);
88 copyLocation(graph, DN, sourceElement, targetElement, cut);
89 copyVertexInverses(graph, DN, sourceElement, targetElement, cut);
90 copyElevation(graph, DN, sourceElement, targetElement, cut);
93 }, 200, TimeUnit.MILLISECONDS);
96 private void copyMapping(WriteGraph graph, DistrictNetworkResource DN, Resource sourceElement, Resource targetElement) throws DatabaseException {
97 Resource sourceMapping = graph.getSingleObject(sourceElement, DN.Mapping);
98 Resource targetMapping = graph.getSingleObject(targetElement, DN.Mapping);
100 graph.deny(sourceElement, DN.Mapping);
101 graph.claim(sourceElement, DN.Mapping, targetMapping);
103 graph.deny(targetElement, DN.Mapping);
104 graph.claim(targetElement, DN.Mapping, sourceMapping);
107 private void copyElevation(WriteGraph graph, DistrictNetworkResource DN, Resource sourceElement, Resource targetElement, boolean cut) throws DatabaseException {
108 double sourceElevation = graph.getRelatedValue(sourceElement, DN.Vertex_HasElevation, Bindings.DOUBLE);
109 double targetElevation = graph.getRelatedValue(targetElement, DN.Vertex_HasElevation, Bindings.DOUBLE);
111 graph.deny(sourceElement, DN.Vertex_HasElevation);
112 graph.claimLiteral(sourceElement, DN.Vertex_HasElevation, targetElevation, Bindings.DOUBLE);
114 graph.deny(targetElement, DN.Vertex_HasElevation);
115 graph.claimLiteral(targetElement, DN.Vertex_HasElevation, sourceElevation, Bindings.DOUBLE);
118 private void copyLocation(WriteGraph graph, DistrictNetworkResource DN, Resource sourceElement, Resource targetElement, boolean cut) throws DatabaseException {
119 DiagramResource DIA = DiagramResource.getInstance(graph);
120 double[] sourceLocation = graph.getRelatedValue(sourceElement, DIA.HasLocation, Bindings.DOUBLE_ARRAY);
121 double[] targetLocation = graph.getRelatedValue(targetElement, DIA.HasLocation, Bindings.DOUBLE_ARRAY);
123 graph.deny(sourceElement, DIA.HasLocation);
124 graph.claimLiteral(sourceElement, DIA.HasLocation, targetLocation, Bindings.DOUBLE_ARRAY);
126 graph.deny(targetElement, DIA.HasLocation);
127 graph.claimLiteral(targetElement, DIA.HasLocation, sourceLocation, Bindings.DOUBLE_ARRAY);
130 private void copyVertexInverses(WriteGraph graph, DistrictNetworkResource DN, Resource sourceElement, Resource targetElement, boolean cut) throws DatabaseException {
131 Collection<Resource> sourceStartVertex_inverse = graph.getObjects(sourceElement, DN.HasStartVertex_Inverse);
132 Collection<Resource> sourceEndVertex_inverse = graph.getObjects(sourceElement, DN.HasEndVertex_Inverse);
133 Collection<Resource> targetStartVertex_inverse = graph.getObjects(targetElement, DN.HasStartVertex_Inverse);
134 Collection<Resource> targetEndVertex_inverse = graph.getObjects(targetElement, DN.HasEndVertex_Inverse);
135 graph.deny(sourceElement, DN.HasStartVertex_Inverse);
136 graph.deny(sourceElement, DN.HasEndVertex_Inverse);
137 graph.deny(targetElement, DN.HasStartVertex_Inverse);
138 graph.deny(targetElement, DN.HasEndVertex_Inverse);
139 for (Resource startVertexInverse : sourceStartVertex_inverse) {
140 graph.claim(targetElement, DN.HasStartVertex_Inverse, startVertexInverse);
142 for (Resource targetVertexInverse : targetStartVertex_inverse) {
143 graph.claim(sourceElement, DN.HasStartVertex_Inverse, targetVertexInverse);
145 for (Resource endVertexInverse : sourceEndVertex_inverse) {
146 graph.claim(targetElement, DN.HasEndVertex_Inverse, endVertexInverse);
148 for (Resource targetVertexInverse : targetEndVertex_inverse) {
149 graph.claim(sourceElement, DN.HasEndVertex_Inverse, targetVertexInverse);
153 } catch (DatabaseException e) {