1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
/*******************************************************************************
* Copyright (c) 2010 CEA LIST.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Nicolas Bros (Mia-Software) - initial API and implementation
*******************************************************************************/
package org.eclipse.emf.facet.widgets.celleditors.internal.ui;
import java.util.Collection;
import java.util.List;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.CompoundCommand;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.facet.widgets.celleditors.ICommandFactory;
public final class EditingUtils {
private EditingUtils() {
//
}
public static void moveElementsUp(final EObject eObject, final EStructuralFeature feature,
final Collection<Object> elements, final ICommandFactory commandFactory,
final EditingDomain editingDomain) {
CompoundCommand compoundCommand = new CompoundCommand();
List<?> list = (List<?>) eObject.eGet(feature);
int minIndex = 0;
for (Object element : elements) {
final int index = list.indexOf(element);
Command moveCommand = commandFactory.createMoveCommand(editingDomain, eObject, feature,
element, Math.max(index - 1, minIndex++));
compoundCommand.append(moveCommand);
}
editingDomain.getCommandStack().execute(compoundCommand);
// int[] selectionIndices;
// Arrays.sort(selectionIndices);
// CompoundCommand compoundCommand = new CompoundCommand();
// int minIndex = 0;
// for(int index : selectionIndices) {
// Command moveCommand = MoveCommand.create(editingDomain,
// eObject,feature, index, Math.max(index - 1,
// minIndex++));
// compoundCommand.append(moveCommand);
// }
// editingDomain.getCommandStack().execute(compoundCommand);
}
public static void moveElementsDown(final EObject eObject, final EStructuralFeature feature,
final Collection<Object> elements, final ICommandFactory commandFactory,
final EditingDomain editingDomain) {
Object value = eObject.eGet(feature);
List<?> list = (List<?>) value;
CompoundCommand compoundCommand = new CompoundCommand();
// prevent the last two elements from swapping
boolean canMove = !elements.contains(list.get(list.size() - 1));
for (int i = list.size() - 2; i >= 0; i--) {
final Object selectedObject = list.get(i);
if (elements.contains(selectedObject)) {
if (canMove) {
Command moveCommand = commandFactory.createMoveCommand(editingDomain, eObject,
feature, selectedObject, i + 1);
compoundCommand.append(moveCommand);
}
} else {
canMove = true;
}
}
editingDomain.getCommandStack().execute(compoundCommand);
}
}
|