]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/ChangeMappingTypeHandler.java
Fixed most warnings from district codebase after JavaSE-11 switch
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / contributions / ChangeMappingTypeHandler.java
index 2b3d1888a41b2a0218221444ac8fbf5536f02ce2..3b33020f452d5e1252c7fc72193f53da437a3e1a 100644 (file)
@@ -7,6 +7,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
+import java.util.stream.Collectors;
 
 import javax.inject.Named;
 
@@ -47,6 +48,8 @@ import org.simantics.db.exception.ServiceException;
 import org.simantics.db.exception.ValidationException;
 import org.simantics.db.layer0.SelectionHints;
 import org.simantics.db.procedure.Procedure;
+import org.simantics.db.request.Read;
+import org.simantics.district.network.DistrictNetworkUtil;
 import org.simantics.district.network.ontology.DistrictNetworkResource;
 import org.simantics.district.network.ui.function.Functions;
 import org.simantics.district.network.ui.internal.Activator;
@@ -60,36 +63,29 @@ public class ChangeMappingTypeHandler {
 
     @CanExecute
     public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) {
-//        List<Resource> elements = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
-//        if (elements.size() < 1)
-//            return false;
-//        try {
-//            return Simantics.getSession().syncRequest(new Read<Boolean>() {
-//
-//                @Override
-//                public Boolean perform(ReadGraph graph) throws DatabaseException {
-//                    Layer0 L0 = Layer0.getInstance(graph);
-//                    Resource instanceOf = null;
-//                    for (Resource element : elements) {
-//                        if (instanceOf == null) {
-//                            instanceOf = graph.getSingleObject(element, L0.InstanceOf);
-//                        } else {
-//                            Resource currentInstanceOf = graph.getSingleObject(element, L0.InstanceOf);
-//                            if (!currentInstanceOf.equals(instanceOf)) {
-//                                return false;
-//                            }
-//                        }
-//                    }
-//                    return true;
-//                }
-//            });
-//        } catch (DatabaseException e) {
-//            e.printStackTrace();
-//            return false;
-//        }
-        return true;
+        List<Resource> elements = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
+        if (elements.size() < 1)
+            return false;
+        try {
+            return Simantics.getSession().syncRequest(new Read<Boolean>() {
+
+                @Override
+                public Boolean perform(ReadGraph graph) throws DatabaseException {
+                    DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+                    for (Resource selection : elements) {
+                        if (!graph.isInstanceOf(selection, DN.Element)) {
+                            return false;
+                        }
+                    }
+                    return true;
+                }
+            });
+        } catch (DatabaseException e) {
+            LOGGER.error("Could not evaluate if mapping can be changed for selection {}", elements, e);
+            return false;
+        }
     }
-    
+
     @Execute
     public void execute(@Named(IServiceConstants.ACTIVE_SELECTION) Object selection) {
         final List<Resource> elements = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
@@ -152,15 +148,11 @@ public class ChangeMappingTypeHandler {
                                 
                                 @Override
                                 public void perform(WriteGraph graph) throws DatabaseException {
-                                    DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
-                                    
                                     for (Map.Entry<Resource, Collection<NamedResource>> entry : results.entrySet()) {
-                                        Resource newMapping = entry.getKey();
-                                        Collection<NamedResource> elements = entry.getValue();
-                                        for (NamedResource element : elements) {
-                                            graph.deny(element.getResource(), DN.HasMapping);
-                                            graph.claim(element.getResource(), DN.HasMapping, newMapping);
-                                        }
+                                        List<Resource> elements = entry.getValue().stream()
+                                            .map(NamedResource::getResource)
+                                            .collect(Collectors.toList());
+                                        DistrictNetworkUtil.changeMappingType(graph, entry.getKey(), elements);
                                     }
                                 }
                             });
@@ -189,18 +181,12 @@ public class ChangeMappingTypeHandler {
         
         private Map<NamedResource, Map<String, Resource>> possibleMappings = new HashMap<>();
         
-        private Resource defaultVertexMapping;
-
         protected SelectMappingDialog(Shell parentShell, CompletableFuture<Map<NamedResource, Collection<NamedResource>>> elements) {
             super(parentShell);
             this.elements = elements;
             setTitle("Change mappings");
         }
 
-        public Resource getDefaultVertexMapping() {
-            return defaultVertexMapping;
-        }
-
         @Override
         protected Control createDialogArea(Composite parent) {
             composite = (Composite) super.createDialogArea(parent);
@@ -245,18 +231,25 @@ public class ChangeMappingTypeHandler {
         
         @Override
         protected void computeResult() {
+            Map<NamedResource, Collection<NamedResource>> currentElements = null;
+            try {
+                currentElements = elements.get();
+            } catch (InterruptedException | ExecutionException e) {
+                LOGGER.error("Could not get currentElements", e);
+                throw new RuntimeException("Could not get currentElements", e);
+            }
             for (Map.Entry<NamedResource, Combo> combos : mappingCombos.entrySet()) {
                 NamedResource resource = combos.getKey();
                 Combo c = combos.getValue();
                 String item = c.getItem(c.getSelectionIndex());
-                
+                Collection<NamedResource> collection = currentElements.get(resource);
                 Map<String, Resource> map = possibleMappings.get(resource);
                 Resource newMapping = map.get(item);
                 results.compute(newMapping, (t, u) -> {
                     if (u == null) {
                         u = new HashSet<>();
                     }
-                    u.add(resource);
+                    u.addAll(collection);
                     return u;
                 });
             }