package es.iiia.sgi.handlers;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.IHandler;
import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.handlers.HandlerUtil;
import es.iiia.sgi.editors.RuleEditor;
import es.iiia.sgi.providers.RuleContentProvider;
import es.iiia.sgi.views.RuleView;
import es.iiia.shapeeditor.ShapeGrammarInput;
import es.iiia.shapegrammar.enums.ShapeGrammarRuleType;
import es.iiia.shapegrammar.rule.RuleModel;
import es.iiia.shapegrammar.utils.Debugger;
public abstract class AddRuleAbstractHandler extends AbstractHandler implements
IHandler {
public void execute(ExecutionEvent event, ShapeGrammarRuleType type) {
// find corresponding views to which we'll hook the listeners
IWorkbenchWindow workbenchWindow = HandlerUtil
.getActiveWorkbenchWindow(event);
// first the shape view
RuleView rules = (RuleView) workbenchWindow.getActivePage().findView(
RuleView.ID);
try {
// Add rule
RuleModel model = ((RuleContentProvider) rules.getContentProvider())
.addRule(type);
if (model != null) {
// Create input
ShapeGrammarInput input = new ShapeGrammarInput(model);
// opens editor
RuleEditor editor = (RuleEditor) workbenchWindow
.getActivePage().openEditor(input, RuleEditor.ID);
// hook the listener
model.addPropertyChangeListener(editor);
// shows properties
workbenchWindow.getActivePage().showView(
IPageLayout.ID_PROP_SHEET);
}
} catch (PartInitException e) {
Debugger.getInstance().addMessage(e.getStackTrace());
}
}
}