Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4a3dc4fdb20c80c56f30b661f89e6ffd7caac11f (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
/*******************************************************************************
 * 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:
 * 		Juergen Haug
 * 
 *******************************************************************************/

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

import java.util.ArrayList;

import org.eclipse.etrice.core.room.ActorClass;
import org.eclipse.etrice.core.room.StructureClass;
import org.eclipse.etrice.ui.structure.support.context.DeleteContext;
import org.eclipse.etrice.ui.structure.support.context.PositionUpdateContext;
import org.eclipse.etrice.ui.structure.support.provider.IPositionProvider;
import org.eclipse.etrice.ui.structure.support.provider.IPositionProvider.PosAndSize;
import org.eclipse.graphiti.features.IFeatureProvider;
import org.eclipse.graphiti.features.IReason;
import org.eclipse.graphiti.features.IUpdateFeature;
import org.eclipse.graphiti.features.context.IUpdateContext;
import org.eclipse.graphiti.features.context.impl.AddContext;
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.algorithms.GraphicsAlgorithm;
import org.eclipse.graphiti.mm.pictograms.Connection;
import org.eclipse.graphiti.mm.pictograms.ContainerShape;
import org.eclipse.graphiti.mm.pictograms.Diagram;
import org.eclipse.graphiti.mm.pictograms.Shape;

public class DiagramUpdateFeature extends AbstractUpdateFeature {

	public DiagramUpdateFeature(IFeatureProvider fp) {
		super(fp);
	}

	@Override
	public boolean canUpdate(IUpdateContext context) {
		return true;
	}

	@Override
	public IReason updateNeeded(IUpdateContext context) {
		StructureClass sc = (StructureClass) getBusinessObjectForPictogramElement(getDiagram());
		
		if(sc instanceof ActorClass && ((ActorClass)sc).getBase() != null)
			return new Reason(true);
		if(updateConnectionsNeeded(getDiagram()))
			return new Reason(true);
		if(updateNeeded(getDiagram()))
			return new Reason(true);

		return new Reason(false);
	}

	/**
	 * This just removes dangling connections (bindings and layer connections).
	 * New ones are added by the structure class support.
	 * 
	 * @param diagram
	 * @return
	 */
	private boolean updateConnectionsNeeded(Diagram diagram) {
		boolean needed = false;

		for (Connection conn : new ArrayList<Connection>(diagram.getConnections())) {
			UpdateContext context = new UpdateContext(conn);
			IUpdateFeature updateFeature = getFeatureProvider().getUpdateFeature(context);
			if (updateFeature!=null && updateFeature.canUpdate(context))
				if (updateFeature.updateNeeded(context).toBoolean())
					needed = true;
		}
		return needed;
	}

	/**
	 * @param diagram
	 * @return
	 */
	private boolean updateNeeded(ContainerShape container) {
		boolean needed = false;
		
		for (Shape child : new ArrayList<Shape>(container.getChildren())) {
			if (child instanceof ContainerShape)
				if (updateNeeded((ContainerShape) child))
					needed = true;
		}
		
		// avoid infinite recursion by not entering with diagram again
		if (!(container instanceof Diagram)) {
			UpdateContext context = new UpdateContext(container);
			IUpdateFeature updateFeature = getFeatureProvider().getUpdateFeature(context);
			if (updateFeature!=null && updateFeature.canUpdate(context))
				if (updateFeature.updateNeeded(context).toBoolean())
					needed = true;
		}
		
		return needed;
	}

	@Override
	public boolean update(IUpdateContext context) {
		StructureClass sc = (StructureClass) getBusinessObjectForPictogramElement(getDiagram());
		ContainerShape scShape = DiagramUtil.findScShape(getDiagram());
		IPositionProvider positionProvider = null;
		if(context instanceof PositionUpdateContext)
			positionProvider = ((PositionUpdateContext) context).getPositionProvider();
		
		if(scShape == null)
			scShape = createStructureClass(sc, positionProvider);
		
		boolean doneChanges = updateConnections(getDiagram());
		
		if (updateIfNeeded(getDiagram()))
			doneChanges = true;
		
		// structure class update
		if(scShape != null){
			IUpdateContext ctx;
			if(positionProvider != null)
				ctx = new PositionUpdateContext(scShape, positionProvider);
			else
				ctx = new UpdateContext(scShape);
			
			doneChanges |= getFeatureProvider().updateIfPossible(ctx).toBoolean();
		} 
		
		return doneChanges;
	}

	/**
	 * This just removes dangling connections (bindings and layer connections).
	 * New ones are added by the structure class support.
	 * 
	 * @param diagram
	 * @return
	 */
	private boolean updateConnections(Diagram diagram) {
		boolean doneChanges = false;

		ArrayList<Connection> connections = new ArrayList<Connection>(diagram.getConnections());
		for (Connection conn : connections) {
			UpdateContext context = new DeleteContext(conn);
			IUpdateFeature updateFeature = getFeatureProvider().getUpdateFeature(context);
			if (updateFeature!=null && updateFeature.canUpdate(context))
				if (updateFeature.updateNeeded(context).toBoolean())
					if (updateFeature.update(context))
						doneChanges = true;
		}
		return doneChanges;
	}

	/**
	 * @param diagram
	 * @return
	 */
	private boolean updateIfNeeded(ContainerShape container) {
		boolean doneChanges = false;
		
		// first recursion because ref ports might be needed for bindings
		// we need to make a copy since children might be removed
		ArrayList<Shape> children = new ArrayList<Shape>(container.getChildren());
		for (Shape child : children) {
			if (child instanceof ContainerShape)
				if (updateIfNeeded((ContainerShape) child))
					doneChanges = true;
		}
		
		// avoid infinite recursion by not entering with diagram again
		if (!(container instanceof Diagram)) {
			UpdateContext context = new DeleteContext(container);
			IUpdateFeature updateFeature = getFeatureProvider().getUpdateFeature(context);
			if (updateFeature!=null && updateFeature.canUpdate(context))
				if (updateFeature.updateNeeded(context).toBoolean())
					if (updateFeature.update(context))
						doneChanges = true;
		}
		
		return doneChanges;
	}
	
	private ContainerShape createStructureClass(StructureClass sc, IPositionProvider positionProvider){
		AddContext addContext = new AddContext();
		addContext.setNewObject(sc);
		addContext.setTargetContainer(getDiagram());
		addContext.setX(StructureClassSupport.MARGIN);
		addContext.setY(StructureClassSupport.MARGIN);
				
		ContainerShape scShape = (ContainerShape) getFeatureProvider().addIfPossible(addContext);
		
		if(positionProvider != null){
			PosAndSize pos = positionProvider.getDiagramPosition();
			GraphicsAlgorithm ga = scShape.getGraphicsAlgorithm();
			if(pos != null){
				ga.setX(pos.getX());
				ga.setY(pos.getY());
				if(pos.getW() > 0)
					ga.setWidth(pos.getW());
				if(pos.getH() > 0)
					ga.setHeight(pos.getH());
			}
		}
		
		return scShape;
	}

}

Back to the top