package jetbrains.mps.lang.editor.forms.runtime;
/*Generated by MPS */
import jetbrains.mps.openapi.editor.EditorContext;
import jetbrains.mps.nodeEditor.selectionRestoring.RestorableSelection;
import jetbrains.mps.openapi.editor.cells.EditorCell;
import jetbrains.mps.openapi.editor.cells.EditorCell_Collection;
import jetbrains.mps.util.IterableUtil;
import jetbrains.mps.nodeEditor.selectionRestoring.RestorableSelectionByCell;
import jetbrains.mps.nodeEditor.selectionRestoring.ChildCellLocator;
import jetbrains.mps.nodeEditor.selectionRestoring.CellIdLocator;
import jetbrains.mps.nodeEditor.selectionRestoring.CellSelector;
public abstract class AbstractToggleCheckboxAction {
private static final int EXPECTED_CHILD_INDEX = 0;
public void execute(EditorContext editorContext) {
RestorableSelection selection = saveSelection(editorContext);
doExecute();
if (selection != null) {
editorContext.flushEvents();
selection.restore(editorContext);
}
}
private RestorableSelection saveSelection(EditorContext editorContext) {
EditorCell selectedCell = editorContext.getSelectedCell();
if (selectedCell == null || selectedCell instanceof EditorCell_Collection) {
// No need to save selection, it will be restored by normal means
return null;
}
EditorCell_Collection parent = selectedCell.getParent();
if (parent.getCellsCount() < EXPECTED_CHILD_INDEX + 1 || IterableUtil.get(parent, EXPECTED_CHILD_INDEX) != selectedCell) {
// No need to save selection, it will be restored by normal means
return null;
}
return new RestorableSelectionByCell(new ChildCellLocator(new CellIdLocator(parent.getCellId(), parent.getSNode()), EXPECTED_CHILD_INDEX), createCellSelector(selectedCell));
}
protected abstract CellSelector createCellSelector(EditorCell selectedCell);
protected abstract void doExecute();
}