X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.g2d%2Fsrc%2Forg%2Fsimantics%2Fg2d%2Felement%2Fimpl%2FElement.java;h=d8d31ec6d89f7bd225dfa3eec8b79576dd98a691;hb=a43df9e895f022132f9b94dfedeae78fb4eed712;hp=e1b54d84e12d3f8e7a532a23a248413ce859ebcb;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/element/impl/Element.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/element/impl/Element.java index e1b54d84e..d8d31ec6d 100644 --- a/bundles/org.simantics.g2d/src/org/simantics/g2d/element/impl/Element.java +++ b/bundles/org.simantics.g2d/src/org/simantics/g2d/element/impl/Element.java @@ -1,161 +1,161 @@ -/******************************************************************************* - * Copyright (c) 2007, 2010 Association for Decentralized Information Management - * in Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -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.ElementHints; -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; - -/** - * @author Toni Kalajainen - */ -public class Element extends HintContext implements IElement { - - /** - * Spawn brand new element to the world. - * The element is initialized. - * - * @param clazz - * @return - */ - public static IElement clone(IElement cloneFrom) - { - IElement e = new Element(cloneFrom.getElementClass()); - e.setHints(cloneFrom.getHints()); - return e; - } - - /** - * Spawn brand new element to the world. - * The element is initialized. - * - * @param clazz - * @return - */ - public static IElement spawnNew(ElementClass clazz) - { - assert(clazz!=null); - IElement e = new Element(clazz); - Element.fireCreated(e); - return e; - } - - /** - * Instantiate old element back into the world. Initialization is - * bypassed. - * - * @param clazz - * @param oldValues old values of the element (or null) - * @return - */ - public static IElement instantiate(ElementClass clazz, Map oldValues) - { - IElement e = new Element(clazz); - if (oldValues!=null) - e.setHints(oldValues); - return e; - } - - private ElementClass clazz; - private IDiagram diagram; - - public Element(ElementClass clazz) - { - assert(clazz!=null); - this.clazz = clazz; - } - - @Override - public IDiagram getDiagram() { - assert(diagram!=null); - return diagram; - } - - @Override - public IDiagram peekDiagram() { - return diagram; - } - - @Override - public ElementClass getElementClass() { - return clazz; - } - - /** - * This is an internal mechanism to be used only when in complete - * comprehension of the effects. Care must be taken to install the right - * hints when setting a new element class. - * - * @param newClass - * @param newHints - */ - public void setElementClass(ElementClass newClass) { - if (newClass == null) - throw new IllegalArgumentException("null element class"); - clazz = newClass; - } - - @Override - public String toString() { - String text = getHint(ElementHints.KEY_TEXT); - if (text==null) text = ""; - Object dada = getHint(ElementHints.KEY_OBJECT); - if (dada==null) dada = ""; - String clazzId = clazz.getId(); - String clazzStr = clazzId != null && !clazzId.isEmpty() ? clazzId - : (clazz.getClass().getSimpleName() + "@" + clazz.hashCode()); - return clazzStr + "[" + super.toString() + " " + text + " " + dada + "]"; - } - - @Override - public void addedToDiagram(IDiagram diagram) { - this.diagram = diagram; - } - - @Override - public void destroy() { - Element.fireDestroyed(this); - } - - public static void fireDestroyed(IElement e) - { - for (LifeCycle lc : e.getElementClass().getItemsByClass(LifeCycle.class)) - lc.onElementDestroyed(e); - } - - public static void fireCreated(IElement e) - { - for (LifeCycle lc : e.getElementClass().getItemsByClass(LifeCycle.class)) - lc.onElementCreated(e); - } - - @Override - public void dispose() { - clearWithoutNotification(); - } - - public T adapt(Class adapter) { - for (ElementAdapter ea : clazz.getItemsByClass(ElementAdapter.class)) - { - T result = ea.adapt(this, adapter); - if (result!=null) return result; - } - return null; - } - -} +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +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.ElementHints; +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; + +/** + * @author Toni Kalajainen + */ +public class Element extends HintContext implements IElement { + + /** + * Spawn brand new element to the world. + * The element is initialized. + * + * @param clazz + * @return + */ + public static IElement clone(IElement cloneFrom) + { + IElement e = new Element(cloneFrom.getElementClass()); + e.setHints(cloneFrom.getHints()); + return e; + } + + /** + * Spawn brand new element to the world. + * The element is initialized. + * + * @param clazz + * @return + */ + public static IElement spawnNew(ElementClass clazz) + { + assert(clazz!=null); + IElement e = new Element(clazz); + Element.fireCreated(e); + return e; + } + + /** + * Instantiate old element back into the world. Initialization is + * bypassed. + * + * @param clazz + * @param oldValues old values of the element (or null) + * @return + */ + public static IElement instantiate(ElementClass clazz, Map oldValues) + { + IElement e = new Element(clazz); + if (oldValues!=null) + e.setHints(oldValues); + return e; + } + + private ElementClass clazz; + private IDiagram diagram; + + public Element(ElementClass clazz) + { + assert(clazz!=null); + this.clazz = clazz; + } + + @Override + public IDiagram getDiagram() { + assert(diagram!=null); + return diagram; + } + + @Override + public IDiagram peekDiagram() { + return diagram; + } + + @Override + public ElementClass getElementClass() { + return clazz; + } + + /** + * This is an internal mechanism to be used only when in complete + * comprehension of the effects. Care must be taken to install the right + * hints when setting a new element class. + * + * @param newClass + * @param newHints + */ + public void setElementClass(ElementClass newClass) { + if (newClass == null) + throw new IllegalArgumentException("null element class"); + clazz = newClass; + } + + @Override + public String toString() { + String text = getHint(ElementHints.KEY_TEXT); + if (text==null) text = ""; + Object dada = getHint(ElementHints.KEY_OBJECT); + if (dada==null) dada = ""; + String clazzId = clazz.getId(); + String clazzStr = clazzId != null && !clazzId.isEmpty() ? clazzId + : (clazz.getClass().getSimpleName() + "@" + clazz.hashCode()); + return clazzStr + "[" + super.toString() + " " + text + " " + dada + "]"; + } + + @Override + public void addedToDiagram(IDiagram diagram) { + this.diagram = diagram; + } + + @Override + public void destroy() { + Element.fireDestroyed(this); + } + + public static void fireDestroyed(IElement e) + { + for (LifeCycle lc : e.getElementClass().getItemsByClass(LifeCycle.class)) + lc.onElementDestroyed(e); + } + + public static void fireCreated(IElement e) + { + for (LifeCycle lc : e.getElementClass().getItemsByClass(LifeCycle.class)) + lc.onElementCreated(e); + } + + @Override + public void dispose() { + clearWithoutNotification(); + } + + public T adapt(Class adapter) { + for (ElementAdapter ea : clazz.getItemsByClass(ElementAdapter.class)) + { + T result = ea.adapt(this, adapter); + if (result!=null) return result; + } + return null; + } + +}