1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.elements;
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;
29 public class ElementPropertySetter extends PlainElementPropertySetter {
31 private static final long serialVersionUID = 712071316674596405L;
33 public static final ElementPropertySetter INSTANCE = new ElementPropertySetter(ElementHints.KEY_SG_NODE);
35 public ElementPropertySetter(Key key) {
40 * Load default properties from graph. This is called by class factory, hence not defined in the interface
46 public void loadProperties(IElement element, Resource r, ReadGraph g) {
47 Properties properties = new Properties();
48 Layer0 L0 = Layer0.getInstance(g);
50 // This way asserts do work:
51 for (Resource predicate : g.getPredicates(r)) {
52 if (g.isSubrelationOf(predicate, L0.HasProperty)) {
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);
61 } catch (AdaptionException e) {
62 // TODO Auto-generated catch block
64 } catch (ValidationException e) {
65 // TODO Auto-generated catch block
67 } catch (NoSingleResultException e) {
68 // TODO Auto-generated catch block
70 } catch (DoesNotContainValueException e) {
71 // TODO Auto-generated catch block
76 } catch (ServiceException e) {
77 // TODO Auto-generated catch block
80 element.setHint(ElementHints.KEY_ELEMENT_PROPERTIES, properties);