]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/PasteDistrictVertexHandler.java
Support copy and cut of dh element & create connections after
[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.TimeUnit;
6
7 import javax.inject.Named;
8
9 import org.eclipse.e4.core.di.annotations.CanExecute;
10 import org.eclipse.e4.core.di.annotations.Execute;
11 import org.eclipse.e4.ui.services.IServiceConstants;
12 import org.eclipse.jface.viewers.ISelection;
13 import org.simantics.Simantics;
14 import org.simantics.databoard.Bindings;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.Statement;
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.DistrictNetworkUtil;
26 import org.simantics.district.network.ontology.DistrictNetworkResource;
27 import org.simantics.layer0.Layer0;
28 import org.simantics.utils.threads.ThreadUtils;
29 import org.simantics.utils.ui.ISelectionUtils;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class PasteDistrictVertexHandler {
34
35     private static final Logger LOGGER = LoggerFactory.getLogger(PasteDistrictVertexHandler.class);
36
37     @CanExecute
38     public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) {
39         List<Resource> elements = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
40         if (elements.size() < 1)
41             return false;
42         try {
43             return Simantics.getSession().syncRequest(new Read<Boolean>() {
44
45                 @Override
46                 public Boolean perform(ReadGraph graph) throws DatabaseException {
47                     DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
48                     for (Resource selection : elements) {
49                         if (!graph.isInstanceOf(selection, DN.Element)) {
50                             return false;
51                         }
52                     }
53                     return true;
54                 }
55             });
56         } catch (DatabaseException e) {
57             LOGGER.error("Could not evaluate if mapping can be changed for selection {}", elements, e);
58             return false;
59         }
60     }
61     
62     private static Resource doCopy(WriteGraph graph, Resource diagram, Resource target, Resource source) throws DatabaseException {
63                 DiagramResource DIA = DiagramResource.getInstance(graph);
64                 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
65                 double[] location = graph.getRelatedValue(target, DIA.HasLocation, Bindings.DOUBLE_ARRAY);
66                 double elevation = graph.getRelatedValue(target, DN.Vertex_HasElevation, Bindings.DOUBLE);
67                 Resource vertex = DistrictNetworkUtil.createVertex(graph, diagram, location, elevation);
68                 copySourceToTarget(graph, source, vertex);
69                 return vertex;
70     }
71
72     private static void copySourceToTarget(WriteGraph graph, Resource source, Resource target) throws DatabaseException {
73         Layer0 L0 = Layer0.getInstance(graph);
74         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
75         graph.deny(target, DN.HasMapping);
76         
77                 Collection<Statement> statements = graph.getStatements(source, L0.HasProperty);
78                 Collection<Statement> assertedStatements = graph.getAssertedStatements(source, L0.HasProperty);
79                 statements.removeAll(assertedStatements);
80                 for (Statement stm : statements) {
81                         if (!graph.hasStatement(target, stm.getPredicate())) {
82                                 graph.claim(target, stm.getPredicate(), stm.getObject());
83                         }
84                 }
85     }
86
87         private static void copyVertexInverses(WriteGraph graph, Resource sourceElement, Resource targetElement) throws DatabaseException {
88                 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
89                 Collection<Resource> sourceStartVertex_inverse = graph.getObjects(sourceElement, DN.HasStartVertex_Inverse);
90                 Collection<Resource> sourceEndVertex_inverse = graph.getObjects(sourceElement, DN.HasEndVertex_Inverse);
91                 Collection<Resource> targetStartVertex_inverse = graph.getObjects(targetElement, DN.HasStartVertex_Inverse);
92                 Collection<Resource> targetEndVertex_inverse = graph.getObjects(targetElement, DN.HasEndVertex_Inverse);
93                 graph.deny(sourceElement, DN.HasStartVertex_Inverse);
94                 graph.deny(sourceElement, DN.HasEndVertex_Inverse);
95                 graph.deny(targetElement, DN.HasStartVertex_Inverse);
96                 graph.deny(targetElement, DN.HasEndVertex_Inverse);
97                 for (Resource startVertexInverse : sourceStartVertex_inverse) {
98                         graph.claim(targetElement, DN.HasStartVertex_Inverse, startVertexInverse);
99                 }
100                 for (Resource targetVertexInverse : targetStartVertex_inverse) {
101                         graph.claim(sourceElement, DN.HasStartVertex_Inverse, targetVertexInverse);
102                 }
103                 for (Resource endVertexInverse : sourceEndVertex_inverse) {
104                         graph.claim(targetElement, DN.HasEndVertex_Inverse, endVertexInverse);
105                 }
106                 for (Resource targetVertexInverse : targetEndVertex_inverse) {
107                         graph.claim(sourceElement, DN.HasEndVertex_Inverse, targetVertexInverse);
108                 }
109         }
110     
111     private static void deleteExistingTarget(WriteGraph graph, Resource target) throws DatabaseException {
112         RemoverUtil.remove(graph, target);
113     }
114     
115     @Execute
116     public void execute(@Named(IServiceConstants.ACTIVE_SELECTION) Object selection) {
117         final List<Resource> elements = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
118         final List<Resource> copiedElements = CopyDistrictVertexHandler.elements; // TODO: this could be implemented more nicely with clipboard ?
119         boolean cut = CopyDistrictVertexHandler.cut;
120         
121         Resource targetElement = elements.get(0);
122         Resource sourceElement = copiedElements.get(0);
123         
124         try {
125             Simantics.getSession().syncRequest(new WriteRequest() {
126                 
127                 @Override
128                 public void perform(WriteGraph graph) throws DatabaseException {
129                     DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
130                     
131                     Resource sourceMappedElement = graph.getPossibleObject(sourceElement, DN.MappedComponent);
132                     Resource targetMappedElement = graph.getPossibleObject(targetElement, DN.MappedComponent);
133                     if (sourceMappedElement != null && cut)
134                         RemoverUtil.remove(graph, sourceMappedElement);
135                     if (targetMappedElement != null)
136                         RemoverUtil.remove(graph, targetMappedElement);
137                 }
138             });
139             ThreadUtils.getNonBlockingWorkExecutor().schedule(() -> {
140                 try {
141                                         Simantics.getSession().syncRequest(new WriteRequest() {
142
143                                                 @Override
144                                                 public void perform(WriteGraph graph) throws DatabaseException {
145                                                         Layer0 L0 = Layer0.getInstance(graph);
146
147                                                         Resource diagram = graph.getSingleObject(sourceElement, L0.PartOf);
148                                                         Resource newTarget = doCopy(graph, diagram, targetElement, sourceElement);
149                                                         if (cut) {
150                                                                 Resource newSource = doCopy(graph, diagram, sourceElement, targetElement);
151                                                                 copyVertexInverses(graph, sourceElement, newSource);
152                                                                 deleteExistingTarget(graph, sourceElement);
153                                                         }
154                                                         copyVertexInverses(graph, targetElement, newTarget);
155                                                         deleteExistingTarget(graph, targetElement);
156                                                 }
157                                         });
158                                 } catch (DatabaseException e) {
159                                         // TODO Auto-generated catch block
160                                         e.printStackTrace();
161                                 }
162             }, 500, TimeUnit.MILLISECONDS);
163
164 //              ThreadUtils.getNonBlockingWorkExecutor().schedule(() -> {
165 //                Simantics.getSession().asyncRequest(new WriteRequest() {
166 //                    
167 //                    @Override
168 //                    public void perform(WriteGraph graph) throws DatabaseException {
169 //                        DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
170 //                        //copyMapping(graph, DN, sourceElement, targetElement);
171 //                        copyLocation(graph, DN, sourceElement, targetElement, cut);
172 //                        copyVertexInverses(graph, DN, sourceElement, targetElement, cut);
173 //                        copyElevation(graph, DN, sourceElement, targetElement, cut);
174 //                    }
175 //                    
176 //                    private void copyMapping(WriteGraph graph, DistrictNetworkResource DN, Resource sourceElement, Resource targetElement) throws DatabaseException {
177 //                        Resource sourceMapping = graph.getSingleObject(sourceElement, DN.Mapping);
178 //                        Resource targetMapping = graph.getSingleObject(targetElement, DN.Mapping);
179 //                        if (cut) {
180 //                            graph.deny(sourceElement, DN.Mapping);
181 //                            graph.claim(sourceElement, DN.Mapping, targetMapping);
182 //                        }
183 //                        graph.deny(targetElement, DN.Mapping);
184 //                        graph.claim(targetElement, DN.Mapping, sourceMapping);
185 //                    }
186 //
187 //                    private void copyElevation(WriteGraph graph, DistrictNetworkResource DN, Resource sourceElement, Resource targetElement, boolean cut) throws DatabaseException {
188 //                        double sourceElevation = graph.getRelatedValue(sourceElement, DN.Vertex_HasElevation, Bindings.DOUBLE);
189 //                        double targetElevation = graph.getRelatedValue(targetElement, DN.Vertex_HasElevation, Bindings.DOUBLE);
190 //                        if (cut) {
191 //                            graph.deny(sourceElement, DN.Vertex_HasElevation);
192 //                            graph.claimLiteral(sourceElement, DN.Vertex_HasElevation, targetElevation, Bindings.DOUBLE);
193 //                        }
194 //                        graph.deny(targetElement, DN.Vertex_HasElevation);
195 //                        graph.claimLiteral(targetElement, DN.Vertex_HasElevation, sourceElevation, Bindings.DOUBLE);
196 //                    }
197 //
198 //                    private void copyLocation(WriteGraph graph, DistrictNetworkResource DN, Resource sourceElement, Resource targetElement, boolean cut) throws DatabaseException {
199 //                        DiagramResource DIA = DiagramResource.getInstance(graph);
200 //                        double[] sourceLocation = graph.getRelatedValue(sourceElement, DIA.HasLocation, Bindings.DOUBLE_ARRAY);
201 //                        double[] targetLocation = graph.getRelatedValue(targetElement, DIA.HasLocation, Bindings.DOUBLE_ARRAY);
202 //                        if (cut) {
203 //                            graph.deny(sourceElement, DIA.HasLocation);
204 //                            graph.claimLiteral(sourceElement, DIA.HasLocation, targetLocation, Bindings.DOUBLE_ARRAY);
205 //                        }
206 //                        graph.deny(targetElement, DIA.HasLocation);
207 //                        graph.claimLiteral(targetElement, DIA.HasLocation, sourceLocation, Bindings.DOUBLE_ARRAY);
208 //                    }
209 //                    
210
211 //                    
212 //                    
213 //                });
214 //            }, 500, TimeUnit.MILLISECONDS);
215
216         } catch (DatabaseException e) {
217             e.printStackTrace();
218         }
219         
220     }
221 }