--- /dev/null
+/*******************************************************************************\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;\r
+\r
+import java.util.Collection;\r
+\r
+import org.simantics.g2d.diagram.IDiagram;\r
+import org.simantics.utils.ObjectUtils;\r
+\r
+/**\r
+ * A diagram handler for managing and querying arbitrary relationships between\r
+ * diagram objects. The management is done by claiming and denying relationships\r
+ * about the objects. A single relationship can only exist once or not at all\r
+ * between two objects, i.e. no duplicate relationships can be added.\r
+ * \r
+ * <p>\r
+ * There is no pre-defined meaning for a relationship - it is completely\r
+ * customizable by implementing your own {@link Relationship} interface.\r
+ * </p>\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public interface RelationshipHandler extends DiagramHandler {\r
+\r
+ /**\r
+ * A simple holder class for a single relationship. Holds all the\r
+ * ingredients: two elements and the role of the relationship. The\r
+ * relationship is either uni- or bi-directional.\r
+ */\r
+ public final class Relation {\r
+ private final Object subject;\r
+ private final Relationship relationship;\r
+ private final Object object;\r
+\r
+ public Relation(Object subject, Relationship relationship, Object object) {\r
+ assert subject != null;\r
+ assert relationship != null;\r
+ assert object != null;\r
+ this.subject = subject;\r
+ this.relationship = relationship;\r
+ this.object = object;\r
+ }\r
+\r
+ public Object getSubject() {\r
+ return subject;\r
+ }\r
+\r
+ public Relationship getRelationship() {\r
+ return relationship;\r
+ }\r
+\r
+ public Object getObject() {\r
+ return object;\r
+ }\r
+\r
+ @Override\r
+ public int hashCode() {\r
+ return ((ObjectUtils.hashCode(subject) * 31)\r
+ + ObjectUtils.hashCode(object) * 31)\r
+ + ObjectUtils.hashCode(relationship);\r
+ }\r
+\r
+ @Override\r
+ public boolean equals(Object obj) {\r
+ if (obj == this)\r
+ return true;\r
+ if (!(obj instanceof Relation))\r
+ return false;\r
+ Relation other = (Relation) obj;\r
+ return subject.equals(other.subject) && object.equals(other.object) && relationship.equals(other.relationship);\r
+ }\r
+\r
+ @Override\r
+ public String toString() {\r
+ return "(" + subject + ", " + relationship + ", " + object + ")";\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Claim a single relationship between the specified subject and object. An\r
+ * inverse relation will also be claimed if one exists.\r
+ * \r
+ * Subject and object should be alive (not destroyed). However it should not\r
+ * be enforced to be activated within the specified diagram, at the moment\r
+ * of invocation. This is because the relationship may be denied while the\r
+ * elements are still in the process of being added to or removed from a\r
+ * diagram.\r
+ * \r
+ * @param diagram the diagram to operate on\r
+ * @param subject the subject of the relationship\r
+ * @param predicate the relationship itself\r
+ * @param object the object of the relationship\r
+ */\r
+ void claim(IDiagram diagram, Object subject, Relationship predicate, Object object);\r
+\r
+ /**\r
+ * Deny a relationship. This will also deny the inverse relationship if one\r
+ * exists.\r
+ * \r
+ * Subject and object should be alive (not destroyed). However it should not\r
+ * be enforced to be activated within the specified diagram, at the moment\r
+ * of invocation. This is because the relationship may be denied while the\r
+ * elements are still in the process of being added to or removed from a\r
+ * diagram.\r
+ */\r
+ void deny(IDiagram diagram, Object subject, Relationship predicate, Object object);\r
+ void deny(IDiagram diagram, Relation relation);\r
+ void denyAll(IDiagram diagram, Object element);\r
+\r
+ /**\r
+ * Get all the relationships that are recorded for the specified element.\r
+ * \r
+ * @param diagram the diagram to operate on\r
+ * @param element the element to get all relations for\r
+ * @param result a collection for gathering the result of the query or null\r
+ * to create a new collection for the result.\r
+ * @return\r
+ */\r
+ Collection<Relation> getRelations(IDiagram diagram, Object element, Collection<Relation> result);\r
+\r
+}\r