package jetbrains.mps.lang.editor.diagram.runtime.jetpad.palette.impl;
/*Generated by MPS */
import jetbrains.mps.openapi.editor.EditorContext;
import jetbrains.jetpad.projectional.view.ViewTrait;
import jetbrains.mps.baseLanguage.closures.runtime._FunctionTypes;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.nodeEditor.cells.jetpad.DiagramCell;
import jetbrains.mps.openapi.editor.cells.SubstituteAction;
import jetbrains.mps.smodel.action.NodeSubstituteActionWrapper;
import org.jetbrains.annotations.Nullable;
import jetbrains.jetpad.projectional.view.ViewTraitBuilder;
import jetbrains.jetpad.projectional.view.ViewEvents;
import jetbrains.jetpad.projectional.view.ViewEventHandler;
import jetbrains.jetpad.event.MouseEvent;
import jetbrains.jetpad.projectional.view.View;
import jetbrains.mps.editor.runtime.commands.EditorCommand;
public class PaletteElementsCreationAction extends AbstractPaletteCreationAction {
private EditorContext myEditorContext;
private int myX;
private int myY;
private ViewTrait myTrait;
private _FunctionTypes._void_P3_E0<? super SNode, ? super Integer, ? super Integer> myCallback;
public PaletteElementsCreationAction(DiagramCell diagramCell, SubstituteAction action, final _FunctionTypes._void_P3_E0<? super SNode, ? super Integer, ? super Integer> setNodePositionCallback, EditorContext editorContext) {
super(diagramCell, action);
myEditorContext = editorContext;
myCallback = setNodePositionCallback;
}
protected SubstituteAction createSubstituteAction(SubstituteAction action) {
return new NodeSubstituteActionWrapper(action) {
@Override
public SNode substitute(@Nullable EditorContext context, String string) {
SNode result = super.substitute(context, string);
myCallback.invoke(result, myX, myY);
return result;
}
};
}
protected ViewTrait getTrait() {
if (myTrait == null) {
myTrait = new ViewTraitBuilder().on(ViewEvents.MOUSE_PRESSED, new ViewEventHandler<MouseEvent>() {
public void handle(View view, MouseEvent event) {
if (view.viewAt(event.location()) != view) {
return;
}
if (!(view.focused().get())) {
view.container().focusedView().set(view);
}
myX = event.x();
myY = event.y();
final boolean[] result = new boolean[]{false};
myEditorContext.getRepository().getModelAccess().runReadAction(new Runnable() {
public void run() {
result[0] = mySubstituteAction.canSubstitute("");
}
});
if (!(result[0])) {
return;
}
myEditorContext.getRepository().getModelAccess().executeCommand(new EditorCommand(myEditorContext) {
protected void doExecute() {
mySubstituteAction.substitute(myEditorContext, "");
}
});
event.consume();
}
}).build();
}
return myTrait;
}
}