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