Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d2de2bb2c2127d874effdc4aa381ae6cfbfae02f (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
257
258
259
/*****************************************************************************
 * Copyright (c) 2009 Atos Origin.
 *
 *    
 * 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:
 *  Alexia Allanic (Atos Origin) alexia.allanic@atosorigin.com - LayouttoolController Implementation
 *
 *****************************************************************************/

package org.eclipse.papyrus.layout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.zest.layouts.InvalidLayoutConfiguration;
import org.eclipse.zest.layouts.LayoutAlgorithm;
import org.eclipse.zest.layouts.algorithms.AbstractLayoutAlgorithm;
import org.eclipse.zest.layouts.exampleStructures.SimpleNode;
import org.eclipse.zest.layouts.exampleStructures.SimpleRelationship;

/**
 * The class LayouttoolController.
 */
public class LayouttoolController {

	/** The sub editor. */
	private LayouttoolInterface subEditor;

	/** The list edit part selected. */
	private List<EditPart> listEditPartSelected;

	/** The current layout algorithm. */
	private LayoutAlgorithm currentLayoutAlgorithm;

	/** The list nodes. */
	private List<SimpleNode> listNodes = new ArrayList<SimpleNode>();

	/** The list nodes edit part. */
	private List<EditPart> listNodesEditPart = new ArrayList<EditPart>();

	/** The list relationships. */
	private List<SimpleRelationship> listRelationships = new ArrayList<SimpleRelationship>();

	/**
	 * The Constructor.
	 * 
	 * @param list
	 *        of EditPart selected
	 * @param currentAlgorithm
	 *        algorithm to apply
	 */
	public LayouttoolController(List<EditPart> list, AbstractLayoutAlgorithm currentAlgorithm) {
		listEditPartSelected = list;
		this.currentLayoutAlgorithm = currentAlgorithm;
	}

	/**
	 * Transform nodes into SimpleNodes and relationships into SimpleRelationship Calculate bounds
	 * of the layout Apply layout.
	 */
	public void execute() {
		subEditor = LayoutToolExtensionPointManager.getInstance().getSubEditor();
		if(subEditor == null) {
			Activator.getDefault().log("Editor not found");
			return;
		}
		if(listEditPartSelected == null || listEditPartSelected.size() <= 1) {
			Activator.getDefault().log("Select at least two elements");
			return;
		}
		// create all nodes
		createNodes();
		if(listNodes == null || listNodes.size() <= 1) {
			Activator.getDefault().log("Any node selected");
			return;
		}
		// find relationships of all nodes selected
		createRelationships();
		// Create array of EditParts selected
		EditPart[] arrayEditPart = new EditPart[listEditPartSelected.size()];
		listEditPartSelected.toArray(arrayEditPart);
		// Get layout area where algorithms must be apply
		Rectangle layoutArea = subEditor.getLayoutArea(arrayEditPart);
		// Create array of simpleNodes
		SimpleNode[] listSimpleNode = new SimpleNode[listNodes.size()];
		listNodes.toArray(listSimpleNode);
		// Create array of simpleRelationships
		SimpleRelationship[] listSimpleRelationship = new SimpleRelationship[listRelationships.size()];
		listRelationships.toArray(listSimpleRelationship);
		try {
			currentLayoutAlgorithm
					.applyLayout(listSimpleNode, listSimpleRelationship, layoutArea.preciseX(), layoutArea.preciseY(),
					layoutArea.preciseWidth() - 40, layoutArea.preciseHeight() - 5, false, false);
		} catch (InvalidLayoutConfiguration e) {
			Activator.getDefault().log(e.getMessage() + " : Layout cannot be applied", e);
		}
		Map<EditPart, Rectangle> map = matchEditPartCoordinates();
		subEditor.execute(map);
	}

	/**
	 * For all nodes, find source relationships and target relationships and create
	 * SimpleRelationships.
	 */
	private void createRelationships() {
		for(int i = 0; i < listNodes.size(); i++) {
			List<EditPart> sources = getSourceRelationships((EditPart)listNodes.get(i).getRealObject());
			for(EditPart source : sources) {
				EditPart node = subEditor.getTarget(source);
				SimpleRelationship relationship = getSimpleRelationship(subEditor.getSource(source), subEditor
						.getTarget(source));
				if(!listRelationships.contains(relationship) && listNodesEditPart.contains(node)) {
					listRelationships.add(relationship);
				}
				listNodes.get(i).addRelationship(relationship);
			}
			List<EditPart> targets = getTargetRelationships((EditPart)listNodes.get(i).getRealObject());
			for(EditPart target : targets) {
				EditPart node = subEditor.getSource(target);
				SimpleRelationship relationship = getSimpleRelationship(subEditor.getSource(target), subEditor
						.getTarget(target));
				if(!listRelationships.contains(relationship) && listNodesEditPart.contains(node)) {
					listRelationships.add(relationship);
				}
				listNodes.get(i).addRelationship(relationship);
			}
		}
	}

	/**
	 * For all nodes selected, create SimpleNodes.
	 */
	private void createNodes() {
		for(EditPart editPart : listEditPartSelected) {
			if(subEditor.isNode(editPart)) {
				Rectangle coordinates = subEditor.getBounds(editPart);
				if(coordinates != null) {
					listNodes.add(getSimpleNode(editPart, coordinates));
					listNodesEditPart.add(editPart);
				}
			}
		}
	}

	/**
	 * Gets the simple relationship.
	 * 
	 * @param source
	 *        node
	 * @param destination
	 *        node
	 * 
	 * @return SimpleRelationship
	 */
	public SimpleRelationship getSimpleRelationship(EditPart source, EditPart destination) {
		SimpleNode simpleNodeSource = null;
		SimpleNode simpleNodeDestination = null;
		EditPart editPart;
		// for all nodes, search the source SimpleNode and the destination SimpleNode of this
		// relationship
		for(SimpleNode sn : listNodes) {
			editPart = (EditPart)sn.getRealObject();
			if(editPart.equals(source)) {
				simpleNodeSource = sn;
			}
			if(editPart.equals(destination)) {
				simpleNodeDestination = sn;
			}
		}
		SimpleRelationship simpleRelationship = new SimpleRelationship(simpleNodeSource, simpleNodeDestination, true);
		return simpleRelationship;
	}

	/**
	 * Match each editPart with their new coordinates.
	 * 
	 * @return map EditPart with his news coordinates
	 */
	public Map<EditPart, Rectangle> matchEditPartCoordinates() {
		Map<EditPart, Rectangle> map = new HashMap<EditPart, Rectangle>();
		for(SimpleNode node : this.listNodes) {
			Rectangle coordinates = new Rectangle((int)node.getXInLayout(), (int)node.getYInLayout(), (int)node
					.getWidthInLayout(), (int)node.getHeightInLayout());
			map.put((EditPart)node.getRealObject(), coordinates);
		}
		return map;
	}

	/**
	 * Gets the simple node.
	 * 
	 * @param element
	 *        node
	 * @param coordinates
	 *        the coordinates
	 * 
	 * @return SimpleNode
	 */
	public SimpleNode getSimpleNode(EditPart element, Rectangle coordinates) {
		return getSimpleNode(element, coordinates, false);
	}

	/**
	 * Gets the simple node.
	 * 
	 * @param element
	 *        node
	 * @param coordinates
	 *        the coordinates
	 * @param ignoreInLayout
	 *        ignore a node when apply layout
	 * 
	 * @return SimpleNode
	 */
	public SimpleNode getSimpleNode(EditPart element, Rectangle coordinates, boolean ignoreInLayout) {
		SimpleNode node = new SimpleNode(element, coordinates.preciseX(), coordinates.preciseY(), coordinates
				.preciseWidth(), coordinates.preciseHeight());
		node.ignoreInLayout(ignoreInLayout);
		return node;
	}

	/**
	 * Gets the source relationships.
	 * 
	 * @param editPart
	 *        node
	 * 
	 * @return list of EditPart
	 */
	public List<EditPart> getSourceRelationships(EditPart editPart) {
		GraphicalEditPart gep = (GraphicalEditPart)editPart;
		List<EditPart> sourceConnections = gep.getSourceConnections();
		return sourceConnections;
	}

	/**
	 * Gets the target relationships.
	 * 
	 * @param editPart
	 *        node
	 * 
	 * @return list of EditPart
	 */
	public List<EditPart> getTargetRelationships(EditPart editPart) {
		GraphicalEditPart gep = (GraphicalEditPart)editPart;
		List<EditPart> targetConnections = gep.getTargetConnections();
		return targetConnections;
	}
}

Back to the top