]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/element/impl/Element.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / element / impl / Element.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.g2d.element.impl;\r
13 \r
14 import java.util.Map;\r
15 \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
23 \r
24 /**\r
25  * @author Toni Kalajainen\r
26  */\r
27 public class Element extends HintContext implements IElement {\r
28 \r
29     /**\r
30      * Spawn brand new element to the world.\r
31      * The element is initialized.\r
32      * \r
33      * @param clazz\r
34      * @return\r
35      */\r
36     public static IElement clone(IElement cloneFrom)\r
37     {\r
38         IElement e = new Element(cloneFrom.getElementClass());\r
39         e.setHints(cloneFrom.getHints());\r
40         return e;\r
41     }\r
42 \r
43     /**\r
44      * Spawn brand new element to the world.\r
45      * The element is initialized.\r
46      * \r
47      * @param clazz\r
48      * @return\r
49      */\r
50     public static IElement spawnNew(ElementClass clazz)\r
51     {\r
52         assert(clazz!=null);\r
53         IElement e = new Element(clazz);\r
54         Element.fireCreated(e);\r
55         return e;\r
56     }\r
57 \r
58     /**\r
59      * Instantiate old element back into the world. Initialization is\r
60      * bypassed.\r
61      * \r
62      * @param clazz\r
63      * @param oldValues old values of the element (or null)\r
64      * @return\r
65      */\r
66     public static IElement instantiate(ElementClass clazz, Map<Key, Object> oldValues)\r
67     {\r
68         IElement e = new Element(clazz);\r
69         if (oldValues!=null)\r
70             e.setHints(oldValues);\r
71         return e;\r
72     }\r
73 \r
74     private ElementClass clazz;\r
75     private IDiagram diagram;\r
76 \r
77     public Element(ElementClass clazz)\r
78     {\r
79         assert(clazz!=null);\r
80         this.clazz = clazz;\r
81     }\r
82 \r
83     @Override\r
84     public IDiagram getDiagram() {\r
85         assert(diagram!=null);\r
86         return diagram;\r
87     }\r
88 \r
89     @Override\r
90     public IDiagram peekDiagram() {\r
91         return diagram;\r
92     }\r
93 \r
94     @Override\r
95     public ElementClass getElementClass() {\r
96         return clazz;\r
97     }\r
98 \r
99     /**\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
103      * \r
104      * @param newClass\r
105      * @param newHints\r
106      */\r
107     public void setElementClass(ElementClass newClass) {\r
108         if (newClass == null)\r
109             throw new IllegalArgumentException("null element class");\r
110         clazz = newClass;\r
111     }\r
112 \r
113     @Override\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
123     }\r
124 \r
125     @Override\r
126     public void addedToDiagram(IDiagram diagram) {\r
127         this.diagram = diagram;\r
128     }\r
129 \r
130     @Override\r
131     public void destroy() {\r
132         Element.fireDestroyed(this);\r
133     }\r
134 \r
135     public static void fireDestroyed(IElement e)\r
136     {\r
137         for (LifeCycle lc : e.getElementClass().getItemsByClass(LifeCycle.class))\r
138             lc.onElementDestroyed(e);\r
139     }\r
140 \r
141     public static void fireCreated(IElement e)\r
142     {\r
143         for (LifeCycle lc : e.getElementClass().getItemsByClass(LifeCycle.class))\r
144             lc.onElementCreated(e);\r
145     }\r
146 \r
147     @Override\r
148     public void dispose() {\r
149         clearWithoutNotification();\r
150     }\r
151 \r
152     public <T> T adapt(Class<T> adapter) {\r
153         for (ElementAdapter ea : clazz.getItemsByClass(ElementAdapter.class))\r
154         {\r
155             T result = ea.adapt(this, adapter);\r
156             if (result!=null) return result;\r
157         }\r
158         return null;\r
159     }\r
160 \r
161 }\r