X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.g2d%2Fsrc%2Forg%2Fsimantics%2Fg2d%2Felement%2Fimpl%2FMutatedElement.java;h=d68579673835e3c87752abc322595ce072595672;hp=57ecf936df4d677d54f537f980f70e9aed7aa4bb;hb=refs%2Fchanges%2F38%2F238%2F2;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/element/impl/MutatedElement.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/element/impl/MutatedElement.java index 57ecf936d..d68579673 100644 --- a/bundles/org.simantics.g2d/src/org/simantics/g2d/element/impl/MutatedElement.java +++ b/bundles/org.simantics.g2d/src/org/simantics/g2d/element/impl/MutatedElement.java @@ -1,140 +1,140 @@ -package org.simantics.g2d.element.impl; - -import java.util.Map; - -import org.simantics.g2d.diagram.IDiagram; -import org.simantics.g2d.element.ElementClass; -import org.simantics.g2d.element.IElement; -import org.simantics.g2d.element.handler.ElementAdapter; -import org.simantics.g2d.element.handler.LifeCycle; -import org.simantics.utils.datastructures.hints.HintContext; -import org.simantics.utils.datastructures.hints.HintStack; - -/** - * @author Tuukka Lehtonen - */ -public class MutatedElement extends HintStack implements IElement { - - IDiagram diagram; - IElement orig; - ElementClass origClass; - HintContext mutations; - - public MutatedElement(IElement orig) { - if (orig == null) - throw new IllegalArgumentException("null arg"); - this.orig = orig; - this.origClass = orig.getElementClass(); - this.addHintContext(orig, 0); - } - - @Override - public void addedToDiagram(IDiagram ctx) { - this.diagram = ctx; - } - - @Override - public IDiagram getDiagram() { - assert diagram != null; - return diagram; - } - - @Override - public IDiagram peekDiagram() { - return diagram; - } - - @Override - public ElementClass getElementClass() { - return origClass; - } - - @Override - public synchronized void clearWithoutNotification() { - if (mutations != null) - mutations.clearWithoutNotification(); - } - - @Override - public synchronized E removeHint(Key key) { - if (key == null) - throw new IllegalArgumentException("key is null"); - if (mutations == null) - return null; - E result = mutations.removeHint(key); - if (mutations.size() == 0) { - removeHintContext(mutations); - mutations = null; - } - return result; - } - - @Override - public synchronized void setHint(Key key, Object value) { - if (key == null) - throw new IllegalArgumentException("key is null"); - if (value == null) - throw new IllegalArgumentException("value is null"); - if (!key.isValueAccepted(value)) - throw new RuntimeException("Value \"" + value + "\" is not accepted with key " + key.getClass().getName()); - if (mutations == null) { - mutations = new HintContext(); - addHintContext(mutations, 10); - } - mutations.setHint(key, value); - } - - @Override - public synchronized void setHints(Map hints) { - if (mutations == null) { - mutations = new HintContext(); - addHintContext(mutations, 10); - } - mutations.setHints(hints); - } - - @Override - public void destroy() { - for (LifeCycle lc : getElementClass().getItemsByClass(LifeCycle.class)) - lc.onElementDestroyed(this); - dispose(); - } - - @Override - public void dispose() { - if (mutations != null) - this.removeHintContext(mutations); - this.removeHintContext(orig); - orig = null; - origClass = null; - mutations = null; - } - - @SuppressWarnings({ "rawtypes", "unchecked" }) - public Object getAdapter(Class adapter) { - for (ElementAdapter ea : getElementClass().getItemsByClass(ElementAdapter.class)) { - Object result = ea.adapt(this, adapter); - if (result != null) - return result; - } - return null; - } - - @Override - public int hashCode() { - return orig != null ? orig.hashCode() : 0; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - return orig == null ? false : orig.equals(obj); - } - - @Override - public String toString() { - return "MutatedElement[" + orig + " " + origClass + "]"; - } - -} +package org.simantics.g2d.element.impl; + +import java.util.Map; + +import org.simantics.g2d.diagram.IDiagram; +import org.simantics.g2d.element.ElementClass; +import org.simantics.g2d.element.IElement; +import org.simantics.g2d.element.handler.ElementAdapter; +import org.simantics.g2d.element.handler.LifeCycle; +import org.simantics.utils.datastructures.hints.HintContext; +import org.simantics.utils.datastructures.hints.HintStack; + +/** + * @author Tuukka Lehtonen + */ +public class MutatedElement extends HintStack implements IElement { + + IDiagram diagram; + IElement orig; + ElementClass origClass; + HintContext mutations; + + public MutatedElement(IElement orig) { + if (orig == null) + throw new IllegalArgumentException("null arg"); + this.orig = orig; + this.origClass = orig.getElementClass(); + this.addHintContext(orig, 0); + } + + @Override + public void addedToDiagram(IDiagram ctx) { + this.diagram = ctx; + } + + @Override + public IDiagram getDiagram() { + assert diagram != null; + return diagram; + } + + @Override + public IDiagram peekDiagram() { + return diagram; + } + + @Override + public ElementClass getElementClass() { + return origClass; + } + + @Override + public synchronized void clearWithoutNotification() { + if (mutations != null) + mutations.clearWithoutNotification(); + } + + @Override + public synchronized E removeHint(Key key) { + if (key == null) + throw new IllegalArgumentException("key is null"); + if (mutations == null) + return null; + E result = mutations.removeHint(key); + if (mutations.size() == 0) { + removeHintContext(mutations); + mutations = null; + } + return result; + } + + @Override + public synchronized void setHint(Key key, Object value) { + if (key == null) + throw new IllegalArgumentException("key is null"); + if (value == null) + throw new IllegalArgumentException("value is null"); + if (!key.isValueAccepted(value)) + throw new RuntimeException("Value \"" + value + "\" is not accepted with key " + key.getClass().getName()); + if (mutations == null) { + mutations = new HintContext(); + addHintContext(mutations, 10); + } + mutations.setHint(key, value); + } + + @Override + public synchronized void setHints(Map hints) { + if (mutations == null) { + mutations = new HintContext(); + addHintContext(mutations, 10); + } + mutations.setHints(hints); + } + + @Override + public void destroy() { + for (LifeCycle lc : getElementClass().getItemsByClass(LifeCycle.class)) + lc.onElementDestroyed(this); + dispose(); + } + + @Override + public void dispose() { + if (mutations != null) + this.removeHintContext(mutations); + this.removeHintContext(orig); + orig = null; + origClass = null; + mutations = null; + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + public Object getAdapter(Class adapter) { + for (ElementAdapter ea : getElementClass().getItemsByClass(ElementAdapter.class)) { + Object result = ea.adapt(this, adapter); + if (result != null) + return result; + } + return null; + } + + @Override + public int hashCode() { + return orig != null ? orig.hashCode() : 0; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + return orig == null ? false : orig.equals(obj); + } + + @Override + public String toString() { + return "MutatedElement[" + orig + " " + origClass + "]"; + } + +}