]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/Layer0Utils.java
Utility function for claiming literals
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / util / Layer0Utils.java
index 56e1e11f23a10893b0b2818c31845e5bbe7d1c48..d89d7165226e7522fdf0a015a7209418e5185847 100644 (file)
@@ -882,7 +882,7 @@ public class Layer0Utils {
 
        public static Collection<Resource> copyTo(WriteGraph graph, Resource targetContainer, PasteEventHandler handler, CopyHandler copyHandler, PasteHandler pasteHandler) throws DatabaseException {
                SimanticsClipboardImpl clipboard = new SimanticsClipboardImpl();
-               copyHandler.copyToClipboard(graph, clipboard);
+               copyHandler.copyToClipboard(graph, clipboard, new NullProgressMonitor());
                if(targetContainer != null) {
                        if(pasteHandler == null) pasteHandler = graph.adapt(targetContainer, PasteHandler.class);
                        return pasteHandler.pasteFromClipboard(graph, clipboard, handler);
@@ -927,6 +927,27 @@ public class Layer0Utils {
                return stm != null && stm.isAsserted(subject);
        }
 
+       /*
+        * Works around problems in WriteGraph methods with similar signature. 
+        * Especially handles better some cases with existing literals.
+        * 
+        */
+       public static void claimLiteral(WriteGraph graph, Resource r, Resource p, Resource i, Resource t, Object value, Binding binding) throws DatabaseException {
+           Statement stm = graph.getPossibleStatement(r, p);
+           if(stm != null && !stm.isAsserted(r)) {
+               if(graph.isInstanceOf(stm.getObject(), t)) {
+                   // Existing statement is compatible, reuse the literal
+                   graph.claimValue(stm.getObject(), value, binding);
+                   return;
+               } else {
+                   // Existing statement is incompatible - remove it
+                   graph.deny(stm);
+               }
+           }
+           // Create new statement
+           graph.claimLiteral(r, p, i, t, value, binding);
+       }
+       
        public static void setExpression(WriteGraph graph, Variable context, String text, Resource expressionValueType) throws DatabaseException {
                
                Resource value = context.getRepresents(graph);
@@ -1157,7 +1178,8 @@ public class Layer0Utils {
 
         Resource indexRoot = graph.syncRequest(new PossibleVariableIndexRoot(variable));
         if(indexRoot == null) return false;
-        if(variable.equals(indexRoot)) return false;
+        Resource represents = variable.getPossibleRepresents(graph);
+        if(represents != null && represents.equals(indexRoot)) return false;
         return isPublished(graph, indexRoot);
 
     }
@@ -1192,7 +1214,7 @@ public class Layer0Utils {
 
        SimanticsClipboardImpl cp = new SimanticsClipboardImpl();
        CopyHandler c1 = graph.adapt(r, CopyHandler.class);
-       c1.copyToClipboard(graph, cp);
+       c1.copyToClipboard(graph, cp, null);
        Collection<Set<Representation>> reps = cp.getContents();
        if(reps.size() != 1) return null;
        return ClipboardUtils.accept(graph, reps.iterator().next(), SimanticsKeys.KEY_TRANSFERABLE_GRAPH);
@@ -1203,7 +1225,7 @@ public class Layer0Utils {
 
        SimanticsClipboardImpl cp = new SimanticsClipboardImpl();
        CopyHandler c1 = graph.adapt(r, CopyHandler.class);
-       c1.copyToClipboard(graph, cp);
+       c1.copyToClipboard(graph, cp, null);
        Collection<Set<Representation>> reps = cp.getContents();
        if(reps.size() != 1) return null;
        return ClipboardUtils.accept(graph, reps.iterator().next(), SimanticsKeys.KEY_TRANSFERABLE_GRAPH_SOURCE);