(refs #7526) Allow population of UC to diagram directly 72/1072/1
authorHannu Niemistö <hannu.niemisto@semantum.fi>
Wed, 4 Oct 2017 09:19:19 +0000 (12:19 +0300)
committerHannu Niemistö <hannu.niemisto@semantum.fi>
Wed, 4 Oct 2017 09:19:19 +0000 (12:19 +0300)
Change-Id: I23b480f326107baaf0996e9da8c2670caab0585f

bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/PopulateElementDropParticipant.java

index 3b6da0b5bd69b4d683bcfc5fbe995031908cf02a..4f27406025f752cd713b5dcc77cab0a5a2a2a6e0 100644 (file)
@@ -159,10 +159,10 @@ public class PopulateElementDropParticipant extends AbstractDiagramParticipant i
                                     }
 
                                     try {
-                                        String valid = validateDrop(synchronizer.getSession(), r,
+                                        Object errorOrSymbolResource = validateDrop(synchronizer.getSession(), r,
                                                 diagram.<Resource> getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE));
-                                        if (valid == null) {
-                                            ElementClassDragItem item = new ElementClassDragItem(synchronizer.getNodeClass(r));
+                                        if (errorOrSymbolResource instanceof Resource) {
+                                            ElementClassDragItem item = new ElementClassDragItem(synchronizer.getNodeClass((Resource)errorOrSymbolResource));
                                             item.getHintContext().setHint(ElementHints.KEY_TRANSFORM, AffineTransform.getScaleInstance(1, 1));
                                             dp.add(item);
                                         }
@@ -207,10 +207,10 @@ public class PopulateElementDropParticipant extends AbstractDiagramParticipant i
         }
     }
 
-    private String validateDrop(RequestProcessor processor, final Resource draggedResource, final Resource dropTarget) throws DatabaseException {
-        return processor.syncRequest(new UniqueRead<String>() {
+    private Object validateDrop(RequestProcessor processor, final Resource draggedResource, final Resource dropTarget) throws DatabaseException {
+        return processor.syncRequest(new UniqueRead<Object>() {
             @Override
-            public String perform(ReadGraph graph) throws DatabaseException {
+            public Object perform(ReadGraph graph) throws DatabaseException {
 //                System.out.println("dragged resource: " + draggedResource);
 //                System.out.println("drop target resource: " + dropTarget);
                 Resource sourceModel = graph.syncRequest(new PossibleIndexRoot(draggedResource));
@@ -240,18 +240,28 @@ public class PopulateElementDropParticipant extends AbstractDiagramParticipant i
                     }
                 }
 
+                // Check if dragged object is symbol or component type and determine other
+                Resource componentType;
+                Resource symbol = graph.getPossibleObject(draggedResource, MOD.ComponentTypeToSymbol);
+                
+                if(symbol != null)
+                    componentType = draggedResource;
+                else {
+                    componentType = graph.getPossibleObject(draggedResource, MOD.SymbolToComponentType);
+                    symbol = draggedResource;
+                }
+                
                 // Prevent dragging a symbol of component type into its own configuration.
-                Resource componentTypeFromSymbol = graph.getPossibleObject(draggedResource, MOD.SymbolToComponentType);
-                if (componentTypeFromSymbol != null) {
+                if (componentType != null) {
                     if (configuration != null) {
                         Resource componentTypeFromDiagram = graph.getPossibleObject(configuration, STR.Defines);
-                        if (componentTypeFromDiagram != null && componentTypeFromSymbol.equals(componentTypeFromDiagram)) {
+                        if (componentTypeFromDiagram != null && componentType.equals(componentTypeFromDiagram)) {
                             return "Cannot instantiate user component within its own configuration.";
                         }
                     }
                 }
 
-                return null;
+                return symbol;
             }
         });
     }