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.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.handlers.HandlerUtil;
import es.iiia.sgi.editors.RenderingEditor;
import es.iiia.sgi.views.RuleView;
import es.iiia.sgi.views.ShapeView;
import es.iiia.shapeeditor.ShapeGrammarInput;
import es.iiia.shapegrammar.model.ShapeGrammarModel;
public class CreateGrammarHandler extends AbstractHandler implements IHandler {
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow workbenchWindow = HandlerUtil
.getActiveWorkbenchWindow(event);
// first close all editors and save current data
boolean canContinue = workbenchWindow.getActivePage().closeAllEditors(
true);
if (canContinue) {
// create new grammar
ShapeGrammarModel newModel = this.createGrammar();
// create input
ShapeGrammarInput input = new ShapeGrammarInput(newModel);
// reload views
ShapeView shapes = (ShapeView) workbenchWindow.getActivePage()
.findView(ShapeView.ID);
RuleView rules = (RuleView) workbenchWindow.getActivePage()
.findView(RuleView.ID);
// set view content
shapes.setContent(input);
rules.setContent(input);
// open rendering editor
this.openRenderingEditor(workbenchWindow, input);
// rereques evaluation
this.requestEvaluation();
}
return null;
}
protected ShapeGrammarModel createGrammar() {
return new ShapeGrammarModel();
}
protected void requestEvaluation() {
// recreate menu
// IEvaluationService service = (IEvaluationService) PlatformUI
// .getWorkbench().getService(IEvaluationService.class);
// service.requestEvaluation("es.iiia.sgi.propertytesters.isFileOpen");
}
protected void openRenderingEditor(IWorkbenchWindow workbenchWindow,
ShapeGrammarInput input) {
try {
workbenchWindow.getActivePage().openEditor(input,
RenderingEditor.ID);
} catch (PartInitException e) {
e.printStackTrace();
}
}
}