]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.tests/src/org/simantics/modeling/tests/commands/Connect.java
Small but effective HiDPI fixes for platform G2D
[simantics/platform.git] / bundles / org.simantics.modeling.tests / src / org / simantics / modeling / tests / commands / Connect.java
1 package org.simantics.modeling.tests.commands;
2
3 import java.awt.geom.Point2D;
4 import java.util.Arrays;
5 import java.util.Collections;
6
7 import junit.framework.Assert;
8
9 import org.simantics.db.Resource;
10 import org.simantics.db.WriteGraph;
11 import org.simantics.db.common.utils.OrderedSetUtils;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.diagram.content.ConnectionUtil;
14 import org.simantics.diagram.query.DiagramRequests;
15 import org.simantics.diagram.synchronization.graph.AddElement;
16 import org.simantics.layer0.Layer0;
17 import org.simantics.modeling.tests.cases.ModelingCommandSequenceTest;
18 import org.simantics.modeling.tests.traits.SingleResourceTrait;
19 import org.simantics.modeling.tests.traits.SingleResourceTraitImpl;
20 import org.simantics.structural.stubs.StructuralResource2;
21 import org.simantics.structural2.modelingRules.CPTerminal;
22 import org.simantics.structural2.modelingRules.ConnectionJudgement;
23 import org.simantics.structural2.modelingRules.IConnectionPoint;
24 import org.simantics.structural2.modelingRules.IModelingRules;
25
26
27 public class Connect extends ResourceWriteCommand<ModelingCommandSequenceTest> implements SingleResourceTrait {
28
29         private SingleResourceTrait diagram;
30         private SingleResourceTrait m1;
31         private SingleResourceTrait m2;
32         private SingleResourceTrait c1;
33         private SingleResourceTrait c2;
34         
35         public Connect(SingleResourceTrait diagram, SingleResourceTrait m1, SingleResourceTrait c1, SingleResourceTrait m2, SingleResourceTrait c2) {
36                 this.diagram = diagram;
37                 this.m1 = m1;
38                 this.c1 = c1;
39                 this.m2 = m2;
40                 this.c2 = c2;
41         }
42         
43         public Connect(SingleResourceTrait diagram, SingleResourceTrait m1, SingleResourceTrait c1, SingleResourceTrait m2, Resource c2) {
44                 this.diagram = diagram;
45                 this.m1 = m1;
46                 this.c1 = c1;
47                 this.m2 = m2;
48                 this.c2 = new SingleResourceTraitImpl(c2);
49         }
50
51         public Connect(SingleResourceTrait diagram, SingleResourceTrait m1, Resource c1, SingleResourceTrait m2, SingleResourceTrait c2) {
52                 this.diagram = diagram;
53                 this.m1 = m1;
54                 this.c1 = new SingleResourceTraitImpl(c1);
55                 this.m2 = m2;
56                 this.c2 = c2;
57         }
58         
59         public Connect(SingleResourceTrait diagram, SingleResourceTrait m1, Resource c1, SingleResourceTrait m2, Resource c2) {
60                 this.diagram = diagram;
61                 this.m1 = m1;
62                 this.c1 = new SingleResourceTraitImpl(c1);
63                 this.m2 = m2;
64                 this.c2 = new SingleResourceTraitImpl(c2);
65         }
66
67         @Override
68         protected Resource run(WriteGraph graph, ModelingCommandSequenceTest environment) throws DatabaseException {
69         
70                 ConnectionUtil cu = new ConnectionUtil(graph);
71                 Resource conn = cu.createConnectionWithCorners(graph, m1.getResource(graph), c1.getResource(graph), m2.getResource(graph), c2.getResource(graph), Collections.<Point2D>emptyList());
72                 
73                 Layer0 L0 = Layer0.getInstance(graph);
74                 StructuralResource2 STR = StructuralResource2.getInstance(graph);
75         graph.claim(conn, L0.PartOf, diagram.getResource());
76
77                 AddElement.claimFreshElementName(graph, diagram.getResource(), conn);
78         OrderedSetUtils.addFirst(graph, diagram.getResource(), conn);
79
80         IModelingRules modelingRules = graph.syncRequest(DiagramRequests.getModelingRules(diagram.getResource(), null));
81
82         IConnectionPoint cp1 = new CPTerminal(m1.getResource(graph), c1.getResource(graph));
83         IConnectionPoint cp2 = new CPTerminal(m2.getResource(graph), c2.getResource(graph));
84        
85         ConnectionJudgement judgement = modelingRules.judgeConnection(graph, Arrays.asList(cp1, cp2));
86         
87         Assert.assertNotNull(judgement.connectionType);
88         
89         graph.claim(conn, STR.HasConnectionType, null, judgement.connectionType);
90         
91         return conn;
92         
93         }
94         
95 }