-/*******************************************************************************\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.diagram.connection;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Arrays;\r
-\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.RequestProcessor;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.Session;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.request.Read;\r
-import org.simantics.diagram.content.ConnectionUtil;\r
-import org.simantics.g2d.connection.IConnectionAdvisor;\r
-import org.simantics.g2d.diagram.handler.Topology.Terminal;\r
-import org.simantics.g2d.element.ElementUtils;\r
-import org.simantics.g2d.element.IElement;\r
-import org.simantics.g2d.elementclass.FlagHandler;\r
-import org.simantics.structural2.modelingRules.CPIgnore;\r
-import org.simantics.structural2.modelingRules.ConnectionJudgement;\r
-import org.simantics.structural2.modelingRules.ConnectionJudgementType;\r
-import org.simantics.structural2.modelingRules.IConnectionPoint;\r
-import org.simantics.structural2.modelingRules.IModelingRules;\r
-\r
-/**\r
- * To customize, prefer overriding\r
- * {@link #canBeConnected(ReadGraph, IElement, Terminal, IElement, Terminal)}\r
- * and {@link #canBeginConnection(ReadGraph, IElement, Terminal)}.\r
- * \r
- * @author Hannu Niemistö\r
- */\r
-public class ModelledConnectionAdvisor implements IConnectionAdvisor {\r
-\r
- protected IModelingRules modelingRules;\r
- protected Session session;\r
-\r
- /**\r
- * @param modelingRules\r
- * @param session\r
- */\r
- public ModelledConnectionAdvisor(IModelingRules modelingRules,\r
- Session session) {\r
- this.modelingRules = modelingRules;\r
- this.session = session;\r
- }\r
-\r
- protected IConnectionPoint getConnectionPoint(ReadGraph g, IElement element, Terminal term) throws DatabaseException {\r
- return getConnectionPoint(g, element, term, false);\r
- }\r
-\r
- protected IConnectionPoint getConnectionPoint(ReadGraph g, IElement element, Terminal term, boolean ignoreUnrecognized) throws DatabaseException {\r
- Object obj = null;\r
- if (element != null)\r
- obj = ElementUtils.getObject(element);\r
-\r
- if (obj instanceof Resource) {\r
- Resource elementResource = (Resource) obj;\r
- return ConnectionUtil.toConnectionPoint(g, elementResource, term);\r
- }\r
-\r
- // FIXME: this currently allows connections to begin from flags\r
- // but is rather hackish.\r
- if (element.getElementClass().containsClass(FlagHandler.class)) {\r
- return CPIgnore.NULL_INSTANCE;\r
- }\r
-\r
- if (ignoreUnrecognized)\r
- return CPIgnore.NULL_INSTANCE;\r
-\r
- throw new IllegalArgumentException("Cannot get IConnectionPoint for (element,terminal) pair:\n\t" + element + "\n\t" + term);\r
- }\r
-\r
- @Override\r
- public Object canBeConnected(Object backend,\r
- final IElement element1, final Terminal term1,\r
- final IElement element2, final Terminal term2) {\r
- try {\r
- if (backend == null)\r
- backend = session;\r
- return ((RequestProcessor)backend).syncRequest(new Read<Object>() {\r
- @Override\r
- public Object perform(ReadGraph g) throws DatabaseException {\r
- return canBeConnected(g, element1, term1, element2, term2);\r
- }\r
- });\r
- } catch(DatabaseException e) {\r
- e.printStackTrace();\r
- return null;\r
- }\r
- }\r
-\r
- @Override\r
- public boolean canBeginConnection(Object backend,\r
- final IElement element, final Terminal term) {\r
- try {\r
- if (backend == null)\r
- backend = session;\r
- return ((RequestProcessor)backend).syncRequest(new Read<Boolean>() {\r
- @Override\r
- public Boolean perform(ReadGraph g) throws DatabaseException {\r
- return canBeginConnection(g, element, term);\r
- }\r
- });\r
- } catch (DatabaseException e) {\r
- e.printStackTrace();\r
- return false;\r
- }\r
- }\r
-\r
- protected Object canBeConnected(ReadGraph graph,\r
- IElement element1, Terminal term1,\r
- IElement element2, Terminal term2) throws DatabaseException {\r
- ArrayList<IConnectionPoint> cps = new ArrayList<IConnectionPoint>(2);\r
- if (element1 != null)\r
- cps.add(getConnectionPoint(graph, element1, term1, true));\r
- if (element2 != null)\r
- cps.add(getConnectionPoint(graph, element2, term2, true));\r
-\r
- ConnectionJudgement judgement = modelingRules.judgeConnection(graph, cps);\r
- if (judgement.type == ConnectionJudgementType.LEGAL\r
- // #6751, Apros #12404: a connection should not be automatically\r
- // denied just because it is not perfectly legal. Further legality\r
- // should be decided on the client side even though the IConnectionAdvisor\r
- // interface is not documented to return ConnectionJudgement instances,\r
- // because the interface knows nothing about this class.\r
- || judgement.type == ConnectionJudgementType.CANBEMADELEGAL) \r
- return judgement;\r
- return null;\r
- }\r
-\r
- protected boolean canBeginConnection(ReadGraph graph,\r
- IElement element, Terminal term) throws DatabaseException {\r
- return modelingRules.judgeConnection(graph,\r
- Arrays.asList(getConnectionPoint(graph, element, term)))\r
- .type != ConnectionJudgementType.ILLEGAL;\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.diagram.connection;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+
+import org.simantics.db.ReadGraph;
+import org.simantics.db.RequestProcessor;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.request.Read;
+import org.simantics.diagram.content.ConnectionUtil;
+import org.simantics.g2d.connection.IConnectionAdvisor;
+import org.simantics.g2d.diagram.handler.Topology.Terminal;
+import org.simantics.g2d.element.ElementUtils;
+import org.simantics.g2d.element.IElement;
+import org.simantics.g2d.elementclass.FlagHandler;
+import org.simantics.structural2.modelingRules.CPIgnore;
+import org.simantics.structural2.modelingRules.ConnectionJudgement;
+import org.simantics.structural2.modelingRules.ConnectionJudgementType;
+import org.simantics.structural2.modelingRules.IConnectionPoint;
+import org.simantics.structural2.modelingRules.IModelingRules;
+
+/**
+ * To customize, prefer overriding
+ * {@link #canBeConnected(ReadGraph, IElement, Terminal, IElement, Terminal)}
+ * and {@link #canBeginConnection(ReadGraph, IElement, Terminal)}.
+ *
+ * @author Hannu Niemistö
+ */
+public class ModelledConnectionAdvisor implements IConnectionAdvisor {
+
+ protected IModelingRules modelingRules;
+ protected Session session;
+
+ /**
+ * @param modelingRules
+ * @param session
+ */
+ public ModelledConnectionAdvisor(IModelingRules modelingRules,
+ Session session) {
+ this.modelingRules = modelingRules;
+ this.session = session;
+ }
+
+ protected IConnectionPoint getConnectionPoint(ReadGraph g, IElement element, Terminal term) throws DatabaseException {
+ return getConnectionPoint(g, element, term, false);
+ }
+
+ protected IConnectionPoint getConnectionPoint(ReadGraph g, IElement element, Terminal term, boolean ignoreUnrecognized) throws DatabaseException {
+ Object obj = null;
+ if (element != null)
+ obj = ElementUtils.getObject(element);
+
+ if (obj instanceof Resource) {
+ Resource elementResource = (Resource) obj;
+ return ConnectionUtil.toConnectionPoint(g, elementResource, term);
+ }
+
+ // FIXME: this currently allows connections to begin from flags
+ // but is rather hackish.
+ if (element.getElementClass().containsClass(FlagHandler.class)) {
+ return CPIgnore.NULL_INSTANCE;
+ }
+
+ if (ignoreUnrecognized)
+ return CPIgnore.NULL_INSTANCE;
+
+ throw new IllegalArgumentException("Cannot get IConnectionPoint for (element,terminal) pair:\n\t" + element + "\n\t" + term);
+ }
+
+ @Override
+ public Object canBeConnected(Object backend,
+ final IElement element1, final Terminal term1,
+ final IElement element2, final Terminal term2) {
+ try {
+ if (backend == null)
+ backend = session;
+ return ((RequestProcessor)backend).syncRequest(new Read<Object>() {
+ @Override
+ public Object perform(ReadGraph g) throws DatabaseException {
+ return canBeConnected(g, element1, term1, element2, term2);
+ }
+ });
+ } catch(DatabaseException e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+ @Override
+ public boolean canBeginConnection(Object backend,
+ final IElement element, final Terminal term) {
+ try {
+ if (backend == null)
+ backend = session;
+ return ((RequestProcessor)backend).syncRequest(new Read<Boolean>() {
+ @Override
+ public Boolean perform(ReadGraph g) throws DatabaseException {
+ return canBeginConnection(g, element, term);
+ }
+ });
+ } catch (DatabaseException e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+
+ protected Object canBeConnected(ReadGraph graph,
+ IElement element1, Terminal term1,
+ IElement element2, Terminal term2) throws DatabaseException {
+ ArrayList<IConnectionPoint> cps = new ArrayList<IConnectionPoint>(2);
+ if (element1 != null)
+ cps.add(getConnectionPoint(graph, element1, term1, true));
+ if (element2 != null)
+ cps.add(getConnectionPoint(graph, element2, term2, true));
+
+ ConnectionJudgement judgement = modelingRules.judgeConnection(graph, cps);
+ if (judgement.type == ConnectionJudgementType.LEGAL
+ // #6751, Apros #12404: a connection should not be automatically
+ // denied just because it is not perfectly legal. Further legality
+ // should be decided on the client side even though the IConnectionAdvisor
+ // interface is not documented to return ConnectionJudgement instances,
+ // because the interface knows nothing about this class.
+ || judgement.type == ConnectionJudgementType.CANBEMADELEGAL)
+ return judgement;
+ return null;
+ }
+
+ protected boolean canBeginConnection(ReadGraph graph,
+ IElement element, Terminal term) throws DatabaseException {
+ return modelingRules.judgeConnection(graph,
+ Arrays.asList(getConnectionPoint(graph, element, term)))
+ .type != ConnectionJudgementType.ILLEGAL;
+ }
+
+}