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 16, 2024
1 parent 881011e commit 7e043a3
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 13 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"
id="com.archimatetool.editor.action.deleteParentObject"
name="Delete Parent Object">
</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 @@ -22,6 +22,7 @@

import com.archimatetool.editor.ArchiPlugin;
import com.archimatetool.editor.diagram.actions.DeleteFromModelAction;
import com.archimatetool.editor.diagram.actions.DeleteParentObjectAction;
import com.archimatetool.editor.diagram.actions.GenerateViewAction;
import com.archimatetool.editor.diagram.actions.ViewpointAction;
import com.archimatetool.editor.diagram.dnd.ArchimateDiagramTransferDropTargetListener;
Expand Down Expand Up @@ -171,6 +172,11 @@ protected void createActions(GraphicalViewer viewer) {
registry.registerAction(action);
getSelectionActions().add(action.getId());

// Delete Parent Object
action = new DeleteParentObjectAction(this);
registry.registerAction(action);
getSelectionActions().add(action.getId());

// Viewpoints
for(IViewpoint viewPoint : ViewpointManager.INSTANCE.getAllViewpoints()) {
action = new ViewpointAction(this, viewPoint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import com.archimatetool.editor.actions.ArchiActionFactory;
import com.archimatetool.editor.diagram.actions.DeleteFromModelAction;
import com.archimatetool.editor.diagram.actions.DeleteParentObjectAction;
import com.archimatetool.editor.diagram.actions.TextPositionAction;
import com.archimatetool.model.viewpoints.IViewpoint;
import com.archimatetool.model.viewpoints.ViewpointManager;
Expand All @@ -39,6 +40,11 @@ protected void buildActions() {
retargetAction.setActionDefinitionId(DeleteFromModelAction.ID);
addRetargetAction(retargetAction);

// Delete Parent Object
retargetAction = new RetargetAction(DeleteParentObjectAction.ID, DeleteParentObjectAction.TEXT);
retargetAction.setActionDefinitionId(DeleteParentObjectAction.ID);
addRetargetAction(retargetAction);

// Viewpoints
for(IViewpoint viewPoint : ViewpointManager.INSTANCE.getAllViewpoints()) {
retargetAction = new RetargetAction(viewPoint.toString(), viewPoint.getName(), IAction.AS_RADIO_BUTTON);
Expand Down Expand Up @@ -67,6 +73,7 @@ protected IMenuManager contributeToEditMenu(IMenuManager menuManager) {

editMenu.insertAfter(ArchiActionFactory.DELETE.getId(), new GroupMarker(editDeleteMenuGroup));
editMenu.appendToGroup(editDeleteMenuGroup, getAction(DeleteFromModelAction.ID));
editMenu.appendToGroup(editDeleteMenuGroup, getAction(DeleteParentObjectAction.ID));

return editMenu;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import com.archimatetool.editor.actions.ArchiActionFactory;
import com.archimatetool.editor.diagram.actions.DeleteFromModelAction;
import com.archimatetool.editor.diagram.actions.DeleteParentObjectAction;
import com.archimatetool.model.viewpoints.IViewpoint;
import com.archimatetool.model.viewpoints.ViewpointManager;

Expand Down Expand Up @@ -50,6 +51,9 @@ public void buildContextMenu(IMenuManager menu) {
// Delete from Model
menu.appendToGroup(GROUP_EDIT, actionRegistry.getAction(DeleteFromModelAction.ID));

// Delete Parent Object
menu.appendToGroup(GROUP_EDIT, actionRegistry.getAction(DeleteParentObjectAction.ID));

// Generate View For Element
menu.appendToGroup(GROUP_RENAME, actionRegistry.getAction(ArchiActionFactory.GENERATE_VIEW.getId()));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* 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.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.IBounds;
import com.archimatetool.model.IDiagramModelContainer;
import com.archimatetool.model.IDiagramModelObject;



/**
* Delete Parent Object and move children to parent
*
* @author Phillip Beauvoir
*/
public class DeleteParentObjectAction extends SelectionAction {

public static final String ID = "com.archimatetool.editor.action.deleteParentObject"; //$NON-NLS-1$
public static final String TEXT = "Delete Parent Object";

public DeleteParentObjectAction(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 target && dmo.eContainer() instanceof IDiagramModelContainer parent) {
for(IDiagramModelObject child : target.getChildren()) {
compoundCommand.add(new RemoveListMemberCommand<>(target.getChildren(), child));
compoundCommand.add(new AddListMemberCommand<>(parent.getChildren(), child));

IBounds targetBounds = dmo.getBounds();
IBounds childBounds = child.getBounds();

childBounds.setX(childBounds.getX() + targetBounds.getX());
childBounds.setY(childBounds.getY() + targetBounds.getY());
}

compoundCommand.add(DiagramCommandFactory.createDeleteDiagramObjectNoChildrenCommand(dmo));
}
}
}

execute(compoundCommand);
}
}
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 7e043a3

Please sign in to comment.