]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
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.elements;\r
13 \r
14 import org.simantics.db.ReadGraph;\r
15 import org.simantics.db.Resource;\r
16 import org.simantics.db.exception.AdaptionException;\r
17 import org.simantics.db.exception.DoesNotContainValueException;\r
18 import org.simantics.db.exception.NoSingleResultException;\r
19 import org.simantics.db.exception.ServiceException;\r
20 import org.simantics.db.exception.ValidationException;\r
21 import org.simantics.diagram.G2DUtils;\r
22 import org.simantics.g2d.element.ElementHints;\r
23 import org.simantics.g2d.element.ElementHints.Properties;\r
24 import org.simantics.g2d.element.IElement;\r
25 import org.simantics.g2d.elementclass.PlainElementPropertySetter;\r
26 import org.simantics.layer0.Layer0;\r
27 import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
28 \r
29 public class ElementPropertySetter extends PlainElementPropertySetter {\r
30 \r
31     private static final long serialVersionUID = 712071316674596405L;\r
32 \r
33     public static final ElementPropertySetter INSTANCE = new ElementPropertySetter(ElementHints.KEY_SG_NODE);\r
34 \r
35     public ElementPropertySetter(Key key) {\r
36         super(key);\r
37     }\r
38 \r
39     /**\r
40      * Load default properties from graph. This is called by class factory, hence not defined in the interface\r
41      * \r
42      * @param element\r
43      * @param r\r
44      * @param g\r
45      */\r
46     public void loadProperties(IElement element, Resource r, ReadGraph g) {\r
47         Properties properties = new Properties();\r
48         Layer0 L0 = Layer0.getInstance(g);\r
49         try {\r
50             // This way asserts do work:\r
51             for (Resource predicate : g.getPredicates(r)) {\r
52                 if (g.isSubrelationOf(predicate, L0.HasProperty)) {\r
53                     try {\r
54                         String name = g.adapt(predicate, String.class);\r
55                         Resource o = g.getPossibleObject(r, predicate);\r
56                         if(o != null) { // FIXME: there should always be an object..\r
57                             Object val = G2DUtils.getObject(g, o);\r
58 //                            System.out.println(name+" = "+val);\r
59                             properties.put(name, val);\r
60                         }\r
61                     } catch (AdaptionException e) {\r
62                         // TODO Auto-generated catch block\r
63                         e.printStackTrace();\r
64                     } catch (ValidationException e) {\r
65                         // TODO Auto-generated catch block\r
66                         e.printStackTrace();\r
67                     } catch (NoSingleResultException e) {\r
68                         // TODO Auto-generated catch block\r
69                         e.printStackTrace();\r
70                     } catch (DoesNotContainValueException e) {\r
71                         // TODO Auto-generated catch block\r
72                         e.printStackTrace();\r
73                     }\r
74                 }\r
75             }\r
76         } catch (ServiceException e) {\r
77             // TODO Auto-generated catch block\r
78             e.printStackTrace();\r
79         }\r
80         element.setHint(ElementHints.KEY_ELEMENT_PROPERTIES, properties);\r
81     }\r
82 }\r