Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 78825d3ace2774020ec0959a94da9f6915595908 (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
/*******************************************************************************
 * 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.fsm.fSM.State;
import org.eclipse.etrice.ui.behavior.fsm.support.TrPointSupport;
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 Behavior Editor.
 * 
 * @author jayant
 */
public class BehaviorLayoutCommand extends ETriceLayoutCommand {

	public BehaviorLayoutCommand(TransactionalEditingDomain domain,
			IFeatureProvider thefeatureProvider) {
		super(domain, thefeatureProvider);
		// TODO Auto-generated constructor stub
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @author jayant
	 */
	@Override
	protected void applyNodeLayout(KNode knode, PictogramElement pelem) {
		setCalculatedPositionAndSize(knode, knode.getParent(),
				(ContainerShape) pelem);
	};

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

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

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

		GraphicsAlgorithm ga = shape.getGraphicsAlgorithm();
		EObject bo = shape.getContainer().getLink().getBusinessObjects().get(0);

		// margin and size for bounding box (State Graph)
		int margin = TrPointSupport.MARGIN;
		int size = TrPointSupport.ITEM_SIZE;

		if (bo instanceof State) {
			// margin and size for a State
			margin = TrPointSupport.MARGIN_SMALL;
			size = TrPointSupport.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 TrPointSuppot.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 halign = Orientation.ALIGNMENT_CENTER;
		Orientation valign = Orientation.ALIGNMENT_CENTER;

		int pos = 0;

		if (x <= margin)
			halign = Orientation.ALIGNMENT_LEFT;
		else if ((width - margin) <= x)
			halign = Orientation.ALIGNMENT_RIGHT;
		if (y <= margin) {
			pos = 0;
			valign = Orientation.ALIGNMENT_BOTTOM;
		} else {
			pos = 5 * margin / 4;
			valign = Orientation.ALIGNMENT_TOP;
		}

		label.setHorizontalAlignment(halign);
		label.setVerticalAlignment(valign);

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

}

Back to the top