1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.synchronization.graph;
14 import java.util.HashMap;
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;
33 * @author Tuukka Lehtonen
35 public class RemoveElement extends ModificationAdapter {
38 Resource removedElement;
40 public RemoveElement(Resource diagram, Resource removed) {
41 super(REMOVE_NODE_PRIORITY);
44 throw new NullPointerException();
46 throw new NullPointerException();
48 this.diagram = diagram;
49 this.removedElement = removed;
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);
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);
67 ConnectionUtil cu = new ConnectionUtil(g);
68 cu.removeConnection(removedElement);
70 Remover r = RemoverUtil.getPossibleRemover(g, removedElement);
72 String problem = r.canRemove(g, new HashMap<Object, Object>(4));
73 if (problem != null) {
74 throw new CannotRemoveException(problem);
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);
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);
92 // Add comment to change set.
93 CommentMetadata cm = g.getMetadata(CommentMetadata.class);
94 g.addMetadata(cm.add("Removed element " + elementName + " " + removedElement));