-/*******************************************************************************\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.diagram.handler.impl;\r
-\r
-import java.util.Collection;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.Set;\r
-\r
-import org.simantics.g2d.connection.EndKeyOf;\r
-import org.simantics.g2d.connection.TerminalKeyOf;\r
-import org.simantics.g2d.diagram.IDiagram;\r
-import org.simantics.g2d.diagram.handler.ElementListener;\r
-import org.simantics.g2d.diagram.handler.SubstituteElement;\r
-import org.simantics.g2d.diagram.handler.Topology;\r
-import org.simantics.g2d.element.ElementHints;\r
-import org.simantics.g2d.element.ElementUtils;\r
-import org.simantics.g2d.element.IElement;\r
-import org.simantics.g2d.element.handler.EdgeVisuals.EdgeEnd;\r
-import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
-\r
-/**\r
- * \r
- * @author Toni Kalajainen\r
- */\r
-public class TopologyImpl implements Topology, ElementListener {\r
-\r
- @Override\r
- public Connection getConnection(IElement edge, EdgeEnd end)\r
- {\r
- Key key = EndKeyOf.get(end);\r
- Connection c = edge.getHint(key);\r
- if (c == null)\r
- return null;\r
- IDiagram d = ElementUtils.getDiagram(edge);\r
- c = _updateReferences(d, c);\r
- return c;\r
- }\r
-\r
- @Override\r
- public void getConnections(IElement node, Terminal terminal, Collection<Connection> connections)\r
- {\r
- // FIXME: only allows single connection / terminal\r
- Key key = new TerminalKeyOf(terminal, null, Connection.class);\r
- Connection c = node.getHint(key);\r
- if (c != null) {\r
- connections.add(c);\r
- }\r
- IDiagram d = ElementUtils.getDiagram(node);\r
- _updateReferences(d, c);\r
- }\r
-\r
- private Connection _updateReferences(IDiagram d, Connection c)\r
- {\r
- List<SubstituteElement> list = d.getDiagramClass().getItemsByClass(SubstituteElement.class);\r
- if (list.size()==0) return c;\r
-\r
- IElement e = c.edge;\r
- IElement n = c.node;\r
-\r
- for (SubstituteElement se : list)\r
- {\r
- IElement ne = se.substitute(d, e);\r
- IElement nn = se.substitute(d, n);\r
- if (ne != null)\r
- e = ne;\r
- if (nn != null)\r
- n = nn;\r
- }\r
-\r
- return new Connection(e, c.end, n, c.terminal);\r
- }\r
-\r
- @Override\r
- public void connect(IElement edge, EdgeEnd end, IElement node, Terminal terminal) {\r
- if (edge == null)\r
- throw new NullPointerException("null edge");\r
- if (end == null)\r
- throw new NullPointerException("null end");\r
- if (node == null)\r
- throw new NullPointerException("null node");\r
- if (terminal == null)\r
- throw new NullPointerException("null terminal");\r
-\r
- IDiagram d = ElementUtils.getDiagram(edge);\r
- assert( d == ElementUtils.getDiagram(node) );\r
-\r
- Connection c = new Connection(edge, end, node, terminal);\r
-\r
- // It is not the job of this code to check or resolve modelling issues\r
- // caused by this connection.\r
-\r
- // This makes it possible to have more than one connection to the same\r
- // terminal.\r
- Object edgeData = edge.getHint(ElementHints.KEY_OBJECT);\r
-\r
- TerminalKeyOf key = new TerminalKeyOf(terminal, edgeData, Connection.class);\r
- node.setHint(key, c);\r
- EndKeyOf key2 = EndKeyOf.get(end);\r
- edge.setHint(key2, c);\r
- }\r
-\r
- @Override\r
- public void disconnect(IElement edge, EdgeEnd end, IElement node, Terminal terminal) {\r
- if (edge == null)\r
- throw new NullPointerException("null edge");\r
- if (end == null)\r
- throw new NullPointerException("null end");\r
- if (node == null)\r
- throw new NullPointerException("null node");\r
- if (terminal == null)\r
- throw new NullPointerException("null terminal");\r
-\r
- IDiagram d = ElementUtils.getDiagram(edge);\r
- assert( d == ElementUtils.getDiagram(node) );\r
-\r
- EndKeyOf edgeKey = EndKeyOf.get(end);\r
- Connection c = edge.getHint(edgeKey);\r
- if (c == null)\r
- throw new UnsupportedOperationException("cannot disconnect, no Connection in edge " + edge);\r
-\r
- for (Map.Entry<TerminalKeyOf, Object> entry : node.getHintsOfClass(TerminalKeyOf.class).entrySet()) {\r
- Connection cc = (Connection) entry.getValue();\r
- if (c == cc) {\r
- node.removeHint(entry.getKey());\r
- edge.removeHint(edgeKey);\r
- return;\r
- }\r
- }\r
-\r
- throw new UnsupportedOperationException("cannot disconnect, no connection between found between edge "\r
- + edge + " and node " + node);\r
- }\r
-\r
- @Override\r
- public void onElementAdded(IDiagram d, IElement e) {\r
- }\r
-\r
- @Override\r
- public void onElementRemoved(IDiagram d, IElement e) {\r
- Set<TerminalKeyOf> keys1 = e.getHintsOfClass(TerminalKeyOf.class).keySet();\r
- for (TerminalKeyOf key : keys1)\r
- e.removeHint(key);\r
- for (EndKeyOf key : EndKeyOf.KEYS)\r
- e.removeHint(key);\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * 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.diagram.handler.impl;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.simantics.g2d.connection.EndKeyOf;
+import org.simantics.g2d.connection.TerminalKeyOf;
+import org.simantics.g2d.diagram.IDiagram;
+import org.simantics.g2d.diagram.handler.ElementListener;
+import org.simantics.g2d.diagram.handler.SubstituteElement;
+import org.simantics.g2d.diagram.handler.Topology;
+import org.simantics.g2d.element.ElementHints;
+import org.simantics.g2d.element.ElementUtils;
+import org.simantics.g2d.element.IElement;
+import org.simantics.g2d.element.handler.EdgeVisuals.EdgeEnd;
+import org.simantics.utils.datastructures.hints.IHintContext.Key;
+
+/**
+ *
+ * @author Toni Kalajainen
+ */
+public class TopologyImpl implements Topology, ElementListener {
+
+ @Override
+ public Connection getConnection(IElement edge, EdgeEnd end)
+ {
+ Key key = EndKeyOf.get(end);
+ Connection c = edge.getHint(key);
+ if (c == null)
+ return null;
+ IDiagram d = ElementUtils.getDiagram(edge);
+ c = _updateReferences(d, c);
+ return c;
+ }
+
+ @Override
+ public void getConnections(IElement node, Terminal terminal, Collection<Connection> connections)
+ {
+ // FIXME: only allows single connection / terminal
+ Key key = new TerminalKeyOf(terminal, null, Connection.class);
+ Connection c = node.getHint(key);
+ if (c != null) {
+ connections.add(c);
+ }
+ IDiagram d = ElementUtils.getDiagram(node);
+ _updateReferences(d, c);
+ }
+
+ private Connection _updateReferences(IDiagram d, Connection c)
+ {
+ List<SubstituteElement> list = d.getDiagramClass().getItemsByClass(SubstituteElement.class);
+ if (list.size()==0) return c;
+
+ IElement e = c.edge;
+ IElement n = c.node;
+
+ for (SubstituteElement se : list)
+ {
+ IElement ne = se.substitute(d, e);
+ IElement nn = se.substitute(d, n);
+ if (ne != null)
+ e = ne;
+ if (nn != null)
+ n = nn;
+ }
+
+ return new Connection(e, c.end, n, c.terminal);
+ }
+
+ @Override
+ public void connect(IElement edge, EdgeEnd end, IElement node, Terminal terminal) {
+ if (edge == null)
+ throw new NullPointerException("null edge");
+ if (end == null)
+ throw new NullPointerException("null end");
+ if (node == null)
+ throw new NullPointerException("null node");
+ if (terminal == null)
+ throw new NullPointerException("null terminal");
+
+ IDiagram d = ElementUtils.getDiagram(edge);
+ assert( d == ElementUtils.getDiagram(node) );
+
+ Connection c = new Connection(edge, end, node, terminal);
+
+ // It is not the job of this code to check or resolve modelling issues
+ // caused by this connection.
+
+ // This makes it possible to have more than one connection to the same
+ // terminal.
+ Object edgeData = edge.getHint(ElementHints.KEY_OBJECT);
+
+ TerminalKeyOf key = new TerminalKeyOf(terminal, edgeData, Connection.class);
+ node.setHint(key, c);
+ EndKeyOf key2 = EndKeyOf.get(end);
+ edge.setHint(key2, c);
+ }
+
+ @Override
+ public void disconnect(IElement edge, EdgeEnd end, IElement node, Terminal terminal) {
+ if (edge == null)
+ throw new NullPointerException("null edge");
+ if (end == null)
+ throw new NullPointerException("null end");
+ if (node == null)
+ throw new NullPointerException("null node");
+ if (terminal == null)
+ throw new NullPointerException("null terminal");
+
+ IDiagram d = ElementUtils.getDiagram(edge);
+ assert( d == ElementUtils.getDiagram(node) );
+
+ EndKeyOf edgeKey = EndKeyOf.get(end);
+ Connection c = edge.getHint(edgeKey);
+ if (c == null)
+ throw new UnsupportedOperationException("cannot disconnect, no Connection in edge " + edge);
+
+ for (Map.Entry<TerminalKeyOf, Object> entry : node.getHintsOfClass(TerminalKeyOf.class).entrySet()) {
+ Connection cc = (Connection) entry.getValue();
+ if (c == cc) {
+ node.removeHint(entry.getKey());
+ edge.removeHint(edgeKey);
+ return;
+ }
+ }
+
+ throw new UnsupportedOperationException("cannot disconnect, no connection between found between edge "
+ + edge + " and node " + node);
+ }
+
+ @Override
+ public void onElementAdded(IDiagram d, IElement e) {
+ }
+
+ @Override
+ public void onElementRemoved(IDiagram d, IElement e) {
+ Set<TerminalKeyOf> keys1 = e.getHintsOfClass(TerminalKeyOf.class).keySet();
+ for (TerminalKeyOf key : keys1)
+ e.removeHint(key);
+ for (EndKeyOf key : EndKeyOf.KEYS)
+ e.removeHint(key);
+ }
+
+}