]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/element/impl/Element.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / element / impl / Element.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.g2d.element.impl;
13
14 import java.util.Map;
15
16 import org.simantics.g2d.diagram.IDiagram;
17 import org.simantics.g2d.element.ElementClass;
18 import org.simantics.g2d.element.ElementHints;
19 import org.simantics.g2d.element.IElement;
20 import org.simantics.g2d.element.handler.ElementAdapter;
21 import org.simantics.g2d.element.handler.LifeCycle;
22 import org.simantics.utils.datastructures.hints.HintContext;
23
24 /**
25  * @author Toni Kalajainen
26  */
27 public class Element extends HintContext implements IElement {
28
29     /**
30      * Spawn brand new element to the world.
31      * The element is initialized.
32      * 
33      * @param clazz
34      * @return
35      */
36     public static IElement clone(IElement cloneFrom)
37     {
38         IElement e = new Element(cloneFrom.getElementClass());
39         e.setHints(cloneFrom.getHints());
40         return e;
41     }
42
43     /**
44      * Spawn brand new element to the world.
45      * The element is initialized.
46      * 
47      * @param clazz
48      * @return
49      */
50     public static IElement spawnNew(ElementClass clazz)
51     {
52         assert(clazz!=null);
53         IElement e = new Element(clazz);
54         Element.fireCreated(e);
55         return e;
56     }
57
58     /**
59      * Instantiate old element back into the world. Initialization is
60      * bypassed.
61      * 
62      * @param clazz
63      * @param oldValues old values of the element (or null)
64      * @return
65      */
66     public static IElement instantiate(ElementClass clazz, Map<Key, Object> oldValues)
67     {
68         IElement e = new Element(clazz);
69         if (oldValues!=null)
70             e.setHints(oldValues);
71         return e;
72     }
73
74     private ElementClass clazz;
75     private IDiagram diagram;
76
77     public Element(ElementClass clazz)
78     {
79         assert(clazz!=null);
80         this.clazz = clazz;
81     }
82
83     @Override
84     public IDiagram getDiagram() {
85         assert(diagram!=null);
86         return diagram;
87     }
88
89     @Override
90     public IDiagram peekDiagram() {
91         return diagram;
92     }
93
94     @Override
95     public ElementClass getElementClass() {
96         return clazz;
97     }
98
99     /**
100      * This is an internal mechanism to be used only when in complete
101      * comprehension of the effects. Care must be taken to install the right
102      * hints when setting a new element class.
103      * 
104      * @param newClass
105      * @param newHints
106      */
107     public void setElementClass(ElementClass newClass) {
108         if (newClass == null)
109             throw new IllegalArgumentException("null element class");
110         clazz = newClass;
111     }
112
113     @Override
114     public String toString() {
115         String text = getHint(ElementHints.KEY_TEXT);
116         if (text==null) text = "";
117         Object dada = getHint(ElementHints.KEY_OBJECT);
118         if (dada==null) dada = "";
119         String clazzId = clazz.getId();
120         String clazzStr = clazzId != null && !clazzId.isEmpty() ? clazzId
121                 : (clazz.getClass().getSimpleName() + "@" + clazz.hashCode());
122         return clazzStr + "[" + super.toString() + " " + text + " " + dada + "]";
123     }
124
125     @Override
126     public void addedToDiagram(IDiagram diagram) {
127         this.diagram = diagram;
128     }
129
130     @Override
131     public void destroy() {
132         Element.fireDestroyed(this);
133     }
134
135     public static void fireDestroyed(IElement e)
136     {
137         for (LifeCycle lc : e.getElementClass().getItemsByClass(LifeCycle.class))
138             lc.onElementDestroyed(e);
139     }
140
141     public static void fireCreated(IElement e)
142     {
143         for (LifeCycle lc : e.getElementClass().getItemsByClass(LifeCycle.class))
144             lc.onElementCreated(e);
145     }
146
147     @Override
148     public void dispose() {
149         clearWithoutNotification();
150     }
151
152     public <T> T adapt(Class<T> adapter) {
153         for (ElementAdapter ea : clazz.getItemsByClass(ElementAdapter.class))
154         {
155             T result = ea.adapt(this, adapter);
156             if (result!=null) return result;
157         }
158         return null;
159     }
160
161 }