package es.iiia.sgi.handlers;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;
import es.iiia.shapeeditor.ShapeEditor;
import es.iiia.shapegrammar.model.ShapeGrammarModel;
import es.iiia.shapegrammar.rule.RuleModel;
import es.iiia.shapegrammar.shape.ShapeModel;
public class DeleteShapeHandler extends AbstractHandler implements IHandler{
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
if (MessageDialog
.openConfirm(
window.getShell(),
"Delete shape",
"Do you really wish to delete this shape?\r\nThis action cannot be undone")) {
ISelection selection = window.getSelectionService().getSelection();
if (selection != null && selection instanceof IStructuredSelection) {
Object obj = ((IStructuredSelection) selection)
.getFirstElement();
// If we had a selection lets open the editor
if (obj != null) {
ShapeModel shape = (ShapeModel) obj;
ShapeGrammarModel grammar = (ShapeGrammarModel) shape
.getParent();
grammar.deleteShape(shape);
// now close the editor holding this rule
for (IEditorReference editor : window.getActivePage()
.getEditorReferences()) {
if (editor.getEditor(false) instanceof ShapeEditor) {
RuleModel model = (RuleModel) ((ShapeEditor) editor.getEditor(false))
.getModel();
if (model.equals(shape)) {
window.getActivePage().closeEditor(
editor.getEditor(false), false);
}
}
}
}
return null;
}
}
return null;
}
}