Skip to content

Package: CreateChildAction

CreateChildAction

nameinstructionbranchcomplexitylinemethod
CreateChildAction(EObject, EditingDomain, ISelectionProvider, CommandParameter, CreateElementCallback)
M: 0 C: 23
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
createActionCommand(EditingDomain, Collection)
M: 2 C: 15
88%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 4
80%
M: 0 C: 1
100%
run()
M: 2 C: 80
98%
M: 2 C: 10
83%
M: 2 C: 5
71%
M: 1 C: 21
95%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (c) 2002-2007 IBM Corporation and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * IBM - Initial API and implementation
13: */
14: // REUSED CLASS
15: package org.eclipse.emfforms.spi.swt.treemasterdetail.util;
16:
17: import java.util.Collection;
18:
19: import org.eclipse.emf.common.command.Command;
20: import org.eclipse.emf.common.command.UnexecutableCommand;
21: import org.eclipse.emf.ecore.EObject;
22: import org.eclipse.emf.ecore.EReference;
23: import org.eclipse.emf.edit.command.AddCommand;
24: import org.eclipse.emf.edit.command.CommandParameter;
25: import org.eclipse.emf.edit.command.CreateChildCommand;
26: import org.eclipse.emf.edit.command.SetCommand;
27: import org.eclipse.emf.edit.domain.EditingDomain;
28: import org.eclipse.emf.edit.ui.action.emfforms.StaticSelectionCommandAction;
29: import org.eclipse.jface.viewers.ISelectionProvider;
30: import org.eclipse.jface.viewers.StructuredSelection;
31: import org.eclipse.jface.viewers.Viewer;
32:
33: /**
34: * A child creation action is implemented by creating a {@link CreateChildCommand}.
35: */
36: public class CreateChildAction extends StaticSelectionCommandAction {
37:         /**
38:          * This describes the child to be created.
39:          */
40:         private final CommandParameter descriptor;
41:         private final EditingDomain editingDomain;
42:         private final ISelectionProvider selectionProvider;
43:         private final EObject parent;
44:         private final CreateElementCallback createElementCallback;
45:
46:         /**
47:          * This constructs an instance of an action that uses the given editing domain to create a child
48:          * specified by <code>descriptor</code> for the single object in the <code>selection</code>.
49:          *
50:          * @since 2.4.0
51:          */
52:         public CreateChildAction(EObject parent, EditingDomain editingDomain, ISelectionProvider selectionProvider,
53:                 CommandParameter descriptor, CreateElementCallback createElementCallback) {
54:                 super(editingDomain);
55:                 this.parent = parent;
56:                 this.descriptor = descriptor;
57:                 this.selectionProvider = selectionProvider;
58:                 configureAction(selectionProvider.getSelection());
59:                 this.editingDomain = editingDomain;
60:                 this.createElementCallback = createElementCallback;
61:         }
62:
63:         /**
64:          * This creates the command for {@link StaticSelectionCommandAction#createActionCommand}.
65:          */
66:         @Override
67:         protected Command createActionCommand(EditingDomain editingDomain, Collection<?> collection) {
68:•                if (collection.size() == 1) {
69:                         final Object owner = collection.iterator().next();
70:                         return CreateChildCommand.create(editingDomain, owner,
71:                                 descriptor, collection);
72:                 }
73:                 return UnexecutableCommand.INSTANCE;
74:         }
75:
76:         @Override
77:         public void run() {
78:                 final EReference reference = descriptor.getEReference();
79:
80:                 final EObject newObject = descriptor.getEValue();
81:
82:•                if (createElementCallback != null) {
83:                         createElementCallback.initElement(parent, reference, newObject);
84:                 }
85:                 // Add the element, so that it is contained in the ResourceSet.
86:
87:                 final Command addCommand;
88:•                if (reference.getUpperBound() == 1) {
89:                         addCommand = SetCommand.create(editingDomain, parent, reference, newObject);
90:                 } else {
91:                         addCommand = AddCommand.create(editingDomain, parent, reference,
92:                                 newObject);
93:                 }
94:                 editingDomain.getCommandStack().execute(addCommand);
95:
96:                 boolean callbackResult = true;
97:
98:•                if (createElementCallback != null) {
99:                         /* ask callback after command execution so that callback can navigate to parent */
100:                         callbackResult = createElementCallback.beforeCreateElement(newObject);
101:                 }
102:
103:•                if (callbackResult) {
104:                         // Select the newly added element. It is already added
105:•                        if (selectionProvider instanceof Viewer) {
106:                                 ((Viewer) selectionProvider).refresh();
107:                         }
108:                         selectionProvider.setSelection(new StructuredSelection(newObject));
109:•                        if (createElementCallback != null) {
110:                                 createElementCallback.afterCreateElement(newObject);
111:                         }
112:                 } else {
113:                         // If the callback says "cancel" undo the addCommand.
114:                         addCommand.undo();
115:                 }
116:         }
117: }