Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 61c5c28793aefb87f13217f3c251a6a06da9284c (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*******************************************************************************
 * Copyright (c) 2012 protos software gmbh (http://www.protos.de).
 * 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:
 * 		Henrik Rentz-Reichert (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.ui.behavior.support;

import java.util.ArrayList;

import org.eclipse.etrice.core.room.ActorClass;
import org.eclipse.etrice.core.room.StateGraph;
import org.eclipse.etrice.ui.behavior.commands.StateGraphContext;
import org.eclipse.graphiti.features.IFeatureProvider;
import org.eclipse.graphiti.features.IReason;
import org.eclipse.graphiti.features.IRemoveFeature;
import org.eclipse.graphiti.features.IUpdateFeature;
import org.eclipse.graphiti.features.context.IRemoveContext;
import org.eclipse.graphiti.features.context.IUpdateContext;
import org.eclipse.graphiti.features.context.impl.RemoveContext;
import org.eclipse.graphiti.features.context.impl.UpdateContext;
import org.eclipse.graphiti.features.impl.AbstractUpdateFeature;
import org.eclipse.graphiti.features.impl.Reason;
import org.eclipse.graphiti.mm.pictograms.Connection;
import org.eclipse.graphiti.mm.pictograms.ContainerShape;
import org.eclipse.graphiti.mm.pictograms.Shape;
import org.eclipse.graphiti.platform.IDiagramEditor;

/**
 * @author Henrik Rentz-Reichert (initial contribution)
 *
 */
public class DiagramUpdateFeature extends AbstractUpdateFeature {

	private ArrayList<Shape> usedShapes = new ArrayList<Shape>();
	
	/**
	 * @param fp
	 */
	public DiagramUpdateFeature(IFeatureProvider fp) {
		super(fp);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.graphiti.func.IUpdate#canUpdate(org.eclipse.graphiti.features.context.IUpdateContext)
	 */
	@Override
	public boolean canUpdate(IUpdateContext context) {
		return true;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.graphiti.func.IUpdate#updateNeeded(org.eclipse.graphiti.features.context.IUpdateContext)
	 */
	@Override
	public IReason updateNeeded(IUpdateContext context) {
		ActorClass ac = SupportUtil.getActorClass(getDiagram());
		StateGraphContext tree = StateGraphContext.createContextTree(ac);
		
		usedShapes.clear();
		
		IReason needed = updateNeeded(tree);
		if (needed.toBoolean())
			return needed;
		
		// check for unused state graph shapes
		for (Shape sgshape : getDiagram().getChildren()) {
			if (!usedShapes.contains(sgshape))
				return Reason.createTrueReason();
			
			if (sgshape instanceof ContainerShape) {
				for (Shape child : ((ContainerShape) sgshape).getChildren()) {
					if (child instanceof ContainerShape && !usedShapes.contains(child))
						return Reason.createTrueReason();
				}
			}
		}
		
		// check for dangling connections or connections to update
		for (Connection conn : getDiagram().getConnections()) {
			if (conn.getStart()==null || conn.getEnd()==null)
				return Reason.createTrueReason();

			UpdateContext uf = new UpdateContext(conn);
			IUpdateFeature updateFeature = getFeatureProvider().getUpdateFeature(uf);
			if (updateFeature!=null && updateFeature.canUpdate(uf)) {
				IReason needUpdate = updateFeature.updateNeeded(uf);
				if (needUpdate.toBoolean())
					return needUpdate;
			}
		}
		
		return Reason.createFalseReason();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.graphiti.func.IUpdate#update(org.eclipse.graphiti.features.context.IUpdateContext)
	 */
	@Override
	public boolean update(IUpdateContext context) {
		ActorClass ac = SupportUtil.getActorClass(getDiagram());
		StateGraphContext tree = StateGraphContext.createContextTree(ac);

		usedShapes.clear();
		
		boolean changed = update(tree);
		
		// remove unused state graph shapes
		ArrayList<Shape> children = new ArrayList<Shape>(getDiagram().getChildren());
		for (Shape sgshape : children) {
			if (!usedShapes.contains(sgshape)) {
				IRemoveContext rc = new RemoveContext(sgshape);
				IRemoveFeature removeFeature = getFeatureProvider().getRemoveFeature(rc);
				if (removeFeature != null) {
					removeFeature.remove(rc);
					if (removeFeature.hasDoneChanges())
						changed = true;
				}
			}
		}
		
		// remove dangling connections and update other ones
		ArrayList<Connection> connections = new ArrayList<Connection>(getDiagram().getConnections());
		for (Connection conn : connections) {
			if (conn.getStart()==null || conn.getEnd()==null) {
				IRemoveContext rc = new RemoveContext(conn);
				IRemoveFeature removeFeature = getFeatureProvider().getRemoveFeature(rc);
				if (removeFeature != null) {
					removeFeature.remove(rc);
					if (removeFeature.hasDoneChanges())
						changed = true;
				}
			}
			else {
				UpdateContext uf = new UpdateContext(conn);
				IUpdateFeature updateFeature = getFeatureProvider().getUpdateFeature(uf);
				if (updateFeature!=null && updateFeature.canUpdate(uf)) {
					if (updateFeature.update(uf))
						changed = true;
				}
			}
		}
		
		// if we inserted states they have been selected: reset the selection
		IDiagramEditor diagramEditor = getFeatureProvider().getDiagramTypeProvider().getDiagramEditor();
		if (diagramEditor != null)
			diagramEditor.setPictogramElementForSelection(null);
		
		return changed;
	}

	/**
	 * @param ctx
	 * @return
	 */
	private IReason updateNeeded(StateGraphContext ctx) {
		StateGraph sg = ctx.getStateGraph();
		ContainerShape cont = findStateGraphContainer(sg);
		if (cont==null)
			return Reason.createTrueReason("sub graph missing");
		
		usedShapes.add(cont);
		
		StateGraphUpdateContext context = new StateGraphUpdateContext(cont, ctx);
		IUpdateFeature updateFeature = getFeatureProvider().getUpdateFeature(context);
		if (updateFeature!=null && updateFeature.canUpdate(context)) {
			IReason needUpdate = updateFeature.updateNeeded(context);
			if (needUpdate.toBoolean())
				return needUpdate;
		}
		
		for (Shape child : cont.getChildren()) {
			UpdateContext uf = new UpdateContext(child);
			updateFeature = getFeatureProvider().getUpdateFeature(uf);
			if (updateFeature!=null && updateFeature.canUpdate(uf)) {
				IReason needUpdate = updateFeature.updateNeeded(uf);
				if (needUpdate.toBoolean())
					return needUpdate;
			}
		}
		
		// recursion
		for (StateGraphContext child : ctx.getChildren()) {
			IReason needUpdate = updateNeeded(child);
			if (needUpdate.toBoolean())
				return needUpdate;
		}
		
		return Reason.createFalseReason();
	}

	/**
	 * @param ctx
	 * @return
	 */
	private boolean update(StateGraphContext ctx) {
		boolean changed = false;
		
		StateGraph sg = ctx.getStateGraph();
		ContainerShape cont = findStateGraphContainer(sg);
		if (cont==null) {
			// create
			cont = SupportUtil.addStateGraph(ctx, getDiagram(), getFeatureProvider());
			changed = true;
			usedShapes.add(cont);
		}
		else {
			// update
			StateGraphUpdateContext context = new StateGraphUpdateContext(cont, ctx);
			IUpdateFeature updateFeature = getFeatureProvider().getUpdateFeature(context);
			if (updateFeature!=null && updateFeature.canUpdate(context)) {
				changed = updateFeature.update(context);
			}
			usedShapes.add(cont);
			
			// need a copy since original list may be modified by removal of shapes
			ArrayList<Shape> children = new ArrayList<Shape>(cont.getChildren());
			
			for (Shape child : children) {
				UpdateContext uf = new UpdateContext(child);
				updateFeature = getFeatureProvider().getUpdateFeature(uf);
				if (updateFeature!=null && updateFeature.canUpdate(uf)) {
					changed = updateFeature.update(uf);
				}
			}
		}
		
		// recursion
		for (StateGraphContext child : ctx.getChildren()) {
			if (update(child))
				changed = true;
		}
		
		return changed;
	}

	/**
	 * @param stateGraph
	 * @return
	 */
	private ContainerShape findStateGraphContainer(StateGraph sg) {
		for (Shape child : getDiagram().getChildren()) {
			Object bo = getBusinessObjectForPictogramElement(child);
			if (bo==sg)
				return (ContainerShape) child;
		}
		return null;
	}

}

Back to the top