]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/FlagRemover.java
WIP: Some old-ish adapter registry initialization improvements
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / adapters / FlagRemover.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.Collection;
15 import java.util.Collections;
16 import java.util.Map;
17
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.diagram.flag.FlagUtil;
23 import org.simantics.diagram.stubs.DiagramResource;
24 import org.simantics.modeling.adapters.Removers.ValidationResult;
25
26 /**
27  * @author Tuukka Lehtonen
28  */
29 public class FlagRemover extends ElementRemover {
30
31     public FlagRemover(Resource flag) {
32         super(flag);
33     }
34
35     @Override
36     public String canRemove(ReadGraph graph, Map<Object, Object> aux) throws DatabaseException {
37         boolean lifted = FlagUtil.isLifted(graph, resource);
38         if (lifted) {
39             ValidationResult result = Removers.validateFlagRemoval(graph, resource);
40             if (result.inUse()) {
41                 return Removers.formatError(graph, result);
42             }
43         }
44         return null;
45     }
46
47     @Override
48     public void remove(WriteGraph graph) throws DatabaseException {
49         FlagUtil.disconnectFlag(graph, resource);
50
51         DiagramResource DIA = DiagramResource.getInstance(graph);
52         Collection<Resource> connectionRelations = Collections.emptySet();
53         if (FlagUtil.isLifted(graph, resource))
54             connectionRelations = graph.getObjects(resource, DIA.IsLiftedAs);
55
56         removeElement(graph);
57
58         if (!connectionRelations.isEmpty()) {
59             for (Resource connectionRelation : connectionRelations) {
60                 new ConnectionRelationRemover(connectionRelation).remove(graph);
61             }
62         }
63     }
64
65 }