Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 72b7a85268260271c4caacf38ba0cfb55164bbdc (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
/*******************************************************************************
 * Copyright (c) 2012 Jayant Gupta
 * 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:
 * 		Jayant Gupta (initial contribution)
 * 
 * 
 *******************************************************************************/

package org.eclipse.etrice.ui.layout;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.etrice.core.room.ActorContainerRef;
import org.eclipse.etrice.core.room.InterfaceItem;
import org.eclipse.etrice.ui.structure.support.InterfaceItemSupport;
import org.eclipse.graphiti.features.IFeatureProvider;
import org.eclipse.graphiti.mm.algorithms.GraphicsAlgorithm;
import org.eclipse.graphiti.mm.algorithms.Text;
import org.eclipse.graphiti.mm.algorithms.styles.Orientation;
import org.eclipse.graphiti.mm.pictograms.Anchor;
import org.eclipse.graphiti.mm.pictograms.ContainerShape;
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
import org.eclipse.graphiti.services.Graphiti;
import org.eclipse.graphiti.services.IGaService;

import de.cau.cs.kieler.core.kgraph.KNode;
import de.cau.cs.kieler.core.kgraph.KPort;

/**
 * A command for applying the result of automatic layout to diagram elements in
 * eTrice Structure Editor.
 * 
 * @author jayant
 */
public class StructureLayoutCommand extends ETriceLayoutCommand {

	public StructureLayoutCommand(TransactionalEditingDomain domain,
			IFeatureProvider thefeatureProvider) {
		super(domain, thefeatureProvider);
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @author jayant
	 */
	@Override
	protected void applyNodeLayout(KNode knode, PictogramElement pelem) {

		setCalculatedPositionAndSize(knode, knode.getParent(),
				(ContainerShape) pelem);

		// Checking whether this node corresponds to an internal port or
		// ActorContainerRef
		EObject modelObject = pelem.getLink().getBusinessObjects().get(0);
		if (modelObject instanceof InterfaceItem) {
			// adjust label for internal port
			adjustLabelForPort((ContainerShape) pelem);
		}
	};

	/**
	 * {@inheritDoc}
	 * 
	 * @author jayant
	 */
	@Override
	protected void applyPortLayout(KPort kport, PictogramElement pelem) {

		ContainerShape shape = (ContainerShape) ((Anchor) pelem).getParent();

		setCalculatedPositionAndSize(kport, kport.getNode(), shape);

		// adjust label for this (boundary)port
		adjustLabelForPort(shape);
	}

	/**
	 * Extracts relevant information and delegates it to
	 * {@link #adjustLabel(Text, int, int, int, int, int)} for proper adjustment
	 * of port label
	 * 
	 * @param shape
	 *            the Shape for the port
	 * 
	 * @author jayant
	 */
	private static void adjustLabelForPort(ContainerShape shape) {
		GraphicsAlgorithm ga = shape.getGraphicsAlgorithm();
		EObject boContainer = shape.getContainer().getLink()
				.getBusinessObjects().get(0);

		// First make sure that the shape corresponds to a Port
		EObject bo = shape.getLink().getBusinessObjects().get(0);
		if (bo instanceof InterfaceItem) {
			// margin and size for bounding box (ActorClass )
			int margin = InterfaceItemSupport.MARGIN;
			int size = InterfaceItemSupport.ITEM_SIZE;

			if (boContainer instanceof ActorContainerRef) {
				// margin and size for ActorContainerRef
				margin = InterfaceItemSupport.MARGIN_SMALL;
				size = InterfaceItemSupport.ITEM_SIZE_SMALL;
			}

			Text label = (Text) (shape.getChildren().get(0)
					.getGraphicsAlgorithm());

			adjustLabel(label, ga.getX(), ga.getY(), ga.getWidth(), margin,
					size);
		}
	};

	/**
	 * Sets correct port label position depending on the corresponding port
	 * position.
	 * 
	 * @param label
	 *            Text Graphics Algorithm to be placed
	 * @param x
	 *            The x coordinate of the containing shape
	 * @param y
	 *            The y coordinate of the containing shape
	 * @param width
	 *            The width of the containing shape
	 * @param margin
	 *            The margin of the containing shape
	 * @param size
	 *            The size(length/width) of the port's visible graphics
	 *            algorithm
	 * 
	 */
	/*
	 * This method has been copied from InterfaceItemSuppot.FeatureProvider
	 * class since its visibility is not public there
	 */
	private static void adjustLabel(Text label, int x, int y, int width,
			int margin, int size) {
		Orientation align = Orientation.ALIGNMENT_CENTER;
		label.setHorizontalAlignment(align);

		int pos = margin + size / 2;

		if (x <= margin)
			align = Orientation.ALIGNMENT_LEFT;
		else if ((width - margin) <= x)
			align = Orientation.ALIGNMENT_RIGHT;
		if (y <= margin)
			pos = (margin - size) / 2;

		if (align != label.getHorizontalAlignment()) {
			label.setHorizontalAlignment(align);
		}
		if (pos != label.getY()) {
			IGaService gaService = Graphiti.getGaService();
			gaService.setLocationAndSize(label, 0, pos, 2 * margin, margin / 2);
		}
	}
}

Back to the top