blob: 5cc452133b1e3461387ccf6b682b62f22450a09e (
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
/*******************************************************************************
* Copyright (c) 2008, 2014 THALES GLOBAL SERVICES and others.
* 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.tools.api.command;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Stack;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.sirius.business.api.logger.RuntimeLoggerInterpreter;
import org.eclipse.sirius.business.api.logger.RuntimeLoggerManager;
import org.eclipse.sirius.common.tools.api.interpreter.IInterpreter;
import org.eclipse.sirius.tools.api.interpreter.InterpreterUtil;
import org.eclipse.sirius.viewpoint.DRepresentation;
import org.eclipse.sirius.viewpoint.description.tool.For;
import org.eclipse.sirius.viewpoint.description.tool.ToolPackage;
/**
* This class keeps the trace of all contexts.
*
* @author cbrun
*/
public class CommandContext {
/** The stack of all context. */
private Stack<EObject> targetStack = new Stack<EObject>();
/** The initial push. */
private EObject nextPushEObject;
private DRepresentation representation;
/**
* Create a new {@link CommandContext}.
*
* @param target
* the first context.
* @param representation
* the current representation
* @since 0.9.0
*/
public CommandContext(final EObject target, final DRepresentation representation) {
this.nextPushEObject = target;
this.representation = representation;
}
/**
* Get the current representation.
*
* @return the representation currently edited
* @since 0.9.0
*/
public DRepresentation getRepresentation() {
return this.representation;
}
/**
* Return the current context.
*
* @return the current context.
*/
public EObject getCurrentTarget() {
if (this.targetStack.isEmpty()) {
return nextPushEObject;
}
return this.targetStack.peek();
}
/**
* Return the next element to push.
*
* @return the next element to push.
*/
public EObject getNextPush() {
return this.nextPushEObject != null ? this.nextPushEObject : this.getCurrentTarget();
}
/**
* Clear the next push.
*/
public void clearNextPush() {
this.nextPushEObject = null;
}
/**
* Define the next push.
*
* @param nextPushEObject
* the next push.
*/
public void setNextPushEObject(final EObject nextPushEObject) {
this.nextPushEObject = nextPushEObject;
}
/**
* Push the specified object.
*
* @param newTarget
* the object to push.
*/
public void pushTarget(final EObject newTarget) {
targetStack.push(newTarget);
}
/**
* Pop and return the popped context. Return <code>null</code> if the is no
* available context.
*
* @return the popped context.
*/
public EObject popTarget() {
EObject result = null;
if (!targetStack.isEmpty()) {
result = targetStack.pop();
}
return result;
}
/**
* Push the context of the specified command context.
*
* @param context
* the command context.
*/
public static void pushContext(final CommandContext context) {
context.pushTarget(context.getNextPush());
context.clearNextPush();
}
/**
* Pop the context of the specified command context.
*
* @param context
* the command context.
*/
public static void popContext(final CommandContext context) {
context.popTarget();
}
/**
* Load the list of contexts.
*
* @param forOp
* the expression that gets the list of contexts.
* @param context
* the current context.
* @return the list of contexts.
*/
public static List<Object> getContextTargets(final For forOp, final CommandContext context) {
List<Object> contextTargets = null;
final IInterpreter inter = InterpreterUtil.getInterpreter(context.getCurrentTarget());
final RuntimeLoggerInterpreter safeInterpreter = RuntimeLoggerManager.INSTANCE.decorate(inter);
final Object result = safeInterpreter.evaluate(context.getCurrentTarget(), forOp, ToolPackage.eINSTANCE.getFor_Expression());
if (result == null) {
contextTargets = new ArrayList<>();
} else if (result instanceof Collection) {
contextTargets = new ArrayList<Object>((Collection<?>) result);
} else if (result.getClass().isArray()) {
contextTargets = new ArrayList<Object>(Arrays.asList((Object[]) result));
} else {
contextTargets = new ArrayList<Object>(Arrays.asList(result));
}
return contextTargets;
}
}
|