Download this file

ArchiLayoutUtils.java    128 lines (105 with data), 5.4 kB

  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
package net.timbusproject.extractors.modules.tavernaextractor.utils;
import java.awt.Dimension;
import java.awt.Point;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.timbusproject.extractors.modules.tavernaextractor.utils.Container.Orientation;
import uk.ac.bolton.archimate.model.IArchimateDiagramModel;
import uk.ac.bolton.archimate.model.IArchimateElement;
import uk.ac.bolton.archimate.model.IArchimateFactory;
import uk.ac.bolton.archimate.model.IBounds;
import uk.ac.bolton.archimate.model.IDiagramModelArchimateConnection;
import uk.ac.bolton.archimate.model.IDiagramModelArchimateObject;
import uk.ac.bolton.archimate.model.IDiagramModelContainer;
import uk.ac.bolton.archimate.model.IDiagramModelGroup;
import uk.ac.bolton.archimate.model.IDiagramModelObject;
import uk.ac.bolton.archimate.model.IRelationship;
import uk.ac.bolton.archimate.model.impl.DiagramModelObject;
public class ArchiLayoutUtils {
public static final int DEFAULT_ELEMENT_HEIGHT = 55; // default height used by Archi as well
public static final int DEFAULT_ELEMENT_WIDTH = 55; // default width used by Archi as well
public static final int CUSTOM_BUSINESS_PROCESS_WIDTH = 100; // default width used by Archi as well
public static int getLowerEdgePosition(IDiagramModelGroup group_businessObjects) {
return group_businessObjects.getBounds().getY() + group_businessObjects.getBounds().getHeight();
}
public static int getRightEdgePosition(IDiagramModelGroup group_businessObjects) {
return group_businessObjects.getBounds().getX() + group_businessObjects.getBounds().getWidth();
}
public static IDiagramModelGroup createSubModelGroup(IArchimateDiagramModel diagramModel, String name) {
IDiagramModelGroup subModelGroup = IArchimateFactory.eINSTANCE.createDiagramModelGroup();
subModelGroup.setName(name);
diagramModel.getChildren().add(subModelGroup);
return subModelGroup;
}
public static IDiagramModelGroup createSubModelGroup(IDiagramModelGroup diagramModel, String name) {
IDiagramModelGroup subModelGroup = IArchimateFactory.eINSTANCE.createDiagramModelGroup();
subModelGroup.setName(name);
diagramModel.getChildren().add(subModelGroup);
return subModelGroup;
}
public static int[] boundsOf(IDiagramModelGroup group) {
if (group != null) {
IBounds bounds = group.getBounds();
return new int[] { bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight() };
}
return new int[] { 0, 0, 0, 0 };
}
public static void createConnection(IArchimateFactory factory, IRelationship relation,
IDiagramModelArchimateObject sourceDiagramObject, IDiagramModelArchimateObject targetDiagramObject) {
IDiagramModelArchimateConnection diagramModelConnection = factory.createDiagramModelArchimateConnection();
diagramModelConnection.setRelationship(relation);
diagramModelConnection.connect(sourceDiagramObject, targetDiagramObject);
}
public static Map<String, int[]> getBoundsForElements(List<IArchimateElement> elements, Orientation orientation) {
final int MAX_ITEMS_PER_ROW = 6;
Map<String, int[]> map = new HashMap<String, int[]>();
int count = 0;
if (orientation == Orientation.HORIZONTAL) {
int current_y = 10;
int current_x = 10;
for (IArchimateElement element : elements) {
final Dimension elementSize = getElementSize(element);
if (count >= MAX_ITEMS_PER_ROW) {
current_x = 10;
current_y += elementSize.height;
count = 0;
}
map.put(element.getId(), new int[] { current_x, current_y, elementSize.width, elementSize.height });
current_x += elementSize.width + 10;
count++;
}
} else {
for (IArchimateElement element : elements) {
Dimension elementSize = getElementSize(element);
map.put(element.getId(), new int[] { 10, 10 + count, elementSize.width, elementSize.height });
count += elementSize.height;
}
}
return map;
}
// calculates min X,Y size for an element via length of element's name
public static Dimension getElementSize(IArchimateElement element) {
int width = element.getName().length() <= 10 ? 100 : 8 * element.getName().length();
return new Dimension(width, DEFAULT_ELEMENT_HEIGHT);
}
public static void resize(IDiagramModelContainer container) {
Point max = new Point(-1, -1);
for (IDiagramModelObject item : container.getChildren()) {
IBounds bounds = item.getBounds();
int x = bounds.getX() + bounds.getWidth();
int y = bounds.getY() + bounds.getHeight();
Point current = new Point(x, y);
if (current.x >= max.x) {
max.x = current.x;
}
if (current.y >= max.y) {
max.y = current.y;
}
}
IBounds bounds = ((DiagramModelObject) container).getBounds();
bounds.setWidth(max.x + 30);
bounds.setHeight(max.y + 30);
((DiagramModelObject) container).setBounds(bounds);
}
}