]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/PropertyModification.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / synchronization / graph / PropertyModification.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.diagram.synchronization.graph;\r
13 \r
14 import java.util.Arrays;\r
15 \r
16 import org.simantics.databoard.binding.Binding;\r
17 import org.simantics.db.Resource;\r
18 import org.simantics.db.WriteGraph;\r
19 import org.simantics.db.common.CommentMetadata;\r
20 import org.simantics.db.common.utils.NameUtils;\r
21 import org.simantics.db.exception.DatabaseException;\r
22 import org.simantics.diagram.synchronization.ModificationAdapter;\r
23 \r
24 /**\r
25  * @author Tuukka Lehtonen\r
26  */\r
27 public class PropertyModification extends ModificationAdapter {\r
28     private final Resource owner;\r
29     private final Resource relation;\r
30     private final Resource type;\r
31     private final Object value;\r
32     private final Binding binding;\r
33 \r
34     public PropertyModification(Resource propertyOwner, Resource propertyRelation, Resource propertyType, Object value, Binding binding) {\r
35         super(LOW_PRIORITY);\r
36         this.owner = propertyOwner;\r
37         this.relation = propertyRelation;\r
38         this.type = propertyType;\r
39         this.value = value;\r
40         this.binding = binding;\r
41     }\r
42     \r
43         private final boolean arrayEquals(Object av1, Object av2) {\r
44                 if (av2 == null)\r
45                         return false;\r
46                 Class<?> c1 = av1.getClass().getComponentType();\r
47                 Class<?> c2 = av2.getClass().getComponentType();\r
48                 if (c2 == null || !c1.equals(c2))\r
49                         return false;\r
50                 boolean p1 = c1.isPrimitive();\r
51                 boolean p2 = c2.isPrimitive();\r
52                 if (p1 != p2)\r
53                         return false;\r
54                 if (!p1)\r
55                         return Arrays.equals((Object[]) av1, (Object[]) av2);\r
56                 if (boolean.class.equals(c1))\r
57                         return Arrays.equals((boolean[]) av1, (boolean[]) av2);\r
58                 else if (byte.class.equals(c1))\r
59                         return Arrays.equals((byte[]) av1, (byte[]) av2);\r
60                 else if (int.class.equals(c1))\r
61                         return Arrays.equals((int[]) av1, (int[]) av2);\r
62                 else if (long.class.equals(c1))\r
63                         return Arrays.equals((long[]) av1, (long[]) av2);\r
64                 else if (float.class.equals(c1))\r
65                         return Arrays.equals((float[]) av1, (float[]) av2);\r
66                 else if (double.class.equals(c1))\r
67                         return Arrays.equals((double[]) av1, (double[]) av2);\r
68                 throw new RuntimeException("??? Contact application querySupport.");\r
69         }\r
70 \r
71     private boolean sameValue(Object oldValue, Object newValue) {\r
72                 if (newValue.getClass().isArray()) {\r
73                         return arrayEquals(newValue, oldValue);\r
74                 } else {\r
75                         return newValue.equals(oldValue);\r
76                 }\r
77     }\r
78 \r
79     @Override\r
80     public void perform(WriteGraph g) throws DatabaseException {\r
81         \r
82         Object existing = g.getPossibleRelatedValue(owner, relation, binding);\r
83         if(!sameValue(existing, value)) {\r
84                 g.markUndoPoint();\r
85             DiagramGraphUtil.setRelatedValue(g, owner, relation, type, value, binding);\r
86                 // Add comment to change set.\r
87                 CommentMetadata cm = g.getMetadata(CommentMetadata.class);\r
88                 g.addMetadata(cm.add("Changed property " + NameUtils.getSafeName(g, relation)));\r
89         }\r
90 \r
91     }\r
92 }