From 26b3675a6344753e9e46c8f27751fd456a2b1e99 Mon Sep 17 00:00:00 2001 From: Phillipus Date: Thu, 21 Nov 2024 08:54:17 +0000 Subject: [PATCH] Delete object and move child objects to parent --- com.archimatetool.editor/plugin.xml | 6 + .../editor/actions/ArchiActionBarAdvisor.java | 1 + .../editor/diagram/AbstractDiagramEditor.java | 6 + ...ractDiagramEditorActionBarContributor.java | 17 ++- ...tractDiagramEditorContextMenuProvider.java | 6 + ...mateDiagramEditorActionBarContributor.java | 7 +- .../actions/DeleteContainerAction.java | 140 ++++++++++++++++++ .../editor/diagram/actions/Messages.java | 4 + .../diagram/actions/messages.properties | 2 + .../commands/DiagramCommandFactory.java | 29 ++-- .../help/Text/view_copy_paste_delete.html | 17 ++- 11 files changed, 208 insertions(+), 27 deletions(-) create mode 100644 com.archimatetool.editor/src/com/archimatetool/editor/diagram/actions/DeleteContainerAction.java diff --git a/com.archimatetool.editor/plugin.xml b/com.archimatetool.editor/plugin.xml index cbfd28d39..840e8e185 100644 --- a/com.archimatetool.editor/plugin.xml +++ b/com.archimatetool.editor/plugin.xml @@ -958,6 +958,12 @@ id="com.archimatetool.editor.action.deleteFromModel" name="%command.name.20"> + + children = ((IDiagramModelContainer)dmo).getChildren(); + + // Delete main object + add(DiagramCommandFactory.createDeleteDiagramObjectCommand(dmo, false)); + + // Iterate thru child objects to move them to new parent + for(IDiagramModelObject child : new ArrayList<>(children)) { + // Remove child from main object + add(new RemoveListMemberCommand<>(children, child)); + + // Adjust x,y position to new parent + add(new ChangePositionCommand(child, dmo.getBounds())); + + // Add child to new parent + add(new AddListMemberCommand<>(parent.getChildren(), child)); + } + } + } + + @Override + public void add(Command command) { + super.add(command); + command.execute(); // execute command + } + }; + + execute(compoundCommand); + } + + private List getObjectsToBeDeleted() { + List list = new ArrayList<>(); + + for(Object object : getSelectedObjects()) { + if(isValidObject(object)) { + list.add((IDiagramModelObject)((EditPart)object).getModel()); + } + } + + return list; + } + + private boolean isValidObject(Object object) { + return object instanceof EditPart editPart && // Is an EditPart + editPart.getModel() instanceof IDiagramModelObject dmo && // Diagram Model Object + dmo instanceof IDiagramModelContainer container && // And a container + dmo.eContainer() instanceof IDiagramModelContainer && // And parent is a container + !(dmo instanceof ILockable lockable && lockable.isLocked()) && // And not locked + !container.getChildren().isEmpty(); // And has child objects + } + + private static class ChangePositionCommand extends Command { + private IDiagramModelObject dmo; + private IBounds oldBounds, newBounds; + + public ChangePositionCommand(IDiagramModelObject dmo, IBounds parentBounds) { + this.dmo = dmo; + oldBounds = dmo.getBounds(); + newBounds = IArchimateFactory.eINSTANCE.createBounds(oldBounds.getX() + parentBounds.getX(), + oldBounds.getY() + parentBounds.getY(), oldBounds.getWidth(), oldBounds.getHeight()); + } + + @Override + public void execute() { + dmo.setBounds(newBounds); + } + + @Override + public void undo() { + dmo.setBounds(oldBounds); + } + + @Override + public void dispose() { + dmo = null; + oldBounds = null; + newBounds = null; + } + } +} diff --git a/com.archimatetool.editor/src/com/archimatetool/editor/diagram/actions/Messages.java b/com.archimatetool.editor/src/com/archimatetool/editor/diagram/actions/Messages.java index b3519f74f..ca7dcbe92 100644 --- a/com.archimatetool.editor/src/com/archimatetool/editor/diagram/actions/Messages.java +++ b/com.archimatetool.editor/src/com/archimatetool/editor/diagram/actions/Messages.java @@ -39,6 +39,10 @@ public class Messages extends NLS { public static String DefaultEditPartSizeAction_1; + public static String DeleteContainerAction_0; + + public static String DeleteContainerAction_1; + public static String DeleteFromModelAction_0; public static String DeleteFromModelAction_1; diff --git a/com.archimatetool.editor/src/com/archimatetool/editor/diagram/actions/messages.properties b/com.archimatetool.editor/src/com/archimatetool/editor/diagram/actions/messages.properties index fa853b7f3..a8fbd94b7 100644 --- a/com.archimatetool.editor/src/com/archimatetool/editor/diagram/actions/messages.properties +++ b/com.archimatetool.editor/src/com/archimatetool/editor/diagram/actions/messages.properties @@ -20,6 +20,8 @@ CutAction_1=Cut DefaultEditPartSizeAction_0=De&fault Size DefaultEditPartSizeAction_1=Set the default size of this component +DeleteContainerAction_0=Delete from View (keep children) +DeleteContainerAction_1=Delete from View keeping children DeleteFromModelAction_0=Delete from Model DeleteFromModelAction_1=One or more of the elements you have selected is referenced elsewhere in a View. DeleteFromModelAction_2=Are you sure you want to delete the selected element(s)? diff --git a/com.archimatetool.editor/src/com/archimatetool/editor/diagram/commands/DiagramCommandFactory.java b/com.archimatetool.editor/src/com/archimatetool/editor/diagram/commands/DiagramCommandFactory.java index 20dc72a67..d8d3044e3 100644 --- a/com.archimatetool.editor/src/com/archimatetool/editor/diagram/commands/DiagramCommandFactory.java +++ b/com.archimatetool.editor/src/com/archimatetool/editor/diagram/commands/DiagramCommandFactory.java @@ -22,23 +22,22 @@ public final class DiagramCommandFactory { /** - * @param object * @return A new Delete Diagram Object Command */ public static Command createDeleteDiagramObjectCommand(IDiagramModelObject object) { - CompoundCommand result = new CompoundCommand(); - __addDeleteDiagramObjectCommands(object, result); - return result.unwrap(); + return createDeleteDiagramObjectCommand(object, true); } /** - * Recurse and add child delete commands. - * We have to do this because if the object has children with connections going outside these need explicit Delete Commands too - * otherwise we end up with trailing connections... - * @param container - * @param result + * @return A new Delete Diagram Object Command with option of deleting child objects */ - private static void __addDeleteDiagramObjectCommands(IDiagramModelObject object, CompoundCommand result) { + public static Command createDeleteDiagramObjectCommand(IDiagramModelObject object, boolean deleteChildren) { + CompoundCommand result = new CompoundCommand(); + addDeleteDiagramObjectCommands(object, result, deleteChildren); + return result.unwrap(); + } + + private static void addDeleteDiagramObjectCommands(IDiagramModelObject object, CompoundCommand result, boolean deleteChildren) { result.add(new DeleteDiagramObjectCommand(object)); for(IDiagramModelConnection connection : object.getSourceConnections()) { @@ -49,9 +48,9 @@ private static void __addDeleteDiagramObjectCommands(IDiagramModelObject object, result.add(createDeleteDiagramConnectionCommand(connection)); } - if(object instanceof IDiagramModelContainer) { - for(IDiagramModelObject child : ((IDiagramModelContainer)object).getChildren()) { - __addDeleteDiagramObjectCommands(child, result); + if(deleteChildren && object instanceof IDiagramModelContainer container) { + for(IDiagramModelObject child : container.getChildren()) { + addDeleteDiagramObjectCommands(child, result, deleteChildren); } } } @@ -62,11 +61,11 @@ private static void __addDeleteDiagramObjectCommands(IDiagramModelObject object, */ public static Command createDeleteDiagramConnectionCommand(IDiagramModelConnection connection) { CompoundCommand result = new CompoundCommand(); - __addDeleteDiagramConnectionCommands(connection, result); + addDeleteDiagramConnectionCommands(connection, result); return result.unwrap(); } - private static void __addDeleteDiagramConnectionCommands(IDiagramModelConnection connection, CompoundCommand result) { + private static void addDeleteDiagramConnectionCommands(IDiagramModelConnection connection, CompoundCommand result) { for(IDiagramModelConnection conn : connection.getSourceConnections()) { result.add(createDeleteDiagramConnectionCommand(conn)); } diff --git a/com.archimatetool.help/help/Text/view_copy_paste_delete.html b/com.archimatetool.help/help/Text/view_copy_paste_delete.html index 1832e72d5..c1be1daa6 100644 --- a/com.archimatetool.help/help/Text/view_copy_paste_delete.html +++ b/com.archimatetool.help/help/Text/view_copy_paste_delete.html @@ -39,7 +39,17 @@

