1 package org.simantics.g2d.element.impl;
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;
14 * @author Tuukka Lehtonen
16 public class MutatedElement extends HintStack implements IElement {
20 ElementClass origClass;
21 HintContext mutations;
23 public MutatedElement(IElement orig) {
25 throw new IllegalArgumentException("null arg");
27 this.origClass = orig.getElementClass();
28 this.addHintContext(orig, 0);
32 public void addedToDiagram(IDiagram ctx) {
37 public IDiagram getDiagram() {
38 assert diagram != null;
43 public IDiagram peekDiagram() {
48 public ElementClass getElementClass() {
53 public synchronized void clearWithoutNotification() {
54 if (mutations != null)
55 mutations.clearWithoutNotification();
59 public synchronized <E> E removeHint(Key key) {
61 throw new IllegalArgumentException("key is null");
62 if (mutations == null)
64 E result = mutations.removeHint(key);
65 if (mutations.size() == 0) {
66 removeHintContext(mutations);
73 public synchronized void setHint(Key key, Object value) {
75 throw new IllegalArgumentException("key is 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);
84 mutations.setHint(key, value);
88 public synchronized void setHints(Map<Key, Object> hints) {
89 if (mutations == null) {
90 mutations = new HintContext();
91 addHintContext(mutations, 10);
93 mutations.setHints(hints);
97 public void destroy() {
98 for (LifeCycle lc : getElementClass().getItemsByClass(LifeCycle.class))
99 lc.onElementDestroyed(this);
104 public void dispose() {
105 if (mutations != null)
106 this.removeHintContext(mutations);
107 this.removeHintContext(orig);
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);
124 public int hashCode() {
125 return orig != null ? orig.hashCode() : 0;
129 public boolean equals(Object obj) {
132 return orig == null ? false : orig.equals(obj);
136 public String toString() {
137 return "MutatedElement[" + orig + " " + origClass + "]";