blob: 7b566888380fe66b538826116fc381c0f84a6123 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2004-2008 Istvan Rath and Daniel Varro
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Istvan Rath - initial API and implementation
*******************************************************************************/
package org.eclipse.viatra2.treeeditor.commands;
import org.eclipse.viatra2.core.EDeleteSemantics;
import org.eclipse.viatra2.core.IEntity;
import org.eclipse.viatra2.core.IModelElement;
import org.eclipse.viatra2.core.IRelation;
import org.eclipse.viatra2.errors.VPMCoreException;
public class DeleteModelElementCommand extends ViatraEditorCommand {
private IEntity parent;
private String parent_name;
private String child_name;
private IEntity child;
private IModelElement source;
private String source_name;
private String relation_name;
private IRelation relation;
// private ViatraTreeEditor iVTE;
public DeleteModelElementCommand() { // ViatraTreeEditor vte) {
// iVTE = vte;
}
public void setEntity(IEntity aChld) {
child = aChld;
child_name = child.getFullyQualifiedName();
parent = child.getParent();
parent_name = parent.getFullyQualifiedName();
relation_name = null;
relation = null;
source = null;
source_name = null;
}
public void setRelation(IRelation aRel) {
relation = aRel;
relation_name = relation.getFullyQualifiedName();
source = relation.getFrom();
source_name = source.getFullyQualifiedName();
parent = null;
parent_name = null;
child = null;
child_name = null;
}
private void recursive_delete(IEntity target) {
// delete all children recursively
while (!target.getContents().isEmpty()) {
try {
recursive_delete((IEntity) target.getContents().iterator()
.next());
} catch (ClassCastException e) {
System.out.println("Non-entity content detected at "
+ target.getFullyQualifiedName());
}
}
try {
target.getModelSpace().getModelManager().deleteEntity(target,
EDeleteSemantics.DELETE_SEMANTICS_FORCE);
} catch (VPMCoreException e) {
e.printStackTrace();
}
}
public void execute() throws VPMCoreException {
parent = (IEntity) Lookup(parent, parent_name);
child = (IEntity) Lookup(child, child_name);
source = Lookup(source, source_name);
relation = (IRelation) Lookup(relation, relation_name);
// iVTE.setIsNotificationEnabled(false);
if (parent != null && child != null)
{
// we're deleting an entity
iTransactionID = child.getModelSpace().getTransactionManager().beginUndoableTransaction(Boolean.TRUE);
//iTransactionID = child.getModelSpace().getTransactionManager().beginTransaction();
// child.getModelSpace().getUndoManager().nextUndoBlock("vte_delent");
// child.getModelSpace().getModelManager().deleteEntity(child,IModelManager.DELETE_SEMANTICS_FORCE);
try {
recursive_delete(child);
parent.getModelSpace().getTransactionManager().commitTransaction();
} catch (Exception e) {
e.printStackTrace();
parent.getModelSpace().getTransactionManager().abortTransaction();
}
// finally {
// }
}
else if (source != null && relation != null)
{
// we're deleting a relation
iTransactionID = relation.getModelSpace().getTransactionManager().beginUndoableTransaction(Boolean.TRUE);
//relation.getModelSpace().getUndoManager().nextUndoBlock("vte_delrel");
relation.getModelSpace().getModelManager().deleteElement(relation, EDeleteSemantics.DELETE_SEMANTICS_FORCE);
source.getModelSpace().getTransactionManager().commitTransaction();
}
}
public void undo() {
parent = (IEntity) Lookup(parent, parent_name);
source = Lookup(source, source_name);
if (parent != null) {
parent.getModelSpace().getTransactionManager().undoTransaction(iTransactionID);
//parent.getModelSpace().getUndoManager().undo("vte_delent");
} else if (source != null) {
source.getModelSpace().getTransactionManager().undoTransaction(iTransactionID);
//source.getModelSpace().getUndoManager().undo("vte_delrel");
}
}
}