]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/elements/ElementPropertySetter.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / elements / ElementPropertySetter.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.elements;
13
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.exception.AdaptionException;
17 import org.simantics.db.exception.DoesNotContainValueException;
18 import org.simantics.db.exception.NoSingleResultException;
19 import org.simantics.db.exception.ServiceException;
20 import org.simantics.db.exception.ValidationException;
21 import org.simantics.diagram.G2DUtils;
22 import org.simantics.g2d.element.ElementHints;
23 import org.simantics.g2d.element.ElementHints.Properties;
24 import org.simantics.g2d.element.IElement;
25 import org.simantics.g2d.elementclass.PlainElementPropertySetter;
26 import org.simantics.layer0.Layer0;
27 import org.simantics.utils.datastructures.hints.IHintContext.Key;
28
29 public class ElementPropertySetter extends PlainElementPropertySetter {
30
31     private static final long serialVersionUID = 712071316674596405L;
32
33     public static final ElementPropertySetter INSTANCE = new ElementPropertySetter(ElementHints.KEY_SG_NODE);
34
35     public ElementPropertySetter(Key key) {
36         super(key);
37     }
38
39     /**
40      * Load default properties from graph. This is called by class factory, hence not defined in the interface
41      * 
42      * @param element
43      * @param r
44      * @param g
45      */
46     public void loadProperties(IElement element, Resource r, ReadGraph g) {
47         Properties properties = new Properties();
48         Layer0 L0 = Layer0.getInstance(g);
49         try {
50             // This way asserts do work:
51             for (Resource predicate : g.getPredicates(r)) {
52                 if (g.isSubrelationOf(predicate, L0.HasProperty)) {
53                     try {
54                         String name = g.adapt(predicate, String.class);
55                         Resource o = g.getPossibleObject(r, predicate);
56                         if(o != null) { // FIXME: there should always be an object..
57                             Object val = G2DUtils.getObject(g, o);
58 //                            System.out.println(name+" = "+val);
59                             properties.put(name, val);
60                         }
61                     } catch (AdaptionException e) {
62                         // TODO Auto-generated catch block
63                         e.printStackTrace();
64                     } catch (ValidationException e) {
65                         // TODO Auto-generated catch block
66                         e.printStackTrace();
67                     } catch (NoSingleResultException e) {
68                         // TODO Auto-generated catch block
69                         e.printStackTrace();
70                     } catch (DoesNotContainValueException e) {
71                         // TODO Auto-generated catch block
72                         e.printStackTrace();
73                     }
74                 }
75             }
76         } catch (ServiceException e) {
77             // TODO Auto-generated catch block
78             e.printStackTrace();
79         }
80         element.setHint(ElementHints.KEY_ELEMENT_PROPERTIES, properties);
81     }
82 }