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
|
/*****************************************************************************
* Copyright (c) 2012 CEA LIST.
*
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
*****************************************************************************/
package org.eclipse.papyrus.uml.diagram.timing.custom.edit.parts;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.draw2d.AbstractLayout;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.common.util.EList;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartViewer;
import org.eclipse.gmf.runtime.diagram.ui.figures.ResizableCompartmentFigure;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.gmfdiag.common.utils.FigureUtils;
import org.eclipse.papyrus.uml.diagram.common.Activator;
import org.eclipse.papyrus.uml.diagram.timing.custom.figures.CompactLifelineFigure;
import org.eclipse.papyrus.uml.diagram.timing.custom.figures.FullLifelineFigure;
import org.eclipse.papyrus.uml.diagram.timing.custom.layouts.FillLayout;
import org.eclipse.papyrus.uml.diagram.timing.custom.utils.Constants;
import org.eclipse.papyrus.uml.diagram.timing.custom.utils.ViewUtils;
import org.eclipse.papyrus.uml.diagram.timing.edit.parts.FullLifelineEditPartCN;
import org.eclipse.papyrus.uml.diagram.timing.edit.parts.InteractionCompartmentEditPartTN;
import org.eclipse.papyrus.uml.diagram.timing.edit.parts.StateDefinitionEditPart;
public class CustomInteractionCompartmentEditPartTN extends InteractionCompartmentEditPartTN {
public CustomInteractionCompartmentEditPartTN(final View view) {
super(view);
}
@Override
public IFigure createFigure() {
final ResizableCompartmentFigure result = (ResizableCompartmentFigure) super.createFigure();
result.setTitleVisibility(false);
result.setBorder(null);
result.setLayoutManager(new FillLayout());
// each lifeline takes a height proportional to its number of states
result.getContentPane().setLayoutManager(new AbstractLayout() {
@Override
public void layout(final IFigure container) {
@SuppressWarnings("unchecked")
final List<IFigure> children = container.getChildren();
final Rectangle clientArea = container.getClientArea();
final Map<IFigure, Integer> numberOfStates = computeNumberOfStates(children);
double totalStates = 0;
for (final Integer nStates : numberOfStates.values()) {
totalStates += Math.max(nStates.intValue(), 1);
}
int y = clientArea.y;
final int totalHeight = clientArea.height - Constants.MARGIN_BETWEEN_LIFELINES * (children.size() - 1);
for (final IFigure child : children) {
final Integer nStates = numberOfStates.get(child);
if (nStates != null) {
final int height = (int) (totalHeight * (Math.max(nStates.doubleValue(), 1) / totalStates));
child.setBounds(new Rectangle(clientArea.x, y, clientArea.width, height));
y += height + Constants.MARGIN_BETWEEN_LIFELINES;
}
}
}
@Override
protected Dimension calculatePreferredSize(final IFigure container, final int wHint, final int hHint) {
return new Dimension(-1, -1);
}
});
return result;
}
protected Map<IFigure, Integer> computeNumberOfStates(final List<IFigure> children) {
final LinkedHashMap<IFigure, Integer> numberOfStatesPerLifeline = new LinkedHashMap<>();
final EditPartViewer viewer = CustomInteractionCompartmentEditPartTN.this.getRoot().getViewer();
for (int i = 0; i < children.size(); i++) {
final IFigure child = children.get(i);
if (FigureUtils.findChildFigureInstance(child, FullLifelineFigure.class) != null) {
@SuppressWarnings("unchecked")
final Map<IFigure, EditPart> visualPartMap = viewer.getVisualPartMap();
final FullLifelineEditPartCN lifelineEditPart = (FullLifelineEditPartCN) visualPartMap.get(child);
final View lifelineView = (View) lifelineEditPart.getModel();
final int nStates = findNumberOfStatesDisplayedIn(lifelineView);
numberOfStatesPerLifeline.put(child, Integer.valueOf(nStates));
} else if (FigureUtils.findChildFigureInstance(child, CompactLifelineFigure.class) != null) {
numberOfStatesPerLifeline.put(child, Integer.valueOf(1));
} else {
Activator.log.warn("Only instances of " + FullLifelineFigure.class.getSimpleName() + " are expected in the compartment " + CustomInteractionCompartmentEditPartTN.class.getSimpleName()); //$NON-NLS-1$//$NON-NLS-2$
continue;
}
}
return numberOfStatesPerLifeline;
}
protected static int findNumberOfStatesDisplayedIn(final View lifelineView) {
int n = 0;
final View stateDefinitionCompartment = ViewUtils.findStateDefinitionCompartmentView(lifelineView);
@SuppressWarnings("unchecked")
final EList<View> children = stateDefinitionCompartment.getChildren();
final String stateDefID = StateDefinitionEditPart.VISUAL_ID;
for (final View view : children) {
if (stateDefID.equals(view.getType())) {
n++;
}
}
return n;
}
@Override
public boolean isSelectable() {
// No need to select the compartment.
// This saves one click when selecting something inside.
return false;
}
}
|