Skip to content

Package: DuplicateRowAction

DuplicateRowAction

nameinstructionbranchcomplexitylinemethod
DuplicateRowAction(TableRendererViewerActionContext)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
canExecute()
M: 8 C: 5
38%
M: 5 C: 1
17%
M: 3 C: 1
25%
M: 1 C: 2
67%
M: 0 C: 1
100%
copy(Collection)
M: 0 C: 16
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
copyBidirectionalReferences(EObject, EObject)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
copyElements(EObject, EStructuralFeature, EditingDomain, Collection)
M: 25 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
execute()
M: 35 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
getId()
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$1(EObject, EObject, EObject, Collection)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
lambda$2(EObject, EObject, EStructuralFeature.Setting)
M: 0 C: 23
100%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 0 C: 5
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2018 EclipseSource Muenchen GmbH 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: * Mat Hansen - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.spi.table.swt.action;
15:
16: import java.util.Collection;
17: import java.util.List;
18: import java.util.Map;
19:
20: import org.eclipse.emf.common.command.Command;
21: import org.eclipse.emf.ecore.EObject;
22: import org.eclipse.emf.ecore.EReference;
23: import org.eclipse.emf.ecore.EStructuralFeature;
24: import org.eclipse.emf.ecore.EStructuralFeature.Setting;
25: import org.eclipse.emf.ecore.util.EcoreUtil;
26: import org.eclipse.emf.ecore.util.EcoreUtil.Copier;
27: import org.eclipse.emf.edit.command.AddCommand;
28: import org.eclipse.emf.edit.domain.EditingDomain;
29: import org.eclipse.emfforms.spi.swt.table.action.ViewerActionContext;
30: import org.eclipse.jface.viewers.IStructuredSelection;
31: import org.eclipse.jface.viewers.StructuredSelection;
32:
33: /**
34: * Action to duplicate a row within a table viewer.
35: *
36: * @author Mat Hansen <mhansen@eclipsesource.com>
37: * @since 1.18
38: *
39: */
40: public class DuplicateRowAction extends TableRendererAction {
41:
42:         /**
43:          * The ID of this action.
44:          */
45:         public static final String ACTION_ID = PREFIX + "tablecontrol.duplicate_row"; //$NON-NLS-1$
46:
47:         /**
48:          * The default key binding of this action.
49:          */
50:         public static final String DEFAULT_KEYBINDING = "M1+d"; //$NON-NLS-1$
51:
52:         /**
53:          * The constructor.
54:          *
55:          * @param actionContext the {@link ViewerActionContext}
56:          */
57:         public DuplicateRowAction(TableRendererViewerActionContext actionContext) {
58:                 super(actionContext);
59:         }
60:
61:         @Override
62:         public String getId() {
63:                 return ACTION_ID;
64:         }
65:
66:         @Override
67:         public void execute() {
68:                 final Setting setting = getActionContext().getSetting();
69:                 final EObject eObject = setting.getEObject();
70:                 final EStructuralFeature eStructuralFeature = setting.getEStructuralFeature();
71:
72:                 @SuppressWarnings("unchecked")
73:                 final List<EObject> toDuplicate = ((IStructuredSelection) getTableViewer().getSelection()).toList();
74:
75:                 final Collection<EObject> copies = copyElements(
76:                         eObject, eStructuralFeature, getEditingDomainForContainment(), toDuplicate);
77:
78:                 getTableViewer().setSelection(new StructuredSelection(copies.iterator().next()), true);
79:         }
80:
81:         private Collection<EObject> copyElements(EObject eObject, EStructuralFeature structuralFeature,
82:                 EditingDomain editingDomain, Collection<EObject> elementsToCopy) {
83:
84:                 final Collection<EObject> copies = copy(elementsToCopy);
85:                 final Command createCommand = AddCommand.create(editingDomain, eObject, structuralFeature, copies);
86:•                if (createCommand.canExecute()) {
87:•                        if (editingDomain.getCommandStack() == null) {
88:                                 createCommand.execute();
89:                         } else {
90:                                 editingDomain.getCommandStack().execute(createCommand);
91:                         }
92:                 }
93:                 return copies;
94:         }
95:
96:         /**
97:          * Copies the collection of {@link EObject}.
98:          *
99:          * @param elementsToCopy The {@link Collection} to copy
100:          * @return The copied {@link Collection}
101:          */
102:         Collection<EObject> copy(Collection<EObject> elementsToCopy) {
103:                 final Copier copier = new Copier();
104:                 final Collection<EObject> copies = copier.copyAll(elementsToCopy);
105:                 copier.copyReferences();
106:                 copier.forEach(this::copyBidirectionalReferences);
107:                 return copies;
108:         }
109:
110:         private void copyBidirectionalReferences(final EObject original, final EObject copy) {
111:                 final Map<EObject, Collection<Setting>> find = EcoreUtil.ExternalCrossReferencer.find(original);
112:                 // find all bidirectional references and manually set them on the copy
113:                 find.forEach((referenced, settings) -> {
114:                         settings.forEach(setting -> {
115:                                 final EStructuralFeature feat = setting.getEStructuralFeature();
116:                                 final EReference opp = ((EReference) feat).getEOpposite();
117:                                 // if the opposite is a singleton, then we would change its reference, so ignore
118:•                                if (opp != null && opp.isMany() && setting.getEObject() == original) {
119:                                         copy.eSet(feat, setting.get(true));
120:                                 }
121:                         });
122:                 });
123:         }
124:
125:         @Override
126:         public boolean canExecute() {
127:•                if (isTableDisabled() || isUpperBoundReached() || isViewerSelectionInvalid()) {
128:                         return false;
129:                 }
130:
131:                 return true;
132:         }
133:
134: }