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.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.handlers.HandlerUtil;
import es.iiia.sgi.editors.RuleEditor;
import es.iiia.shapegrammar.rule.RuleModel;
import es.iiia.shapegrammar.shape.ShapeModel;
public class AddToRightHandler extends AbstractHandler implements IHandler {
public Object execute(ExecutionEvent event) throws ExecutionException {
// get current model
RuleEditor editor = (RuleEditor) HandlerUtil.getActiveEditor(event);
RuleModel rule = (RuleModel) editor.getModel();
// notify user that shape could be replaced
if (rule.getRightShape() != null) {
}
// replace current left shape with the one selected
ISelection selection = HandlerUtil.getActivePart(event).getSite()
.getSelectionProvider().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;
// find the window centre
// IFigure part = ((RuleEditPart)editor.getGraphicalViewer().getContents()).getFigure();
// rule.setCenter(new PointModel(part.getBounds().getCenter().x, part.getBounds().getCenter().y));
rule.setRightShape(shape);
}
}
return null;
}
}