]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/element/impl/Element.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / element / impl / Element.java
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
new file mode 100644 (file)
index 0000000..e1b54d8
--- /dev/null
@@ -0,0 +1,161 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.g2d.element.impl;\r
+\r
+import java.util.Map;\r
+\r
+import org.simantics.g2d.diagram.IDiagram;\r
+import org.simantics.g2d.element.ElementClass;\r
+import org.simantics.g2d.element.ElementHints;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.g2d.element.handler.ElementAdapter;\r
+import org.simantics.g2d.element.handler.LifeCycle;\r
+import org.simantics.utils.datastructures.hints.HintContext;\r
+\r
+/**\r
+ * @author Toni Kalajainen\r
+ */\r
+public class Element extends HintContext implements IElement {\r
+\r
+    /**\r
+     * Spawn brand new element to the world.\r
+     * The element is initialized.\r
+     * \r
+     * @param clazz\r
+     * @return\r
+     */\r
+    public static IElement clone(IElement cloneFrom)\r
+    {\r
+        IElement e = new Element(cloneFrom.getElementClass());\r
+        e.setHints(cloneFrom.getHints());\r
+        return e;\r
+    }\r
+\r
+    /**\r
+     * Spawn brand new element to the world.\r
+     * The element is initialized.\r
+     * \r
+     * @param clazz\r
+     * @return\r
+     */\r
+    public static IElement spawnNew(ElementClass clazz)\r
+    {\r
+        assert(clazz!=null);\r
+        IElement e = new Element(clazz);\r
+        Element.fireCreated(e);\r
+        return e;\r
+    }\r
+\r
+    /**\r
+     * Instantiate old element back into the world. Initialization is\r
+     * bypassed.\r
+     * \r
+     * @param clazz\r
+     * @param oldValues old values of the element (or null)\r
+     * @return\r
+     */\r
+    public static IElement instantiate(ElementClass clazz, Map<Key, Object> oldValues)\r
+    {\r
+        IElement e = new Element(clazz);\r
+        if (oldValues!=null)\r
+            e.setHints(oldValues);\r
+        return e;\r
+    }\r
+\r
+    private ElementClass clazz;\r
+    private IDiagram diagram;\r
+\r
+    public Element(ElementClass clazz)\r
+    {\r
+        assert(clazz!=null);\r
+        this.clazz = clazz;\r
+    }\r
+\r
+    @Override\r
+    public IDiagram getDiagram() {\r
+        assert(diagram!=null);\r
+        return diagram;\r
+    }\r
+\r
+    @Override\r
+    public IDiagram peekDiagram() {\r
+        return diagram;\r
+    }\r
+\r
+    @Override\r
+    public ElementClass getElementClass() {\r
+        return clazz;\r
+    }\r
+\r
+    /**\r
+     * This is an internal mechanism to be used only when in complete\r
+     * comprehension of the effects. Care must be taken to install the right\r
+     * hints when setting a new element class.\r
+     * \r
+     * @param newClass\r
+     * @param newHints\r
+     */\r
+    public void setElementClass(ElementClass newClass) {\r
+        if (newClass == null)\r
+            throw new IllegalArgumentException("null element class");\r
+        clazz = newClass;\r
+    }\r
+\r
+    @Override\r
+    public String toString() {\r
+        String text = getHint(ElementHints.KEY_TEXT);\r
+        if (text==null) text = "";\r
+        Object dada = getHint(ElementHints.KEY_OBJECT);\r
+        if (dada==null) dada = "";\r
+        String clazzId = clazz.getId();\r
+        String clazzStr = clazzId != null && !clazzId.isEmpty() ? clazzId\r
+                : (clazz.getClass().getSimpleName() + "@" + clazz.hashCode());\r
+        return clazzStr + "[" + super.toString() + " " + text + " " + dada + "]";\r
+    }\r
+\r
+    @Override\r
+    public void addedToDiagram(IDiagram diagram) {\r
+        this.diagram = diagram;\r
+    }\r
+\r
+    @Override\r
+    public void destroy() {\r
+        Element.fireDestroyed(this);\r
+    }\r
+\r
+    public static void fireDestroyed(IElement e)\r
+    {\r
+        for (LifeCycle lc : e.getElementClass().getItemsByClass(LifeCycle.class))\r
+            lc.onElementDestroyed(e);\r
+    }\r
+\r
+    public static void fireCreated(IElement e)\r
+    {\r
+        for (LifeCycle lc : e.getElementClass().getItemsByClass(LifeCycle.class))\r
+            lc.onElementCreated(e);\r
+    }\r
+\r
+    @Override\r
+    public void dispose() {\r
+        clearWithoutNotification();\r
+    }\r
+\r
+    public <T> T adapt(Class<T> adapter) {\r
+        for (ElementAdapter ea : clazz.getItemsByClass(ElementAdapter.class))\r
+        {\r
+            T result = ea.adapt(this, adapter);\r
+            if (result!=null) return result;\r
+        }\r
+        return null;\r
+    }\r
+\r
+}\r