Skip to content

Package: MoveRowUpAction

MoveRowUpAction

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