Copy As Image to Clipboard

Deleting Elements and Relationships (Connections) in a View

-

Selected elements and/or connections in a View can be deleted from the View by choosing the "Delete from View" menu item from the main "Edit" menu, from the main toolbar or from the right-click menu. Note - this action deletes those elements from the View not from the model. To delete the element completely you have to delete it in the Model Tree or select "Delete from Model".

+

Selected elements and/or connections in a View can be deleted from the View by choosing the "Delete from View" menu item from the main "Edit" menu, from the main toolbar or from the right-click menu. Note - this action deletes the selected objects from the View not from the model.

+ +
+ +

Delete from View (keep children)

+

This menu item is available from the main "Edit" menu or when right-clicking an object in a View. It allows you to delete the selected objects but retain the object's child objects. Child objects are re-parented to the selected object's immediate parent. Note - this action deletes the selected objects from the View not from the model.

+ +
+ +

Delete from Model

+

This menu item is available from the main "Edit" menu or when right-clicking an element or relationship in a View. The selected elements and/or relationships are deleted from the model itself and any Views that reference those elements. This is the equivalent of selecting the elements in the Model Tree and choosing "Delete".


@@ -52,11 +62,6 @@

Changing an Object's ArchiMate Concept Type in a View

Select in Model Tree

This menu item is available when right-clicking an element or relationship in a View. It will select the corresponding model element in the Model Tree.

-
- -

Delete from Model

-

This menu item is available when right-clicking an element or relationship in a View. The selected elements and/or relationships are then deleted from the model itself and any Views that reference those elements. This is the equivalent of selecting the elements in the Model Tree and choosing "Delete".

-