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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.g2d.element.impl;
\r
14 import java.util.Map;
\r
16 import org.simantics.g2d.diagram.IDiagram;
\r
17 import org.simantics.g2d.element.ElementClass;
\r
18 import org.simantics.g2d.element.ElementHints;
\r
19 import org.simantics.g2d.element.IElement;
\r
20 import org.simantics.g2d.element.handler.ElementAdapter;
\r
21 import org.simantics.g2d.element.handler.LifeCycle;
\r
22 import org.simantics.utils.datastructures.hints.HintContext;
\r
25 * @author Toni Kalajainen
\r
27 public class Element extends HintContext implements IElement {
\r
30 * Spawn brand new element to the world.
\r
31 * The element is initialized.
\r
36 public static IElement clone(IElement cloneFrom)
\r
38 IElement e = new Element(cloneFrom.getElementClass());
\r
39 e.setHints(cloneFrom.getHints());
\r
44 * Spawn brand new element to the world.
\r
45 * The element is initialized.
\r
50 public static IElement spawnNew(ElementClass clazz)
\r
52 assert(clazz!=null);
\r
53 IElement e = new Element(clazz);
\r
54 Element.fireCreated(e);
\r
59 * Instantiate old element back into the world. Initialization is
\r
63 * @param oldValues old values of the element (or null)
\r
66 public static IElement instantiate(ElementClass clazz, Map<Key, Object> oldValues)
\r
68 IElement e = new Element(clazz);
\r
69 if (oldValues!=null)
\r
70 e.setHints(oldValues);
\r
74 private ElementClass clazz;
\r
75 private IDiagram diagram;
\r
77 public Element(ElementClass clazz)
\r
79 assert(clazz!=null);
\r
84 public IDiagram getDiagram() {
\r
85 assert(diagram!=null);
\r
90 public IDiagram peekDiagram() {
\r
95 public ElementClass getElementClass() {
\r
100 * This is an internal mechanism to be used only when in complete
\r
101 * comprehension of the effects. Care must be taken to install the right
\r
102 * hints when setting a new element class.
\r
107 public void setElementClass(ElementClass newClass) {
\r
108 if (newClass == null)
\r
109 throw new IllegalArgumentException("null element class");
\r
114 public String toString() {
\r
115 String text = getHint(ElementHints.KEY_TEXT);
\r
116 if (text==null) text = "";
\r
117 Object dada = getHint(ElementHints.KEY_OBJECT);
\r
118 if (dada==null) dada = "";
\r
119 String clazzId = clazz.getId();
\r
120 String clazzStr = clazzId != null && !clazzId.isEmpty() ? clazzId
\r
121 : (clazz.getClass().getSimpleName() + "@" + clazz.hashCode());
\r
122 return clazzStr + "[" + super.toString() + " " + text + " " + dada + "]";
\r
126 public void addedToDiagram(IDiagram diagram) {
\r
127 this.diagram = diagram;
\r
131 public void destroy() {
\r
132 Element.fireDestroyed(this);
\r
135 public static void fireDestroyed(IElement e)
\r
137 for (LifeCycle lc : e.getElementClass().getItemsByClass(LifeCycle.class))
\r
138 lc.onElementDestroyed(e);
\r
141 public static void fireCreated(IElement e)
\r
143 for (LifeCycle lc : e.getElementClass().getItemsByClass(LifeCycle.class))
\r
144 lc.onElementCreated(e);
\r
148 public void dispose() {
\r
149 clearWithoutNotification();
\r
152 public <T> T adapt(Class<T> adapter) {
\r
153 for (ElementAdapter ea : clazz.getItemsByClass(ElementAdapter.class))
\r
155 T result = ea.adapt(this, adapter);
\r
156 if (result!=null) return result;
\r