Merge branch 'develop' into LibrAlign
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / key / polytomous / operation / InsertPolytomousKeyNodeOperation.java
1 /**
2 * Copyright (C) 2016 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.taxeditor.editor.key.polytomous.operation;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
14 import org.eclipse.core.runtime.IAdaptable;
15 import org.eclipse.core.runtime.IProgressMonitor;
16
17 import eu.etaxonomy.cdm.api.application.CdmChangeEvent.Action;
18 import eu.etaxonomy.cdm.model.common.CdmBase;
19 import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
20 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
21 import eu.etaxonomy.taxeditor.operation.RemotingCdmDefaultOperation;
22
23 /**
24 * @author k.luther
25 * @date 17.11.2016
26 *
27 */
28 public class InsertPolytomousKeyNodeOperation extends RemotingCdmDefaultOperation {
29
30 private final PolytomousKeyNode parentNode;
31
32 private final static String LABEL = "Insert new polytomous key node";
33
34 /**
35 *
36 */
37 protected IPostOperationEnabled postOperationEnabled;
38
39 /**
40 * @param label
41 * @param action
42 * @param source
43 * @param async
44 */
45 public InsertPolytomousKeyNodeOperation(Object source,
46 boolean async,
47 PolytomousKeyNode parentNode, IPostOperationEnabled postOperationEnabled) {
48 super(LABEL, Action.Create, source, async);
49 this.parentNode = parentNode;
50 this.postOperationEnabled = postOperationEnabled;
51 }
52
53 /**
54 * {@inheritDoc}
55 */
56 @Override
57 protected CdmBase doSimpleExecute(IProgressMonitor monitor, IAdaptable info) throws Exception {
58 PolytomousKeyNode childNode = PolytomousKeyNode.NewInstance();
59 List<PolytomousKeyNode> newChildren = parentNode.getChildren();
60 List<PolytomousKeyNode> copy = new ArrayList<PolytomousKeyNode>();
61 for (PolytomousKeyNode node: newChildren) {
62 copy.add(node);
63 }
64 parentNode.addChild(childNode, 0);
65 for (PolytomousKeyNode node: copy){
66 childNode.addChild(node);
67 }
68 this.postOperationEnabled.postOperation(childNode);
69 return childNode;
70 }
71
72 }