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