-
-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Delete object and move child objects to parent
- Loading branch information
Showing
7 changed files
with
187 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
...himatetool.editor/src/com/archimatetool/editor/diagram/actions/DeleteContainerAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/** | ||
* 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 java.util.ArrayList; | ||
import java.util.List; | ||
|
||
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.AlwaysExecutingCompoundCommand; | ||
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; | ||
import com.archimatetool.model.ILockable; | ||
|
||
|
||
|
||
/** | ||
* 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 keeping children"; | ||
|
||
public DeleteContainerAction(IWorkbenchPart part) { | ||
super(part); | ||
setText(TEXT); | ||
setId(ID); | ||
setActionDefinitionId(ID); // register key binding | ||
} | ||
|
||
@Override | ||
protected boolean calculateEnabled() { | ||
return !getValidObjects().isEmpty(); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
// Execute each command before proceeding to the next in case of multi-selection of embedded containers | ||
CompoundCommand compoundCommand = new AlwaysExecutingCompoundCommand(TEXT) { | ||
@Override | ||
public void execute() { | ||
// Iterate thru child objects to move them to new parent | ||
for(IDiagramModelObject dmo : getValidObjects()) { | ||
IDiagramModelContainer parent = (IDiagramModelContainer)dmo.eContainer(); | ||
List<IDiagramModelObject> children = ((IDiagramModelContainer)dmo).getChildren(); | ||
|
||
for(IDiagramModelObject child : new ArrayList<>(children)) { | ||
// Remove child from parent | ||
Command cmd = new RemoveListMemberCommand<>(children, child); | ||
add(cmd); | ||
cmd.execute(); | ||
|
||
// Adjust x,y position to new parent | ||
// Optimisation - do this *before* adding child to its new parent then this command will not send an additional notification to the EditPart | ||
cmd = new ChangePositionCommand(child, dmo.getBounds()); | ||
add(cmd); | ||
cmd.execute(); | ||
|
||
// Add child to new parent | ||
cmd = new AddListMemberCommand<>(parent.getChildren(), child); | ||
add(cmd); | ||
cmd.execute(); | ||
} | ||
|
||
// Delete target container object | ||
Command cmd = DiagramCommandFactory.createDeleteDiagramObjectNoChildrenCommand(dmo); | ||
add(cmd); | ||
cmd.execute(); | ||
} | ||
} | ||
}; | ||
|
||
execute(compoundCommand); | ||
} | ||
|
||
private List<IDiagramModelObject> getValidObjects() { | ||
List<IDiagramModelObject> 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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters