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