]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.diagram/src/org/simantics/diagram/elements/ElementPropertySetter.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / elements / ElementPropertySetter.java
diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/ElementPropertySetter.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/ElementPropertySetter.java
new file mode 100644 (file)
index 0000000..d2f93c4
--- /dev/null
@@ -0,0 +1,82 @@
+/*******************************************************************************\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.elements;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.AdaptionException;\r
+import org.simantics.db.exception.DoesNotContainValueException;\r
+import org.simantics.db.exception.NoSingleResultException;\r
+import org.simantics.db.exception.ServiceException;\r
+import org.simantics.db.exception.ValidationException;\r
+import org.simantics.diagram.G2DUtils;\r
+import org.simantics.g2d.element.ElementHints;\r
+import org.simantics.g2d.element.ElementHints.Properties;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.g2d.elementclass.PlainElementPropertySetter;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
+\r
+public class ElementPropertySetter extends PlainElementPropertySetter {\r
+\r
+    private static final long serialVersionUID = 712071316674596405L;\r
+\r
+    public static final ElementPropertySetter INSTANCE = new ElementPropertySetter(ElementHints.KEY_SG_NODE);\r
+\r
+    public ElementPropertySetter(Key key) {\r
+        super(key);\r
+    }\r
+\r
+    /**\r
+     * Load default properties from graph. This is called by class factory, hence not defined in the interface\r
+     * \r
+     * @param element\r
+     * @param r\r
+     * @param g\r
+     */\r
+    public void loadProperties(IElement element, Resource r, ReadGraph g) {\r
+        Properties properties = new Properties();\r
+        Layer0 L0 = Layer0.getInstance(g);\r
+        try {\r
+            // This way asserts do work:\r
+            for (Resource predicate : g.getPredicates(r)) {\r
+                if (g.isSubrelationOf(predicate, L0.HasProperty)) {\r
+                    try {\r
+                        String name = g.adapt(predicate, String.class);\r
+                        Resource o = g.getPossibleObject(r, predicate);\r
+                        if(o != null) { // FIXME: there should always be an object..\r
+                            Object val = G2DUtils.getObject(g, o);\r
+//                            System.out.println(name+" = "+val);\r
+                            properties.put(name, val);\r
+                        }\r
+                    } catch (AdaptionException e) {\r
+                        // TODO Auto-generated catch block\r
+                        e.printStackTrace();\r
+                    } catch (ValidationException e) {\r
+                        // TODO Auto-generated catch block\r
+                        e.printStackTrace();\r
+                    } catch (NoSingleResultException e) {\r
+                        // TODO Auto-generated catch block\r
+                        e.printStackTrace();\r
+                    } catch (DoesNotContainValueException e) {\r
+                        // TODO Auto-generated catch block\r
+                        e.printStackTrace();\r
+                    }\r
+                }\r
+            }\r
+        } catch (ServiceException e) {\r
+            // TODO Auto-generated catch block\r
+            e.printStackTrace();\r
+        }\r
+        element.setHint(ElementHints.KEY_ELEMENT_PROPERTIES, properties);\r
+    }\r
+}\r