-
-
Notifications
You must be signed in to change notification settings - Fork 270
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
9 changed files
with
202 additions
and
27 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
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 (keep children)"; | ||
public static final String TOOLTIP_TEXT = "Delete from View keeping children"; | ||
|
||
public DeleteContainerAction(IWorkbenchPart part) { | ||
super(part); | ||
setText(TEXT); | ||
setToolTipText(TOOLTIP_TEXT); | ||
setId(ID); | ||
setActionDefinitionId(ID); // register key binding | ||
} | ||
|
||
@Override | ||
protected boolean calculateEnabled() { | ||
return !getObjectsToBeDeleted().isEmpty(); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
// Execute each command before proceeding to the next in case of multi-selection of embedded containers | ||
// Use a AlwaysExecutingCompoundCommand to ensure redo/undo always execute because cmd#canExecute may return false | ||
CompoundCommand compoundCommand = new AlwaysExecutingCompoundCommand(TEXT) { | ||
@Override | ||
public void execute() { | ||
for(IDiagramModelObject dmo : getObjectsToBeDeleted()) { | ||
IDiagramModelContainer parent = (IDiagramModelContainer)dmo.eContainer(); | ||
List<IDiagramModelObject> 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<IDiagramModelObject> getObjectsToBeDeleted() { | ||
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
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