]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/RemoveElement.java
Some enhancements to GraphLayer-related utilities for Diagram layers
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / synchronization / graph / RemoveElement.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.synchronization.graph;
13
14 import java.util.HashMap;
15
16 import org.simantics.databoard.Bindings;
17 import org.simantics.db.Resource;
18 import org.simantics.db.WriteGraph;
19 import org.simantics.db.common.CommentMetadata;
20 import org.simantics.db.common.request.IndexRoot;
21 import org.simantics.db.common.utils.OrderedSetUtils;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.layer0.adapter.Remover;
24 import org.simantics.db.layer0.exception.CannotRemoveException;
25 import org.simantics.db.layer0.util.RemoverUtil;
26 import org.simantics.diagram.content.ConnectionUtil;
27 import org.simantics.diagram.stubs.DiagramResource;
28 import org.simantics.diagram.synchronization.ModificationAdapter;
29 import org.simantics.layer0.Layer0;
30 import org.simantics.scl.commands.Commands;
31
32 /**
33  * @author Tuukka Lehtonen
34  */
35 public class RemoveElement extends ModificationAdapter {
36
37     Resource diagram;
38     Resource removedElement;
39
40     public RemoveElement(Resource diagram, Resource removed) {
41         super(REMOVE_NODE_PRIORITY);
42
43         if(diagram == null)
44             throw new NullPointerException();
45         if(removed == null)
46             throw new NullPointerException();
47         
48         this.diagram = diagram;
49         this.removedElement = removed;
50     }
51
52     @Override
53     public void perform(WriteGraph g) throws DatabaseException {
54         Commands.get(g, "Simantics/Diagram/removeElement")
55                 .execute(g, g.syncRequest(new IndexRoot(diagram)), diagram, removedElement);
56     }
57     
58     public static void removeElement(WriteGraph g, Resource diagram, Resource removedElement) throws DatabaseException {
59         boolean connection = g.isInstanceOf(removedElement, DiagramResource.getInstance(g).Connection);
60         String elementName = "";
61         // Remove element from all layers if possible.
62         DiagramResource DIA = DiagramResource.getInstance(g);
63         g.deny(removedElement, DIA.IsVisible);
64         g.deny(removedElement, DIA.IsFocusable);
65
66         if (connection) {
67             ConnectionUtil cu = new ConnectionUtil(g);
68             cu.removeConnection(removedElement);
69         } else {
70             Remover r = RemoverUtil.getPossibleRemover(g, removedElement);
71             if (r != null) {
72                 String problem = r.canRemove(g, new HashMap<Object, Object>(4));
73                 if (problem != null) {
74                     throw new CannotRemoveException(problem);
75                 }
76             }
77
78             // Perform custom removals through ElementWriter adaption.
79             // FIXME: should be able to use getSingleType if everything else goes correctly??
80             Resource elementType = g.getPossibleType(removedElement, DiagramResource.getInstance(g).Element);
81             if (elementType != null) {
82                 ElementWriter writer = g.adapt(elementType, ElementWriter.class);
83                 writer.removeFromGraph(g, removedElement);
84             }
85
86             // Remove element from diagram
87             OrderedSetUtils.remove(g, diagram, removedElement);
88             elementName = g.getPossibleRelatedValue2(removedElement, Layer0.getInstance(g).HasName, Bindings.STRING);
89             RemoverUtil.remove(g, removedElement);
90         }
91
92         // Add comment to change set.
93         CommentMetadata cm = g.getMetadata(CommentMetadata.class);
94         g.addMetadata(cm.add("Removed element " + elementName + " " + removedElement));
95     }
96 }