--- /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.diagram.flag;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;\r
+import org.simantics.g2d.elementclass.FlagClass;\r
+import org.simantics.g2d.elementclass.FlagClass.Mode;\r
+import org.simantics.g2d.elementclass.FlagClass.Type;\r
+import org.simantics.structural2.modelingRules.IModelingRules;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public abstract class AbstractFlagType implements IFlagType {\r
+\r
+ protected final Resource flag;\r
+ protected final IModelingRules modelingRules;\r
+\r
+ public AbstractFlagType(Resource flag, IModelingRules modelingRules) {\r
+ if (flag == null)\r
+ throw new NullPointerException("null flag");\r
+ if (modelingRules == null)\r
+ throw new NullPointerException("null modeling rules");\r
+ this.flag = flag;\r
+ this.modelingRules = modelingRules;\r
+ }\r
+\r
+ protected Type getType(ReadGraph graph) throws DatabaseException {\r
+ DiagramResource dr = DiagramResource.getInstance(graph);\r
+ FlagClass.Type t = DiagramGraphUtil.toFlagType(dr, graph.getPossibleObject(flag, dr.HasFlagType));\r
+ return t;\r
+ }\r
+\r
+ protected Mode getMode(ReadGraph graph) throws DatabaseException {\r
+ return getMode(graph, flag);\r
+ }\r
+\r
+ public static Mode getMode(ReadGraph graph, Resource flag) throws DatabaseException {\r
+ int joinCount = graph.getObjects(flag, DiagramResource.getInstance(graph).FlagIsJoinedBy).size();\r
+ if(joinCount == 0)\r
+ return FlagClass.Mode.Internal;\r
+ else if(joinCount == 1) {\r
+ Resource otherFlag = FlagUtil.getPossibleCounterpart(graph, flag);\r
+ if(otherFlag == null /* FIXME just to get around npe */ || \r
+ DiagramGraphUtil.onSameDiagram(graph, flag, otherFlag))\r
+ return FlagClass.Mode.Internal;\r
+ else\r
+ return FlagClass.Mode.External;\r
+ }\r
+ else\r
+ return new FlagClass.External(joinCount);\r
+ }\r
+\r
+}
\ No newline at end of file