]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/RemoveElement.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / synchronization / graph / RemoveElement.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.diagram.synchronization.graph;\r
13 \r
14 import java.util.HashMap;\r
15 \r
16 import org.simantics.databoard.Bindings;\r
17 import org.simantics.db.Resource;\r
18 import org.simantics.db.WriteGraph;\r
19 import org.simantics.db.common.CommentMetadata;\r
20 import org.simantics.db.common.request.IndexRoot;\r
21 import org.simantics.db.common.utils.OrderedSetUtils;\r
22 import org.simantics.db.exception.DatabaseException;\r
23 import org.simantics.db.layer0.adapter.Remover;\r
24 import org.simantics.db.layer0.exception.CannotRemoveException;\r
25 import org.simantics.db.layer0.util.RemoverUtil;\r
26 import org.simantics.diagram.content.ConnectionUtil;\r
27 import org.simantics.diagram.stubs.DiagramResource;\r
28 import org.simantics.diagram.synchronization.ModificationAdapter;\r
29 import org.simantics.layer0.Layer0;\r
30 import org.simantics.scl.commands.Commands;\r
31 \r
32 /**\r
33  * @author Tuukka Lehtonen\r
34  */\r
35 public class RemoveElement extends ModificationAdapter {\r
36 \r
37     Resource diagram;\r
38     Resource removedElement;\r
39 \r
40     public RemoveElement(Resource diagram, Resource removed) {\r
41         super(REMOVE_NODE_PRIORITY);\r
42 \r
43         if(diagram == null)\r
44             throw new NullPointerException();\r
45         if(removed == null)\r
46             throw new NullPointerException();\r
47         \r
48         this.diagram = diagram;\r
49         this.removedElement = removed;\r
50     }\r
51 \r
52     @Override\r
53     public void perform(WriteGraph g) throws DatabaseException {\r
54         Commands.get(g, "Simantics/Diagram/removeElement")\r
55                 .execute(g, g.syncRequest(new IndexRoot(diagram)), diagram, removedElement);\r
56     }\r
57     \r
58     public static void removeElement(WriteGraph g, Resource diagram, Resource removedElement) throws DatabaseException {\r
59         boolean connection = g.isInstanceOf(removedElement, DiagramResource.getInstance(g).Connection);\r
60         String elementName = "";\r
61         // Remove element from all layers if possible.\r
62         DiagramResource DIA = DiagramResource.getInstance(g);\r
63         g.deny(removedElement, DIA.IsVisible);\r
64         g.deny(removedElement, DIA.IsFocusable);\r
65 \r
66         if (connection) {\r
67             ConnectionUtil cu = new ConnectionUtil(g);\r
68             cu.removeConnection(removedElement);\r
69         } else {\r
70             Remover r = RemoverUtil.getPossibleRemover(g, removedElement);\r
71             if (r != null) {\r
72                 String problem = r.canRemove(g, new HashMap<Object, Object>(4));\r
73                 if (problem != null) {\r
74                     throw new CannotRemoveException(problem);\r
75                 }\r
76             }\r
77 \r
78             // Perform custom removals through ElementWriter adaption.\r
79             // FIXME: should be able to use getSingleType if everything else goes correctly??\r
80             Resource elementType = g.getPossibleType(removedElement, DiagramResource.getInstance(g).Element);\r
81             if (elementType != null) {\r
82                 ElementWriter writer = g.adapt(elementType, ElementWriter.class);\r
83                 writer.removeFromGraph(g, removedElement);\r
84             }\r
85 \r
86             // Remove element from diagram\r
87             OrderedSetUtils.remove(g, diagram, removedElement);\r
88             elementName = g.getPossibleRelatedValue2(removedElement, Layer0.getInstance(g).HasName, Bindings.STRING);\r
89             RemoverUtil.remove(g, removedElement);\r
90         }\r
91 \r
92         // Add comment to change set.\r
93         CommentMetadata cm = g.getMetadata(CommentMetadata.class);\r
94         g.addMetadata(cm.add("Removed element " + elementName + " " + removedElement));\r
95     }\r
96 }\r