]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/element/impl/MutatedElement.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / element / impl / MutatedElement.java
1 package org.simantics.g2d.element.impl;\r
2 \r
3 import java.util.Map;\r
4 \r
5 import org.simantics.g2d.diagram.IDiagram;\r
6 import org.simantics.g2d.element.ElementClass;\r
7 import org.simantics.g2d.element.IElement;\r
8 import org.simantics.g2d.element.handler.ElementAdapter;\r
9 import org.simantics.g2d.element.handler.LifeCycle;\r
10 import org.simantics.utils.datastructures.hints.HintContext;\r
11 import org.simantics.utils.datastructures.hints.HintStack;\r
12 \r
13 /**\r
14  * @author Tuukka Lehtonen\r
15  */\r
16 public class MutatedElement extends HintStack implements IElement {\r
17 \r
18     IDiagram     diagram;\r
19     IElement     orig;\r
20     ElementClass origClass;\r
21     HintContext  mutations;\r
22 \r
23     public MutatedElement(IElement orig) {\r
24         if (orig == null)\r
25             throw new IllegalArgumentException("null arg");\r
26         this.orig = orig;\r
27         this.origClass = orig.getElementClass();\r
28         this.addHintContext(orig, 0);\r
29     }\r
30 \r
31     @Override\r
32     public void addedToDiagram(IDiagram ctx) {\r
33         this.diagram = ctx;\r
34     }\r
35 \r
36     @Override\r
37     public IDiagram getDiagram() {\r
38         assert diagram != null;\r
39         return diagram;\r
40     }\r
41 \r
42     @Override\r
43     public IDiagram peekDiagram() {\r
44         return diagram;\r
45     }\r
46 \r
47     @Override\r
48     public ElementClass getElementClass() {\r
49         return origClass;\r
50     }\r
51 \r
52     @Override\r
53     public synchronized void clearWithoutNotification() {\r
54         if (mutations != null)\r
55             mutations.clearWithoutNotification();\r
56     }\r
57 \r
58     @Override\r
59     public synchronized <E> E removeHint(Key key) {\r
60         if (key == null)\r
61             throw new IllegalArgumentException("key is null");\r
62         if (mutations == null)\r
63             return null;\r
64         E result = mutations.removeHint(key);\r
65         if (mutations.size() == 0) {\r
66             removeHintContext(mutations);\r
67             mutations = null;\r
68         }\r
69         return result;\r
70     }\r
71 \r
72     @Override\r
73     public synchronized void setHint(Key key, Object value) {\r
74         if (key == null)\r
75             throw new IllegalArgumentException("key is null");\r
76         if (value == null)\r
77             throw new IllegalArgumentException("value is null");\r
78         if (!key.isValueAccepted(value))\r
79             throw new RuntimeException("Value \"" + value + "\" is not accepted with key " + key.getClass().getName());\r
80         if (mutations == null) {\r
81             mutations = new HintContext();\r
82             addHintContext(mutations, 10);\r
83         }\r
84         mutations.setHint(key, value);\r
85     }\r
86 \r
87     @Override\r
88     public synchronized void setHints(Map<Key, Object> hints) {\r
89         if (mutations == null) {\r
90             mutations = new HintContext();\r
91             addHintContext(mutations, 10);\r
92         }\r
93         mutations.setHints(hints);\r
94     }\r
95 \r
96     @Override\r
97     public void destroy() {\r
98         for (LifeCycle lc : getElementClass().getItemsByClass(LifeCycle.class))\r
99             lc.onElementDestroyed(this);\r
100         dispose();\r
101     }\r
102 \r
103     @Override\r
104     public void dispose() {\r
105         if (mutations != null)\r
106             this.removeHintContext(mutations);\r
107         this.removeHintContext(orig);\r
108         orig = null;\r
109         origClass = null;\r
110         mutations = null;\r
111     }\r
112 \r
113     @SuppressWarnings({ "rawtypes", "unchecked" })\r
114     public Object getAdapter(Class adapter) {\r
115         for (ElementAdapter ea : getElementClass().getItemsByClass(ElementAdapter.class)) {\r
116             Object result = ea.adapt(this, adapter);\r
117             if (result != null)\r
118                 return result;\r
119         }\r
120         return null;\r
121     }\r
122 \r
123     @Override\r
124     public int hashCode() {\r
125         return orig != null ? orig.hashCode() : 0;\r
126     }\r
127 \r
128     @Override\r
129     public boolean equals(Object obj) {\r
130         if (this == obj)\r
131             return true;\r
132         return orig == null ? false : orig.equals(obj);\r
133     }\r
134 \r
135     @Override\r
136     public String toString() {\r
137         return "MutatedElement[" + orig + " " + origClass + "]";\r
138     }\r
139 \r
140 }\r