]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/PasteDistrictVertexHandler.java
b9aedefe791e53f3f5e10ea80404849c70667002
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / contributions / PasteDistrictVertexHandler.java
1 package org.simantics.district.network.ui.contributions;
2
3 import java.util.Collection;
4 import java.util.List;
5 import java.util.concurrent.ForkJoinPool;
6 import java.util.concurrent.TimeUnit;
7
8 import javax.inject.Named;
9
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;
30
31 public class PasteDistrictVertexHandler {
32
33     private static final Logger LOGGER = LoggerFactory.getLogger(PasteDistrictVertexHandler.class);
34
35     @CanExecute
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)
39             return false;
40         try {
41             return Simantics.getSession().syncRequest(new Read<Boolean>() {
42
43                 @Override
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)) {
48                             return false;
49                         }
50                     }
51                     return true;
52                 }
53             });
54         } catch (DatabaseException e) {
55             LOGGER.error("Could not evaluate if mapping can be changed for selection {}", elements, e);
56             return false;
57         }
58     }
59
60     @Execute
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;
65         
66         Resource targetElement = elements.get(0);
67         Resource sourceElement = copiedElements.get(0);
68         
69         try {
70             Simantics.getSession().syncRequest(new WriteRequest() {
71                 
72                 @Override
73                 public void perform(WriteGraph graph) throws DatabaseException {
74                     DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
75                     
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() {
84                             
85                             @Override
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);
91                             }
92                         });                        
93                     }, 200, TimeUnit.MILLISECONDS);
94                 }
95                 
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);
99                     if (cut) {
100                         graph.deny(sourceElement, DN.Mapping);
101                         graph.claim(sourceElement, DN.Mapping, targetMapping);
102                     }
103                     graph.deny(targetElement, DN.Mapping);
104                     graph.claim(targetElement, DN.Mapping, sourceMapping);
105                 }
106
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);
110                     if (cut) {
111                         graph.deny(sourceElement, DN.Vertex_HasElevation);
112                         graph.claimLiteral(sourceElement, DN.Vertex_HasElevation, targetElevation, Bindings.DOUBLE);
113                     }
114                     graph.deny(targetElement, DN.Vertex_HasElevation);
115                     graph.claimLiteral(targetElement, DN.Vertex_HasElevation, sourceElevation, Bindings.DOUBLE);
116                 }
117
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);
122                     if (cut) {
123                         graph.deny(sourceElement, DIA.HasLocation);
124                         graph.claimLiteral(sourceElement, DIA.HasLocation, targetLocation, Bindings.DOUBLE_ARRAY);
125                     }
126                     graph.deny(targetElement, DIA.HasLocation);
127                     graph.claimLiteral(targetElement, DIA.HasLocation, sourceLocation, Bindings.DOUBLE_ARRAY);
128                 }
129                 
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);
141                     }
142                     for (Resource targetVertexInverse : targetStartVertex_inverse) {
143                         graph.claim(sourceElement, DN.HasStartVertex_Inverse, targetVertexInverse);
144                     }
145                     for (Resource endVertexInverse : sourceEndVertex_inverse) {
146                         graph.claim(targetElement, DN.HasEndVertex_Inverse, endVertexInverse);
147                     }
148                     for (Resource targetVertexInverse : targetEndVertex_inverse) {
149                         graph.claim(sourceElement, DN.HasEndVertex_Inverse, targetVertexInverse);
150                     }
151                 }
152             });
153         } catch (DatabaseException e) {
154             e.printStackTrace();
155         }
156         
157     }
158 }