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.sgi.editors.RuleEditor;
import es.iiia.shapegrammar.model.ShapeGrammarModel;
import es.iiia.shapegrammar.rule.RuleModel;
public class DeleteRuleHandler extends AbstractHandler implements IHandler {
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
if (MessageDialog
.openConfirm(window.getShell(), "Delete rule",
"Do you really wish to delete this rule?\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) {
RuleModel rule = (RuleModel) obj;
ShapeGrammarModel grammar = (ShapeGrammarModel) rule
.getParent();
grammar.deleteRule(rule);
// now close the editor holding this rule
for (IEditorReference editor : window.getActivePage()
.getEditorReferences()) {
if (editor.getEditor(false) instanceof RuleEditor) {
RuleModel model = (RuleModel) ((RuleEditor) editor.getEditor(false))
.getModel();
if (model.equals(rule)) {
window.getActivePage().closeEditor(
editor.getEditor(false), false);
}
}
}
}
return null;
}
}
return null;
}
}