blob: b74f8ace927745d59a10c6a2ae17e92850976b40 (
plain) (
blame)
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
85
86
87
88
89
90
91
92
93
94
95
|
/*******************************************************************************
* Copyright (c) 2014 THALES GLOBAL SERVICES.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.diagram.ui.tools.internal.handler;
import java.util.List;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CommandStack;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramCommandStack;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.sirius.diagram.ui.edit.api.part.AbstractDiagramContainerEditPart;
import org.eclipse.sirius.diagram.ui.tools.api.requests.RequestConstants;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.handlers.HandlerUtil;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
/**
* The Reset Diagram or Container Origin command handler.
*
* @author Florian Barbin
*
*/
public class ResetOriginHandler extends AbstractHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
ISelection selection = HandlerUtil.getCurrentSelection(event);
IWorkbenchPart workbenchPart = HandlerUtil.getActivePart(event);
if (selection instanceof StructuredSelection) {
StructuredSelection structuredSelection = (StructuredSelection) selection;
if (structuredSelection.getFirstElement() instanceof DiagramEditPart) {
getAndExecuteCmd(workbenchPart, (DiagramEditPart) structuredSelection.getFirstElement());
} else {
Predicate<AbstractDiagramContainerEditPart> noRegionContainer = new Predicate<AbstractDiagramContainerEditPart>() {
@Override
public boolean apply(AbstractDiagramContainerEditPart input) {
return !input.isRegionContainer();
}
};
List<AbstractDiagramContainerEditPart> selectedAbstractDiagramContainerEditParts = Lists.newArrayList(Iterators.filter(
Iterators.filter(structuredSelection.iterator(), AbstractDiagramContainerEditPart.class), noRegionContainer));
if (!selectedAbstractDiagramContainerEditParts.isEmpty()) {
getAndExecuteCmd(workbenchPart, selectedAbstractDiagramContainerEditParts.toArray(new AbstractDiagramContainerEditPart[0]));
}
}
}
return null;
}
private void getAndExecuteCmd(IWorkbenchPart workbenchPart, GraphicalEditPart... selection) {
Command command;
if (selection.length == 1) {
command = selection[0].getCommand(new Request(RequestConstants.REQ_RESET_ORIGIN));
} else {
CompoundCommand compoundCommand = new CompoundCommand();
for (int i = 0; i < selection.length; i++) {
compoundCommand.add(selection[i].getCommand(new Request(RequestConstants.REQ_RESET_ORIGIN)));
}
command = compoundCommand;
}
DiagramCommandStack commandStack = getDiagramCommandStack(workbenchPart);
if (commandStack != null) {
commandStack.execute(command);
}
}
private DiagramCommandStack getDiagramCommandStack(IWorkbenchPart workbenchPart) {
Object stack = workbenchPart.getAdapter(CommandStack.class);
return (stack instanceof DiagramCommandStack) ? (DiagramCommandStack) stack : null;
}
}
|