Skip to content

Package: MoveRowDownAction

MoveRowDownAction

nameinstructionbranchcomplexitylinemethod
MoveRowDownAction(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: 0 C: 37
100%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 0 C: 8
100%
M: 0 C: 1
100%
execute()
M: 0 C: 60
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 15
100%
M: 0 C: 1
100%
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$0(List, int, EditingDomain, EObject, EStructuralFeature, Object)
M: 2 C: 20
91%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 1 C: 4
80%
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.Arrays;
17: import java.util.Collections;
18: import java.util.List;
19: import java.util.Objects;
20: import java.util.stream.Collectors;
21:
22: import org.eclipse.emf.common.command.Command;
23: import org.eclipse.emf.common.command.CompoundCommand;
24: import org.eclipse.emf.ecore.EObject;
25: import org.eclipse.emf.ecore.EStructuralFeature;
26: import org.eclipse.emf.ecore.EStructuralFeature.Setting;
27: import org.eclipse.emf.edit.command.MoveCommand;
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:
32: /**
33: * Action to move a row upwards in a table viewer (requires the containment reference to be ordered).
34: *
35: * @author Mat Hansen <mhansen@eclipsesource.com>
36: * @since 1.18
37: *
38: */
39: public class MoveRowDownAction extends AbstractMoveRowAction {
40:
41:         /**
42:          * The ID of this action.
43:          */
44:         public static final String ACTION_ID = PREFIX + "tablecontrol.move_row_down"; //$NON-NLS-1$
45:
46:         /**
47:          * The default key binding of this action.
48:          */
49:         public static final String DEFAULT_KEYBINDING = "M1+M2+ARROW_DOWN"; //$NON-NLS-1$
50:
51:         /**
52:          * The constructor.
53:          *
54:          * @param actionContext the {@link ViewerActionContext}
55:          */
56:         public MoveRowDownAction(TableRendererViewerActionContext actionContext) {
57:                 super(actionContext);
58:         }
59:
60:         @Override
61:         public String getId() {
62:                 return ACTION_ID;
63:         }
64:
65:         @Override
66:         public void execute() {
67:                 final List<?> containments = getContainments();
68:
69:                 final List<?> moveDownList = Arrays.asList(
70:                         ((IStructuredSelection) getTableViewer().getSelection()).toArray());
71:                 sortSelectionBasedOnIndex(moveDownList, containments);
72:                 // need to reverse to avoid the moves interfering each other
73:                 Collections.reverse(moveDownList);
74:
75:                 final int maxIndex = containments.size() - 1;
76:                 final EditingDomain editingDomain = getActionContext().getEditingDomain();
77:                 final Setting setting = getActionContext().getSetting();
78:                 final EObject eObject = setting.getEObject();
79:                 final EStructuralFeature eStructuralFeature = setting.getEStructuralFeature();
80:
81:                 final List<Command> commands = moveDownList.stream().map(moveDownObject -> {
82:                         final int currentIndex = containments.indexOf(moveDownObject);
83:•                        if (currentIndex < 0 || currentIndex == maxIndex) {
84:                                 return null;
85:                         }
86:                         return new MoveCommand(
87:                                 editingDomain, eObject, eStructuralFeature, currentIndex, currentIndex + 1);
88:                 }).filter(Objects::nonNull).collect(Collectors.toList());
89:
90:                 editingDomain.getCommandStack().execute(new CompoundCommand(commands));
91:                 getTableViewer().refresh();
92:         }
93:
94:         @Override
95:         public boolean canExecute() {
96:                 final List<?> containments = getContainments();
97:
98:                 final List<?> moveDownList = Arrays.asList(
99:                         ((IStructuredSelection) getTableViewer().getSelection()).toArray());
100:•                if (moveDownList.isEmpty()) {
101:                         return false;
102:                 }
103:
104:                 // The lowest selected object must not already be at the end of the table
105:                 sortSelectionBasedOnIndex(moveDownList, containments);
106:                 Collections.reverse(moveDownList);
107:•                return containments.indexOf(moveDownList.get(0)) < containments.size() - 1 && super.canExecute();
108:         }
109: }