]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.document.server.io/src/org/simantics/document/server/io/JSONObjectUtils.java
TreeGridWidget improvements
[simantics/platform.git] / bundles / org.simantics.document.server.io / src / org / simantics / document / server / io / JSONObjectUtils.java
index 8913645a9946c3f4884fd4e8c5ae9f028afaea1a..004166dc0c39bf387dd471b9b8617bb8c8853885 100644 (file)
@@ -13,6 +13,7 @@
 package org.simantics.document.server.io;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
@@ -313,6 +314,26 @@ public class JSONObjectUtils {
         return Collections.emptyList();
     }
     
+    @SuppressWarnings("unchecked")
+    public static Collection<ITreeTableCell> getTreeTableCells(IJSONObject object) {
+        try {
+            Object tableCells = object.getValue("tableCells");
+            if (tableCells instanceof String) {
+                String tableCellsS = (String) tableCells;
+                if (tableCellsS.length() == 0)
+                    return Collections.emptyList();
+            }
+            if (tableCells != null) {
+               return (List<ITreeTableCell>) tableCells;
+            } else {
+               return Collections.emptyList();
+            }
+        } catch (ClassCastException e) {
+            e.printStackTrace();
+        }
+        return Collections.emptyList();
+    }
+
     public static Collection<FileInfo> getFiles(IJSONObject object) {
         try {
             @SuppressWarnings("unchecked")
@@ -343,4 +364,72 @@ public class JSONObjectUtils {
         else
             return Collections.<IListItem>emptyList();
     }
+    
+    @SuppressWarnings("unchecked")
+    public static Collection<ITreeTableCell> getTreeTableCell(IJSONObject object) {
+        try {
+            Object treeTableCells = object.getValue("tableCells");
+            if (treeTableCells instanceof String) {
+                String tableCellsS = (String) treeTableCells;
+                if (tableCellsS.length() == 0)
+                    return Collections.emptyList();
+            }
+            if (treeTableCells != null) {
+                return (List<ITreeTableCell>) treeTableCells;
+            } else {
+                return Collections.emptyList();
+            }
+        } catch (ClassCastException e) {
+            e.printStackTrace();
+        }
+        return Collections.emptyList();
+    }
+
+    public static final boolean equalObjects(Object oldValue, Object newValue) {
+       if (newValue != null) {
+               if (newValue.getClass().isArray()) {
+                       return arrayEquals(newValue, oldValue);
+               } else {
+                       return newValue.equals(oldValue);
+               }
+       } else
+               return oldValue == null;
+    }
+
+    
+       /**
+        * @param av1 an array (guaranteed)
+        * @param av2 any object
+        * @return <code>true</code> if the two arrays are equal
+        */
+       private static final boolean arrayEquals(Object av1, Object av2) {
+               if (av2 == null)
+                       return false;
+               Class<?> c1 = av1.getClass().getComponentType();
+               Class<?> c2 = av2.getClass().getComponentType();
+               if (c2 == null || !c1.equals(c2))
+                       return false;
+               boolean p1 = c1.isPrimitive();
+               boolean p2 = c2.isPrimitive();
+               if (p1 != p2)
+                       return false;
+               if (!p1)
+                       return Arrays.equals((Object[]) av1, (Object[]) av2);
+               if (boolean.class.equals(c1))
+                       return Arrays.equals((boolean[]) av1, (boolean[]) av2);
+               else if (byte.class.equals(c1))
+                       return Arrays.equals((byte[]) av1, (byte[]) av2);
+               else if (int.class.equals(c1))
+                       return Arrays.equals((int[]) av1, (int[]) av2);
+               else if (long.class.equals(c1))
+                       return Arrays.equals((long[]) av1, (long[]) av2);
+               else if (float.class.equals(c1))
+                       return Arrays.equals((float[]) av1, (float[]) av2);
+               else if (double.class.equals(c1))
+                       return Arrays.equals((double[]) av1, (double[]) av2);
+               throw new RuntimeException("Unsupported objects for equality testing ." + av1 + " vs. " + av2);
+               
+       }
+    
+    
 }