]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/TerminalRemover.java
Rename and move 'modeling.adapters.Removers' and make it API
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / adapters / TerminalRemover.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * 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.modeling.adapters;
13
14 import java.util.Map;
15
16 import org.simantics.db.ReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.WriteGraph;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.diagram.stubs.DiagramResource;
21 import org.simantics.layer0.Layer0;
22 import org.simantics.modeling.ModelingResources;
23 import org.simantics.modeling.utils.RemoverUtils;
24 import org.simantics.modeling.utils.RemoverUtils.ValidationResult;
25
26 /**
27  * A remover implementation for symbol terminals.
28  * 
29  * <p>
30  * Contrary to ConnectionRelationRemover, this remover will only remove the
31  * diagram side connection relations in addition to removing the terminal
32  * itself. Deleting a terminal from the symbol does not imply that the whole
33  * connection point needs to be removed from the component type configuration,
34  * only that the graphical representation of the connection point is removed.
35  * 
36  * @author Tuukka Lehtonen
37  * @see ConnectionRelationRemover
38  */
39 public class TerminalRemover extends ElementRemover {
40
41     public TerminalRemover(Resource terminal) {
42         super(terminal);
43     }
44
45     @Override
46     public String canRemove(ReadGraph graph, Map<Object, Object> aux) throws DatabaseException {
47         DiagramResource DIA = DiagramResource.getInstance(graph);
48         ModelingResources MOD = ModelingResources.getInstance(graph);
49
50         for (Resource diagramConnectionRelation : graph.getObjects(resource, DIA.HasConnectionPoint)) {
51             for (Resource connectionRelation : graph.getObjects(diagramConnectionRelation, MOD.DiagramConnectionRelationToConnectionRelation)) {
52                 ValidationResult result = RemoverUtils.validateConnectionRelationRemoval(graph, connectionRelation, diagramConnectionRelation);
53                 if (result.inUse())
54                     return RemoverUtils.formatError(graph, result);
55             }
56         }
57
58         return null;
59     }
60
61     @Override
62     public void remove(WriteGraph graph) throws DatabaseException {
63         Layer0 L0 = Layer0.getInstance(graph);
64         DiagramResource DIA = DiagramResource.getInstance(graph);
65
66         for (Resource diagramConnectionPoint : graph.getObjects(resource, DIA.HasConnectionPoint)) {
67             Resource diagramConnectionPointInverse = graph.getPossibleObject(diagramConnectionPoint, L0.InverseOf);
68             graph.deny(diagramConnectionPoint);
69             if (diagramConnectionPointInverse != null)
70                 graph.deny(diagramConnectionPointInverse);
71         }
72
73         removeElement(graph);
74     }
75
76 }