--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.diagram.synchronization.graph;\r
+\r
+import java.util.Arrays;\r
+\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.CommentMetadata;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.diagram.synchronization.ModificationAdapter;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class PropertyModification extends ModificationAdapter {\r
+ private final Resource owner;\r
+ private final Resource relation;\r
+ private final Resource type;\r
+ private final Object value;\r
+ private final Binding binding;\r
+\r
+ public PropertyModification(Resource propertyOwner, Resource propertyRelation, Resource propertyType, Object value, Binding binding) {\r
+ super(LOW_PRIORITY);\r
+ this.owner = propertyOwner;\r
+ this.relation = propertyRelation;\r
+ this.type = propertyType;\r
+ this.value = value;\r
+ this.binding = binding;\r
+ }\r
+ \r
+ private final boolean arrayEquals(Object av1, Object av2) {\r
+ if (av2 == null)\r
+ return false;\r
+ Class<?> c1 = av1.getClass().getComponentType();\r
+ Class<?> c2 = av2.getClass().getComponentType();\r
+ if (c2 == null || !c1.equals(c2))\r
+ return false;\r
+ boolean p1 = c1.isPrimitive();\r
+ boolean p2 = c2.isPrimitive();\r
+ if (p1 != p2)\r
+ return false;\r
+ if (!p1)\r
+ return Arrays.equals((Object[]) av1, (Object[]) av2);\r
+ if (boolean.class.equals(c1))\r
+ return Arrays.equals((boolean[]) av1, (boolean[]) av2);\r
+ else if (byte.class.equals(c1))\r
+ return Arrays.equals((byte[]) av1, (byte[]) av2);\r
+ else if (int.class.equals(c1))\r
+ return Arrays.equals((int[]) av1, (int[]) av2);\r
+ else if (long.class.equals(c1))\r
+ return Arrays.equals((long[]) av1, (long[]) av2);\r
+ else if (float.class.equals(c1))\r
+ return Arrays.equals((float[]) av1, (float[]) av2);\r
+ else if (double.class.equals(c1))\r
+ return Arrays.equals((double[]) av1, (double[]) av2);\r
+ throw new RuntimeException("??? Contact application querySupport.");\r
+ }\r
+\r
+ private boolean sameValue(Object oldValue, Object newValue) {\r
+ if (newValue.getClass().isArray()) {\r
+ return arrayEquals(newValue, oldValue);\r
+ } else {\r
+ return newValue.equals(oldValue);\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public void perform(WriteGraph g) throws DatabaseException {\r
+ \r
+ Object existing = g.getPossibleRelatedValue(owner, relation, binding);\r
+ if(!sameValue(existing, value)) {\r
+ g.markUndoPoint();\r
+ DiagramGraphUtil.setRelatedValue(g, owner, relation, type, value, binding);\r
+ // Add comment to change set.\r
+ CommentMetadata cm = g.getMetadata(CommentMetadata.class);\r
+ g.addMetadata(cm.add("Changed property " + NameUtils.getSafeName(g, relation)));\r
+ }\r
+\r
+ }\r
+}
\ No newline at end of file