use of org.osate.ge.graphics.Point in project osate2 by osate.
the class CopyAction method run.
@Override
public void run() {
final List<CopiedDiagramElement> copiedElements = new ArrayList<>();
for (final DiagramElement de : getElementsToCopy()) {
final DiagramElement copiedElement = CopyAndPasteUtil.copyDiagramElement(de, null, de.getRelativeReference(), referenceBuilder);
final Object bo = de.getBusinessObject();
final Point position = CopyAndPasteUtil.getPositionToCopy(de);
if (bo instanceof EObject) {
final EObject boEObj = (EObject) de.getBusinessObject();
copiedElements.add(new CopiedDiagramElement(copiedElement, de.getBusinessObject(), EcoreUtil.copy(boEObj), boEObj.eContainingFeature(), position));
} else if (bo instanceof EmbeddedBusinessObject) {
// Don't need to copy object again because it was copied as part of copying the diagram element.
copiedElements.add(new CopiedDiagramElement(copiedElement, de.getBusinessObject(), copiedElement.getBusinessObject(), null, position));
} else {
throw new RuntimeException("Unsupported case: " + bo);
}
}
clipboard.setContents(new CopiedDiagramElements(ImmutableList.copyOf(copiedElements)));
}
use of org.osate.ge.graphics.Point in project osate2 by osate.
the class PasteAction method copyClipboardContents.
/**
* Copies the clipboard contents to the destination business object and diagram element.
* @return the diagram elements which were created. Does not include children of created diagram elements.
*/
private Collection<DiagramElement> copyClipboardContents(final EObject dstBoToModify, final DiagramNode dstDiagramNode, final DiagramModification m, final ReferenceBuilderService refBuilder) {
// Determine the minimum coordinates from the elements whose positions will be copied
// The minimum coordinates is null if none of the copied diagram elements have an absolute position. This is reasonable because the minimum coordinates
// are only needed if a copied diagram element has an absolute position.
final Point minCoordinates = getCopiedDiagramElements().stream().map(CopiedDiagramElement::getAbsolutePosition).filter(Predicates.notNull()).reduce((a, b) -> new Point(Math.min(a.x, b.x), Math.min(a.y, b.y))).orElse(null);
// This list will contain the diagram elements that are created by the copying process. Does not contain their children.
final List<DiagramElement> newDiagramElements = new ArrayList<>();
// Copy each copied diagram element into the diagram and model.
for (final CopiedDiagramElement copiedDiagramElement : getCopiedDiagramElements()) {
final DiagramElement newDiagramElement;
if (copiedDiagramElement.getCopiedBusinessObject() == null) {
newDiagramElement = CopyAndPasteUtil.copyDiagramElement(copiedDiagramElement.getDiagramElement(), dstDiagramNode, copiedDiagramElement.getDiagramElement().getRelativeReference(), refBuilder);
} else {
final Object boFromCopiedDiagramElement = copiedDiagramElement.getCopiedBusinessObject();
final RelativeBusinessObjectReference newRelativeRef;
if (boFromCopiedDiagramElement instanceof EmbeddedBusinessObject) {
// The relative reference will be assigned when copying the diagram element.
newRelativeRef = null;
} else if (boFromCopiedDiagramElement instanceof EObject) {
// Get the list that to which the copied object will be added
final EStructuralFeature compatibleFeature = getCompatibleStructuralFeature(copiedDiagramElement.getContainingFeature(), dstBoToModify.eClass());
final Object containingFeatureValue = dstBoToModify.eGet(compatibleFeature);
if (!(containingFeatureValue instanceof Collection)) {
throw new RuntimeException("Unexpected case. Value of containing feature was not a collection");
}
@SuppressWarnings("unchecked") final Collection<EObject> containingFeatureValueCollection = (Collection<EObject>) containingFeatureValue;
final EObject copiedEObject = EcoreUtil.copy((EObject) boFromCopiedDiagramElement);
containingFeatureValueCollection.add(copiedEObject);
ensureBusinessObjectHasUniqueName(copiedEObject, copiedDiagramElement.getDiagramElement().getBusinessObjectHandler());
ensurePackagesAreImported(copiedEObject);
newRelativeRef = refBuilder.getRelativeReference(copiedEObject);
} else {
throw new RuntimeException("Unsupported case: " + boFromCopiedDiagramElement);
}
newDiagramElement = CopyAndPasteUtil.copyDiagramElement(copiedDiagramElement.getDiagramElement(), dstDiagramNode, newRelativeRef, refBuilder);
}
// Set the position of the new diagram element. They are positioned relative to each other at a fixed offset within the new parent.
final Point cp = copiedDiagramElement.getAbsolutePosition();
final Point newPosition = cp == null ? null : new Point(cp.x - minCoordinates.x + 50, cp.y - minCoordinates.y + 50);
DiagramElementLayoutUtil.moveElement(m, newDiagramElement, newPosition);
// Remove existing element
final DiagramElement existingDiagramElement = dstDiagramNode.getChildByRelativeReference(newDiagramElement.getRelativeReference());
if (existingDiagramElement != null) {
m.removeElement(existingDiagramElement);
}
// Add the new diagram element to the diagram.
m.addElement(newDiagramElement);
newDiagramElements.add(newDiagramElement);
}
return newDiagramElements;
}
use of org.osate.ge.graphics.Point in project osate2 by osate.
the class CopyDiagramElementsHandler method execute.
@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
// Get clipboard. The clipboard service stores the clipboard weakly but this handler does not need to
// because the paste action stores a strong reference to the clipboard. This isn't stored in a field by
// the constructor because the handler is not instantiated for each editor.
final Bundle bundle = FrameworkUtil.getBundle(getClass());
final IEclipseContext context = EclipseContextFactory.getServiceContext(bundle.getBundleContext());
final Clipboard clipboard = Objects.requireNonNull(context.get(ClipboardService.class), "Unable to get clipboard service").getClipboard();
final ReferenceBuilderService refBuilder = Objects.requireNonNull(context.get(ReferenceBuilderService.class), "Unable to get reference builder service");
final List<CopiedDiagramElement> copiedElements = new ArrayList<>();
for (final DiagramElement de : AgeHandlerUtil.getSelectedDiagramElements()) {
final DiagramElement copiedElement = CopyAndPasteUtil.copyDiagramElement(de, null, de.getRelativeReference(), refBuilder);
final Point position = CopyAndPasteUtil.getPositionToCopy(de);
copiedElements.add(new CopiedDiagramElement(copiedElement, de.getBusinessObject(), position));
}
clipboard.setContents(new CopiedDiagramElements(ImmutableList.copyOf(copiedElements)));
return null;
}
use of org.osate.ge.graphics.Point in project osate2 by osate.
the class ArrangeInCircleHandler method getCenter.
private static Point getCenter(final List<DiagramElement> diagramElements, final double radius) {
final int boarderPadding = 10;
double xMin = Double.MAX_VALUE, xMax = Double.MIN_VALUE, yMin = Double.MAX_VALUE, yMax = Double.MIN_VALUE;
double maxWidth = Double.MIN_VALUE, maxHeight = Double.MIN_VALUE;
// Get max dimension of shapes
for (final DiagramElement diagramElement : diagramElements) {
final double width = diagramElement.getWidth() / 2;
final double height = diagramElement.getHeight() / 2;
final double x = diagramElement.getX() + width;
final double y = diagramElement.getY() + height;
if (x < xMin) {
xMin = x;
}
if (x > xMax) {
xMax = x;
}
if (y < yMin) {
yMin = y;
}
if (y > yMax) {
yMax = y;
}
if (maxWidth < width) {
maxWidth = width;
}
if (maxHeight < height) {
maxHeight = height;
}
}
// Calculate center point
double xCenter = (xMin + xMax) / 2;
double yCenter = (yMax + yMin) / 2;
// Move center point to appropriate location if necessary
if (xCenter - radius - maxWidth < 0) {
xCenter += Math.abs(xCenter - radius - maxWidth) + boarderPadding;
}
if (yCenter - radius - maxHeight < 0) {
yCenter += Math.abs(yCenter - radius - maxHeight) + boarderPadding;
}
return new Point(xCenter, yCenter);
}
use of org.osate.ge.graphics.Point 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;
}
Aggregations