1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.flag;
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.diagram.stubs.DiagramResource;
18 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
19 import org.simantics.g2d.elementclass.FlagClass;
20 import org.simantics.g2d.elementclass.FlagClass.Mode;
21 import org.simantics.g2d.elementclass.FlagClass.Type;
22 import org.simantics.structural2.modelingRules.IModelingRules;
25 * @author Tuukka Lehtonen
27 public abstract class AbstractFlagType implements IFlagType {
29 protected final Resource flag;
30 protected final IModelingRules modelingRules;
32 public AbstractFlagType(Resource flag, IModelingRules modelingRules) {
34 throw new NullPointerException("null flag");
35 if (modelingRules == null)
36 throw new NullPointerException("null modeling rules");
38 this.modelingRules = modelingRules;
41 protected Type getType(ReadGraph graph) throws DatabaseException {
42 DiagramResource dr = DiagramResource.getInstance(graph);
43 FlagClass.Type t = DiagramGraphUtil.toFlagType(dr, graph.getPossibleObject(flag, dr.HasFlagType));
47 protected Mode getMode(ReadGraph graph) throws DatabaseException {
48 return getMode(graph, flag);
51 public static Mode getMode(ReadGraph graph, Resource flag) throws DatabaseException {
52 DiagramResource DIA = DiagramResource.getInstance(graph);
53 int joinCount = graph.getObjects(flag, DIA.FlagIsJoinedBy).size();
55 return FlagClass.Mode.Internal;
56 else if(joinCount == 1) {
57 for (Resource connectionJoin : graph.getObjects(flag, DIA.FlagIsJoinedBy))
58 for (Resource otherFlag : graph.getObjects(connectionJoin, DIA.JoinsFlag))
59 if (!flag.equals(otherFlag)
60 && !DiagramGraphUtil.onSameDiagram(graph, flag, otherFlag))
61 return FlagClass.Mode.External;
62 return FlagClass.Mode.Internal;
65 return new FlagClass.External(joinCount);