]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/flag/AbstractFlagType.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / flag / AbstractFlagType.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.flag;
13
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;
23
24 /**
25  * @author Tuukka Lehtonen
26  */
27 public abstract class AbstractFlagType implements IFlagType {
28
29     protected final Resource flag;
30     protected final IModelingRules modelingRules;
31
32     public AbstractFlagType(Resource flag, IModelingRules modelingRules) {
33         if (flag == null)
34             throw new NullPointerException("null flag");
35         if (modelingRules == null)
36             throw new NullPointerException("null modeling rules");
37         this.flag = flag;
38         this.modelingRules = modelingRules;
39     }
40
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));
44         return t;
45     }
46
47     protected Mode getMode(ReadGraph graph) throws DatabaseException {
48         return getMode(graph, flag);
49     }
50
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();
54         if(joinCount == 0)
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;
63         }
64         else
65             return new FlagClass.External(joinCount);
66     }
67
68 }