Skip to content

Commit

Permalink
Delete object and move child objects to parent
Browse files Browse the repository at this point in the history
  • Loading branch information
Phillipus committed Oct 17, 2024
1 parent 881011e commit 5d2e464
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 19 deletions.
6 changes: 6 additions & 0 deletions com.archimatetool.editor/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,12 @@
id="com.archimatetool.editor.action.deleteFromModel"
name="%command.name.20">
</command>
<command
categoryId="org.eclipse.ui.category.edit"
description="Delete parent object but retain child objects"
id="com.archimatetool.editor.action.deleteContainer"
name="Delete and keep child objects">
</command>
<command
categoryId="com.archimatetool.editor.category"
description="%command.description.21"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
import com.archimatetool.editor.diagram.actions.CopyAction;
import com.archimatetool.editor.diagram.actions.CutAction;
import com.archimatetool.editor.diagram.actions.DefaultEditPartSizeAction;
import com.archimatetool.editor.diagram.actions.DeleteContainerAction;
import com.archimatetool.editor.diagram.actions.ExportAsImageAction;
import com.archimatetool.editor.diagram.actions.ExportAsImageToClipboardAction;
import com.archimatetool.editor.diagram.actions.FillColorAction;
Expand Down Expand Up @@ -700,6 +701,11 @@ protected void createActions(GraphicalViewer viewer) {
action.setToolTipText(action.getText());
getUpdateCommandStackActions().add((UpdateAction)action);

// Delete Container
action = new DeleteContainerAction(this);
registry.registerAction(action);
getSelectionActions().add(action.getId());

// Paste
PasteAction pasteAction = new PasteAction(this, viewer);
registry.registerAction(pasteAction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.archimatetool.editor.diagram.actions.LineWidthAction;
import com.archimatetool.editor.diagram.actions.ConnectionRouterAction;
import com.archimatetool.editor.diagram.actions.DefaultEditPartSizeAction;
import com.archimatetool.editor.diagram.actions.DeleteContainerAction;
import com.archimatetool.editor.diagram.actions.ExportAsImageAction;
import com.archimatetool.editor.diagram.actions.ExportAsImageToClipboardAction;
import com.archimatetool.editor.diagram.actions.FillColorAction;
Expand Down Expand Up @@ -75,7 +76,9 @@ public abstract class AbstractDiagramEditorActionBarContributor
protected static final String GROUP_TOOLBAR_END = "group_toolbarEnd"; //$NON-NLS-1$
protected static final String GROUP_POSITION = "group_position"; //$NON-NLS-1$
protected static final String GROUP_CONNECTIONS = "group_connections"; //$NON-NLS-1$

protected static final String GROUP_EDIT_DELETE_MENU = "editDeleteMenuGroup"; //$NON-NLS-1$


@Override
protected void buildActions() {
// Zoom in
Expand Down Expand Up @@ -201,6 +204,11 @@ protected void buildActions() {

// Lock
addRetargetAction(new LabelRetargetAction(LockObjectAction.ID, Messages.AbstractDiagramEditorActionBarContributor_3));

// Delete Container
retargetAction = new RetargetAction(DeleteContainerAction.ID, DeleteContainerAction.TEXT);
retargetAction.setActionDefinitionId(DeleteContainerAction.ID); // key binding
addRetargetAction(retargetAction);
}

@Override
Expand Down Expand Up @@ -331,6 +339,12 @@ protected IMenuManager contributeToEditMenu(IMenuManager menuManager) {
textAlignmentMenu.add(getAction(TextAlignmentAction.ACTION_CENTER_ID));
textAlignmentMenu.add(getAction(TextAlignmentAction.ACTION_RIGHT_ID));
editMenu.appendToGroup(GROUP_EDIT_MENU, textAlignmentMenu);

// Group marker for additional delete actions
editMenu.insertAfter(ArchiActionFactory.DELETE.getId(), new GroupMarker(GROUP_EDIT_DELETE_MENU));

// Delete Container
editMenu.appendToGroup(GROUP_EDIT_DELETE_MENU, getAction(DeleteContainerAction.ID));

return editMenu;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.archimatetool.editor.diagram.actions.BringToFrontAction;
import com.archimatetool.editor.diagram.actions.ConnectionRouterAction;
import com.archimatetool.editor.diagram.actions.DefaultEditPartSizeAction;
import com.archimatetool.editor.diagram.actions.DeleteContainerAction;
import com.archimatetool.editor.diagram.actions.ExportAsImageAction;
import com.archimatetool.editor.diagram.actions.ExportAsImageToClipboardAction;
import com.archimatetool.editor.diagram.actions.LockObjectAction;
Expand Down Expand Up @@ -95,6 +96,9 @@ public void buildContextMenu(IMenuManager menu) {
action = actionRegistry.getAction(ActionFactory.DELETE.getId());
menu.appendToGroup(GROUP_EDIT, action);

// Delete Container
menu.appendToGroup(GROUP_EDIT, actionRegistry.getAction(DeleteContainerAction.ID));

action = actionRegistry.getAction(LockObjectAction.ID);
if(action.isEnabled()) {
menu.appendToGroup(GROUP_EDIT, new Separator());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
package com.archimatetool.editor.diagram;

import org.eclipse.jface.action.GroupMarker;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
Expand All @@ -28,8 +27,6 @@
public class ArchimateDiagramEditorActionBarContributor
extends AbstractDiagramEditorActionBarContributor {

protected static final String editDeleteMenuGroup = "editDeleteMenuGroup"; //$NON-NLS-1$

@Override
protected void buildActions() {
super.buildActions();
Expand Down Expand Up @@ -65,8 +62,8 @@ protected IMenuManager contributeToEditMenu(IMenuManager menuManager) {
}
editMenu.appendToGroup(GROUP_EDIT_MENU, textPositionMenu);

editMenu.insertAfter(ArchiActionFactory.DELETE.getId(), new GroupMarker(editDeleteMenuGroup));
editMenu.appendToGroup(editDeleteMenuGroup, getAction(DeleteFromModelAction.ID));
// Delete from Model
editMenu.appendToGroup(GROUP_EDIT_DELETE_MENU, getAction(DeleteFromModelAction.ID));

return editMenu;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* This program and the accompanying materials
* are made available under the terms of the License
* which accompanies this distribution in the file LICENSE.txt
*/
package com.archimatetool.editor.diagram.actions;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.ui.actions.SelectionAction;
import org.eclipse.ui.IWorkbenchPart;

import com.archimatetool.editor.diagram.commands.DiagramCommandFactory;
import com.archimatetool.editor.model.commands.AddListMemberCommand;
import com.archimatetool.editor.model.commands.NonNotifyingCompoundCommand;
import com.archimatetool.editor.model.commands.RemoveListMemberCommand;
import com.archimatetool.model.IArchimateFactory;
import com.archimatetool.model.IBounds;
import com.archimatetool.model.IDiagramModelContainer;
import com.archimatetool.model.IDiagramModelObject;



/**
* Delete Container object and move children to parent
*
* @author Phillip Beauvoir
*/
public class DeleteContainerAction extends SelectionAction {

public static final String ID = "com.archimatetool.editor.action.deleteContainer"; //$NON-NLS-1$
public static final String TEXT = "Delete from View and keep children";

public DeleteContainerAction(IWorkbenchPart part) {
super(part);
setText(TEXT);
setId(ID);
setActionDefinitionId(ID); // register key binding
}

@Override
protected boolean calculateEnabled() {
for(Object object : getSelectedObjects()) {
if(object instanceof EditPart editPart && editPart.getModel() instanceof IDiagramModelObject) {
return true;
}
}

return false;
}

@Override
public void run() {
CompoundCommand compoundCommand = new NonNotifyingCompoundCommand(TEXT);

for(Object object : getSelectedObjects()) {
if(object instanceof EditPart editPart && editPart.getModel() instanceof IDiagramModelObject dmo) {
if(dmo instanceof IDiagramModelContainer container && dmo.eContainer() instanceof IDiagramModelContainer parent) {
// Iterate thru child objects to move them to new parent
for(IDiagramModelObject child : container.getChildren()) {
// Remove child from parent
compoundCommand.add(new RemoveListMemberCommand<>(container.getChildren(), child));

// Add child to new parent
compoundCommand.add(new AddListMemberCommand<>(parent.getChildren(), child));

// Adjust x,y position to new parent
compoundCommand.add(new ChangePositionCommand(child, dmo.getBounds()));
}

// Delete target container object
compoundCommand.add(DiagramCommandFactory.createDeleteDiagramObjectNoChildrenCommand(dmo));
}
}
}

execute(compoundCommand);
}

private static class ChangePositionCommand extends Command {
private IDiagramModelObject dmo;
private IBounds oldBounds, newBounds;

public ChangePositionCommand(IDiagramModelObject dmo, IBounds parentBounds) {
this.dmo = dmo;
this.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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,24 @@
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);
addDeleteDiagramObjectCommands(object, result, true);
return result.unwrap();
}

/**
* 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 but don't delete child objects
*/
private static void __addDeleteDiagramObjectCommands(IDiagramModelObject object, CompoundCommand result) {
public static Command createDeleteDiagramObjectNoChildrenCommand(IDiagramModelObject object) {
CompoundCommand result = new CompoundCommand();
addDeleteDiagramObjectCommands(object, result, false);
return result.unwrap();
}

private static void addDeleteDiagramObjectCommands(IDiagramModelObject object, CompoundCommand result, boolean recurse) {
result.add(new DeleteDiagramObjectCommand(object));

for(IDiagramModelConnection connection : object.getSourceConnections()) {
Expand All @@ -49,9 +50,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(recurse && object instanceof IDiagramModelContainer container) {
for(IDiagramModelObject child : container.getChildren()) {
addDeleteDiagramObjectCommands(child, result, recurse);
}
}
}
Expand All @@ -62,11 +63,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));
}
Expand Down

0 comments on commit 5d2e464

Please sign in to comment.