use of org.osate.ge.internal.diagram.runtime.AgeDiagram in project osate2 by osate.
the class AlignCenterHandler method execute.
// This handler allows for alignment of selected diagram elements that are not docked top or bottom.
// Any selected element must not an ancestor of another selected element.
@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
final List<DiagramElement> selectedDiagramElements = AgeHandlerUtil.getSelectedDiagramElements();
final AgeDiagram diagram = UiUtil.getDiagram(selectedDiagramElements);
if (diagram == null) {
throw new RuntimeException("Unable to get diagram");
}
final List<AlignmentElement> alignmentElements = selectedDiagramElements.stream().map(alignmentHelper::createAlignmentElement).collect(Collectors.toList());
diagram.modify("Align Center", m -> {
final AlignmentElement primaryAlignmentElement = AlignmentHelper.getPrimaryAlignmentElement(alignmentElements);
// Location that elements will be aligned at
final double alignLocation = primaryAlignmentElement.getAbsoluteLocation() + primaryAlignmentElement.getDiagramElement().getWidth() / 2;
for (final AlignmentElement alignmentElement : alignmentElements) {
alignmentHelper.alignElement(m, alignmentElement, alignLocation, alignmentElement.getDiagramElement().getWidth() / 2);
}
});
return null;
}
use of org.osate.ge.internal.diagram.runtime.AgeDiagram in project osate2 by osate.
the class AlignMiddleHandler method execute.
// This handler allows for alignment of selected diagram elements that are not docked left or right.
// Any selected element must not an ancestor of another selected element.
@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
final List<DiagramElement> selectedDiagramElements = AgeHandlerUtil.getSelectedDiagramElements();
final AgeDiagram diagram = UiUtil.getDiagram(selectedDiagramElements);
if (diagram == null) {
throw new RuntimeException("Unable to get diagram");
}
final List<AlignmentElement> alignmentElements = selectedDiagramElements.stream().map(alignmentHelper::createAlignmentElement).collect(Collectors.toList());
diagram.modify("Align Middle", m -> {
final AlignmentElement primaryAlignmentElement = AlignmentHelper.getPrimaryAlignmentElement(alignmentElements);
// Location that elements will be aligned at
final double alignLocation = primaryAlignmentElement.getAbsoluteLocation() + primaryAlignmentElement.getDiagramElement().getHeight() / 2;
for (final AlignmentElement alignmentElement : alignmentElements) {
alignmentHelper.alignElement(m, alignmentElement, alignLocation, alignmentElement.getDiagramElement().getHeight() / 2);
}
});
return null;
}
use of org.osate.ge.internal.diagram.runtime.AgeDiagram in project osate2 by osate.
the class ArrangeInCircleHandler method execute.
@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
final List<DiagramElement> selectedDiagramElements = AgeHandlerUtil.getSelectedDiagramElements();
final AgeDiagram diagram = UiUtil.getDiagram(selectedDiagramElements);
if (diagram == null) {
throw new RuntimeException("Unable to get diagram");
}
diagram.modify("Arrange in Circle", m -> {
// Calculate and sets the position of the selected shapes into a radial layout.
final List<Double> angles = new ArrayList<>();
final double padding = 5;
double radius = 0;
double sizeOfShapes = 0;
double circumference = -1;
// Find appropriate radius for shapes
while ((sizeOfShapes > circumference) || (Double.isNaN(sizeOfShapes))) {
sizeOfShapes = 0;
angles.clear();
radius++;
circumference = 2 * Math.PI * radius;
for (final DiagramElement e1 : selectedDiagramElements) {
// Get largest dimension of shape
final double shapeDimension = ((e1.getWidth() > e1.getHeight() ? e1.getWidth() : e1.getHeight()) + padding) / 2;
// Calculate diagonal of shape
final double shapeSize = Math.sqrt((Math.pow(shapeDimension, 2) + Math.pow(shapeDimension, 2)));
// Calculate angle for shape diagonal using Law of Cosines and double it
final double angle = Math.acos((Math.pow(radius, 2) + Math.pow(radius, 2) - Math.pow(shapeSize, 2)) / (2 * radius * radius)) * 2;
angles.add(angle);
sizeOfShapes += (circumference * (angle / (2 * Math.PI)));
}
}
final Point center = getCenter(selectedDiagramElements, radius);
double placementAngle = 0;
int i = 0;
for (final DiagramElement e2 : selectedDiagramElements) {
double spacingAngle = angles.get(i++);
// Center of placement
placementAngle += spacingAngle / 2;
// Calculate where to place shape
final long x = Math.round(center.x + radius * Math.cos(placementAngle) - e2.getWidth() / 2);
final long y = Math.round(center.y + radius * Math.sin(placementAngle) - e2.getHeight() / 2);
// End of placement
placementAngle += spacingAngle / 2;
DiagramElementLayoutUtil.moveElement(m, e2, new Point((int) x, (int) y));
}
});
return null;
}
use of org.osate.ge.internal.diagram.runtime.AgeDiagram in project osate2 by osate.
the class ArrangeInGridHandler method execute.
@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
final List<DiagramElement> selectedDiagramElements = AgeHandlerUtil.getSelectedDiagramElements();
final AgeDiagram diagram = UiUtil.getDiagram(selectedDiagramElements);
if (diagram == null) {
throw new RuntimeException("Unable to get diagram");
}
diagram.modify("Arrange in Grid", m -> {
// Calculate and sets the selected diagram elements into a grid layout.
final int padding = 25;
final int numOfCols = (int) Math.ceil(Math.sqrt(selectedDiagramElements.size()));
final int numOfRows = (int) Math.ceil(selectedDiagramElements.size() / (double) numOfCols);
final double[] colSizes = new double[numOfCols - 1];
final double[] rowSizes = new double[numOfRows - 1];
// Assign column and row width for grid
for (int i1 = 0; i1 < selectedDiagramElements.size(); i1++) {
final DiagramElement de = selectedDiagramElements.get(i1);
int col = i1 % numOfCols;
int row = i1 / numOfCols;
if (col < colSizes.length) {
colSizes[col] = Math.max(colSizes[col], de.getWidth() + padding);
}
if (row < rowSizes.length) {
rowSizes[row] = Math.max(rowSizes[row], de.getHeight() + padding);
}
}
// Calculate grid position and place first shape
final Point gridPosition = getGridPosition(selectedDiagramElements);
double x = gridPosition.x;
double y = gridPosition.y;
// Position the elements
DiagramElementLayoutUtil.moveElement(m, selectedDiagramElements.get(0), new Point(x, y));
// Place the rest of the shapes
for (int i2 = 1; i2 < selectedDiagramElements.size(); i2++) {
if (i2 % numOfCols == 0) {
x = gridPosition.x;
if (rowSizes.length != 0) {
y += rowSizes[i2 / numOfCols - 1];
}
} else {
x += colSizes[i2 % numOfCols - 1];
}
DiagramElementLayoutUtil.moveElement(m, selectedDiagramElements.get(i2), new Point(x, y));
}
});
return null;
}
use of org.osate.ge.internal.diagram.runtime.AgeDiagram in project osate2 by osate.
the class ShowContentsUtil method addContentsToSelectedElements.
/**
* Adds contents to the selected diagram elements. Adds all children which pass the specified filter.
* @param event is the ExecutionEvent of the handler which provides the active editor.
* @param filters determines the filters to apply to the selected elements.
*/
public static void addContentsToSelectedElements(final ExecutionEvent event, final Function<DiagramElement, ImmutableSet<ContentFilter>> filters) {
final IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
if (!(activeEditor instanceof InternalDiagramEditor)) {
throw new RuntimeException("Unexpected editor: " + activeEditor);
}
final InternalDiagramEditor diagramEditor = (InternalDiagramEditor) activeEditor;
final ExtensionRegistryService extService = Objects.requireNonNull(Adapters.adapt(diagramEditor, ExtensionRegistryService.class), "Unable to retrieve extension service");
final List<DiagramElement> selectedDiagramElements = AgeHandlerUtil.getSelectedDiagramElements();
final AgeDiagram diagram = diagramEditor.getDiagram();
if (diagram == null) {
throw new RuntimeException("Unable to retrieve diagram");
}
final DiagramUpdater diagramUpdater = Objects.requireNonNull(diagramEditor.getDiagramUpdater(), "Unable to retrieve diagram updater");
final ReferenceBuilderService referenceBuilder = Objects.requireNonNull(Adapters.adapt(diagramEditor, ReferenceBuilderService.class), "Unable to retrieve reference builder service");
if (addChildrenDuringNextUpdate(selectedDiagramElements, diagramUpdater, extService, referenceBuilder, filters)) {
diagramEditor.getActionExecutor().execute("Show Contents", ExecutionMode.NORMAL, () -> {
diagramEditor.updateDiagram();
return null;
});
}
}
Aggregations