Search in sources :

Example 6 with IUndoableEdit

use of edu.cmu.cs.hcii.cogtool.util.IUndoableEdit in project cogtool by cogtool.

the class ProjectController method createChangeTaskPositionAction.

protected IListenerAction createChangeTaskPositionAction() {
    return new IListenerAction() {

        public Class<?> getParameterClass() {
            return ProjectUI.ChangeTaskPositionParms.class;
        }

        public boolean performAction(Object actionParms) {
            ProjectUI.ChangeTaskPositionParms prms = (ProjectUI.ChangeTaskPositionParms) actionParms;
            if ((prms.placeBeforeTask != null) && prms.tasks.isInSelectedHierarchy(prms.placeBeforeTask)) {
                // Nothing to do
                return false;
            }
            if ((prms.tasks == null) || (prms.tasks.getSelectedTaskCount() == 0)) {
                interaction.protestNoSelection();
                return false;
            }
            final AUndertaking[] selectedTasks = prms.tasks.getSelectedTasks(TaskSelectionState.PRUNE_SELECTION | TaskSelectionState.ORDER_SELECTION);
            final TaskParent[] oldParents = new TaskParent[selectedTasks.length];
            final int[] indexes = new int[selectedTasks.length];
            final String[] oldNames = new String[selectedTasks.length];
            final TaskParent placeBeforeTaskParent = (prms.placeBeforeTask != null) ? project.getTaskParent(prms.placeBeforeTask) : project;
            final List<AUndertaking> siblings = placeBeforeTaskParent.getUndertakings();
            // If the place-before-task is selected, find the next
            // sibling that is not selected.
            AUndertaking placeBefore = prms.placeBeforeTask;
            if (prms.tasks.isTaskSelected(placeBefore)) {
                int siblingIndex = siblings.indexOf(placeBefore);
                int siblingCount = siblings.size();
                while (++siblingIndex < siblingCount) {
                    placeBefore = siblings.get(siblingIndex);
                    if (!prms.tasks.isTaskSelected(placeBefore)) {
                        break;
                    }
                }
                if (siblingIndex >= siblingCount) {
                    placeBefore = null;
                }
            }
            // Remove first so that the atIndex computation works!
            removeChildTasks(selectedTasks, oldParents, indexes);
            final int atIndex = (placeBefore != null) ? siblings.indexOf(placeBefore) : siblings.size();
            // Add each selected task as siblings before the given task
            for (int i = 0; i < selectedTasks.length; i++) {
                oldNames[i] = selectedTasks[i].getName();
                String newName = NamedObjectUtil.makeNameUnique(oldNames[i], siblings);
                if (!newName.equals(oldNames[i])) {
                    selectedTasks[i].setName(newName);
                }
                placeBeforeTaskParent.addUndertaking(atIndex + i, selectedTasks[i]);
            }
            // Create undo/redo step and add to undo manager
            IUndoableEdit edit = new AUndoableEdit(ProjectLID.ChangeTaskPosition) {

                @Override
                public String getPresentationName() {
                    return (selectedTasks.length > 1) ? MOVE_TASKS : MOVE_TASK;
                }

                @Override
                public void redo() {
                    super.redo();
                    removeChildTasks(selectedTasks, null, null);
                    // before the given task
                    for (int i = 0; i < selectedTasks.length; i++) {
                        String newName = NamedObjectUtil.makeNameUnique(oldNames[i], siblings);
                        if (!newName.equals(oldNames[i])) {
                            selectedTasks[i].setName(newName);
                        }
                        placeBeforeTaskParent.addUndertaking(atIndex + i, selectedTasks[i]);
                    }
                }

                @Override
                public void undo() {
                    super.undo();
                    removeChildTasks(selectedTasks, null, null);
                    // Un-delete children; IMPORTANT: reverse order!
                    for (int i = selectedTasks.length - 1; 0 <= i; i--) {
                        if (!oldNames[i].equals(selectedTasks[i].getName())) {
                            selectedTasks[i].setName(oldNames[i]);
                        }
                        // Add to old parent at old index
                        oldParents[i].addUndertaking(indexes[i], selectedTasks[i]);
                    }
                }
            };
            undoMgr.addEdit(edit);
            return true;
        }
    };
}
Also used : DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint) ProjectUI(edu.cmu.cs.hcii.cogtool.ui.ProjectUI) IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) TaskParent(edu.cmu.cs.hcii.cogtool.model.TaskParent) AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit)

Example 7 with IUndoableEdit

use of edu.cmu.cs.hcii.cogtool.util.IUndoableEdit in project cogtool by cogtool.

the class ProjectController method duplicateTask.

protected AUndertaking duplicateTask(AUndertaking task, int atIndex, TaskParent parent, List<AUndertaking> siblings, CogToolLID lid, String presentationName, IUndoableEditSequence editSeq) {
    String newTaskName = NamedObjectUtil.makeNameUnique(task.getName(), siblings);
    IUndoableEdit edit;
    AUndertaking duplicateTask = task.duplicate(newTaskName);
    // Create undo/redo step
    if (task.isTaskGroup()) {
        SNIFACTExecContext context = (SNIFACTExecContext) task.getAttribute(WidgetAttributes.SNIFACT_CONTEXT_ATTR);
        // duplicated as well.
        if (!NullSafe.equals(context, WidgetAttributes.NO_CONTEXT)) {
            duplicateTask.setAttribute(WidgetAttributes.SNIFACT_CONTEXT_ATTR, context.duplicate());
        }
        edit = createNewTaskGroupUndo(parent, atIndex, (TaskGroup) duplicateTask, null, null, new AUndertaking[0], lid, presentationName);
    } else {
        edit = createNewTaskUndo(parent, atIndex, duplicateTask, lid, presentationName);
    }
    editSeq.addEdit(edit);
    parent.addUndertaking(atIndex, duplicateTask);
    if (task.isTaskGroup()) {
        duplicateTaskApplications((TaskGroup) task, (TaskGroup) duplicateTask);
    } else {
        duplicateTaskApplications(task, duplicateTask);
    }
    return duplicateTask;
}
Also used : AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit) SNIFACTExecContext(edu.cmu.cs.hcii.cogtool.model.SNIFACTExecContext) TaskGroup(edu.cmu.cs.hcii.cogtool.model.TaskGroup)

Example 8 with IUndoableEdit

use of edu.cmu.cs.hcii.cogtool.util.IUndoableEdit in project cogtool by cogtool.

the class ProjectController method recomputeScripts.

protected boolean recomputeScripts(AUndertaking task, Design design, DemoStateManager demoStateMgr, ComputeMessages computeMsgs, IUndoableEditSequence editSequence) {
    if (CogToolPref.isTracingOverride == null && !CogToolPref.IS_TRACING.getBoolean()) {
        Boolean answer = getInteraction().confirmNoTracing();
        if (answer == null) {
            // canceled
            return false;
        } else if (answer.booleanValue()) {
            CogToolPref.IS_TRACING.setBoolean(true);
        }
    }
    if (task.isTaskGroup()) {
        if (!NullSafe.equals(WidgetAttributes.NO_CONTEXT, task.getAttribute(WidgetAttributes.SNIFACT_CONTEXT_ATTR))) {
            return computeSnifAct(design, task, editSequence, null);
        }
        Iterator<AUndertaking> allTasks = ((TaskGroup) task).getUndertakings().iterator();
        CompoundUndoableEdit groupEditSeq = new CompoundUndoableEdit(RECOMPUTE_SCRIPTS, ProjectLID.RecomputeScript);
        while (allTasks.hasNext()) {
            if (!recomputeScripts(allTasks.next(), design, demoStateMgr, computeMsgs, groupEditSeq)) {
                return false;
            }
        }
        if (groupEditSeq.isSignificant()) {
            groupEditSeq.end();
            editSequence.addEdit(groupEditSeq);
        }
        return true;
    }
    TaskApplication ta = project.getTaskApplication(task, design);
    if (ta != null) {
        IPredictionAlgo activeAlg = ta.determineActiveAlgorithm(project);
        if (activeAlg == SNIFACTPredictionAlgo.ONLY) {
            return computeSnifAct(design, task, editSequence, null);
        }
        if (computeMsgs.canComputeScript(ta, demoStateMgr, editSequence)) {
            if (!activeAlg.allowsComputation()) {
                interaction.setStatusMessage(algDisallowsComputation);
                interaction.reportProblem(RECOMPUTE_SCRIPTS, algDisallowsComputation);
                return false;
            } else if (!ta.getDemonstration().isStartFrameChosen()) {
                // nothing to compute
                interaction.setStatusMessage(noStartFrameChosen);
                return false;
            }
            IUndoableEdit edit = ComputePredictionCmd.computeAllPredictions(project, ta, activeAlg, ta.determineComputeInBackground(project), interaction);
            if (edit != null) {
                ta.setActiveAlgorithm(activeAlg);
                editSequence.addEdit(edit);
            } else {
                interaction.setStatusMessage(computeHadNoResult);
            }
        } else {
            interaction.setStatusMessage(cannotRecomputeInvalid);
        }
    } else {
        if (project.getDefaultAlgo() == SNIFACTPredictionAlgo.ONLY) {
            return computeSnifAct(design, task, editSequence, null);
        }
        interaction.setStatusMessage(cannotRecomputeNoDemo);
    }
    return true;
}
Also used : IPredictionAlgo(edu.cmu.cs.hcii.cogtool.model.IPredictionAlgo) AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit)

Example 9 with IUndoableEdit

use of edu.cmu.cs.hcii.cogtool.util.IUndoableEdit in project cogtool by cogtool.

the class SEDemoController method changeSelfTransition.

protected boolean changeSelfTransition(final ActionScriptStep step, final AAction newAction, final double delayInSecs, final String delayLabel) {
    final AAction oldAction = step.getAction();
    final double oldDelayInSecs = step.getDelayInSecs();
    final String oldDelayLabel = step.getDelayLabel();
    if ((!oldAction.equals(newAction)) || (delayInSecs != oldDelayInSecs) || !oldDelayLabel.equals(delayLabel)) {
        step.setAction(newAction);
        step.setDelay(delayInSecs, delayLabel);
        Demonstration demo = script.getDemonstration();
        final int atIndex = demo.getStepIndex(step);
        final Collection<ComputationUndoRedo> scriptsUndoRedos = DemoScriptCmd.regenerateScripts(demo, atIndex, step, interaction);
        IUndoableEdit edit = new AUndoableEdit(SEDemoLID.Edit) {

            @Override
            public String getPresentationName() {
                return EDIT_SELF_TRANSITION;
            }

            @Override
            public void redo() {
                super.redo();
                step.setAction(newAction);
                step.setDelay(delayInSecs, delayLabel);
                DemoScriptCmd.redoAllChanges(scriptsUndoRedos);
            }

            @Override
            public void undo() {
                super.undo();
                step.setAction(oldAction);
                step.setDelay(oldDelayInSecs, oldDelayLabel);
                DemoScriptCmd.undoAllChanges(scriptsUndoRedos);
            }
        };
        CompoundUndoableEdit editSequence = new CompoundUndoableEdit(EDIT_SELF_TRANSITION, SEDemoLID.Edit);
        editSequence.addEdit(edit);
        if (CogToolPref.REGENERATE_AUTOMATICALLY.getBoolean()) {
            DemoScriptCmd.regenerateScripts(project, demo, demoStateMgr, interaction, editSequence);
        }
        editSequence.end();
        undoMgr.addEdit(editSequence);
    }
    return true;
}
Also used : ComputationUndoRedo(edu.cmu.cs.hcii.cogtool.controller.DemoScriptCmd.ComputationUndoRedo) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit) Demonstration(edu.cmu.cs.hcii.cogtool.model.Demonstration) AAction(edu.cmu.cs.hcii.cogtool.model.AAction)

Example 10 with IUndoableEdit

use of edu.cmu.cs.hcii.cogtool.util.IUndoableEdit in project cogtool by cogtool.

the class SEDemoController method deleteScriptStep.

// createInsertSelfTransitionAction
/**
     * Perform the operations needed to DELETE a script Action
     *
     * @param selection
     * @return
     */
protected boolean deleteScriptStep(SEDemoSelectionState selection) {
    // In case we need to go back to edit initial state.
    final CognitiveModelGenerator modelGen = script.getModelGenerator();
    final Demonstration demo = script.getDemonstration();
    final TaskApplication taskApp = demo.getTaskApplication();
    // If there is no selected action, try to delete the last item
    DefaultModelGeneratorState selectedState = selection.getSelectedState();
    final DefaultModelGeneratorState stateToDelete = getValidStepState(selectedState);
    IUndoableEdit edit;
    // If no states, go back to edit initial state
    if ((stateToDelete == null) || stateToDelete.getScriptStep().isInitiallyGenerated()) {
        closeWindow(false);
        demo.setStartFrameChosen(false);
        SEFrameChooserController.openController(taskApp, modelGen, project);
        edit = new AUndoableEdit(SEDemoLID.Delete) {

            protected DemoScriptCmd.ComputationUndoRedo computeUndoRedo = new DemoScriptCmd.ComputationUndoRedo(script);

            @Override
            public String getPresentationName() {
                return CHANGE_START_FRAME;
            }

            @Override
            public void redo() {
                super.redo();
                DefaultController seDemoController = ControllerRegistry.ONLY.findOpenController(script);
                if (seDemoController != null) {
                    seDemoController.closeWindow(false);
                }
                demo.setStartFrameChosen(false);
                SEFrameChooserController.openController(taskApp, modelGen, project);
                computeUndoRedo.redoChanges();
            }

            @Override
            public void undo() {
                super.undo();
                if (demo.getStartFrame() != null) {
                    demo.setStartFrameChosen(true);
                    // Close the frame chooser window.
                    DefaultController frameChooserController = ControllerRegistry.ONLY.findOpenController(taskApp);
                    if (frameChooserController != null) {
                        frameChooserController.closeWindow(false);
                    }
                    // Open the new demo view window
                    try {
                        SEDemoController.openController(taskApp, modelGen, project);
                    } catch (GraphicsUtil.ImageException ex) {
                        interaction.protestInvalidImageFile();
                    }
                    computeUndoRedo.undoChanges();
                }
            }
        };
        UndoManager seFrameMgr = UndoManager.getUndoManager(taskApp, project);
        seFrameMgr.addEdit(edit);
        undoMgr.addEdit(edit);
        return true;
    }
    AScriptStep step = stateToDelete.getScriptStep();
    // If a generated think step, simply delete
    if ((step instanceof ThinkScriptStep) && !step.isInsertedByUser()) {
        edit = new AUndoableEdit(SEDemoLID.Delete) {

            protected int scriptIndex = script.removeState(stateToDelete);

            protected DemoScriptCmd.ComputationUndoRedo computeUndoRedo = new DemoScriptCmd.ComputationUndoRedo(script);

            @Override
            public String getPresentationName() {
                return DELETE_STEP;
            }

            @Override
            public void redo() {
                super.redo();
                script.removeState(scriptIndex);
                computeUndoRedo.redoChanges();
            }

            @Override
            public void undo() {
                super.undo();
                script.insertState(stateToDelete, scriptIndex);
                computeUndoRedo.undoChanges();
            }
        };
    } else {
        final AScriptStep demoStep = step.getOwner();
        // There are no "new" steps to replace with when deleting
        Set<AScriptStep> emptyDemoSteps = new HashSet<AScriptStep>();
        if (demoStep.getCurrentFrame() == demoStep.getDestinationFrame()) {
            final int atIndex = demo.removeStep(demoStep);
            final Collection<ComputationUndoRedo> scriptsUndoRedos = DemoScriptCmd.regenerateScripts(demo, atIndex, demoStep, interaction);
            Set<AScriptStep> oldDemoSteps = Collections.singleton(demoStep);
            edit = new DemoStateManager.ADemoUndoableEdit(SEDemoLID.Delete, demo, emptyDemoSteps, oldDemoSteps, demoStateMgr) {

                @Override
                public String getPresentationName() {
                    return DELETE_STEP;
                }

                @Override
                public void redo() {
                    super.redo();
                    demo.removeStep(atIndex);
                    DemoScriptCmd.redoAllChanges(scriptsUndoRedos);
                }

                @Override
                public void undo() {
                    super.undo();
                    demo.insertStep(demoStep, atIndex);
                    DemoScriptCmd.undoAllChanges(scriptsUndoRedos);
                }
            };
        } else {
            if ((selectedState != null) && (demoStep != demo.getLastStep()) && !interaction.confirmDeleteScriptStep()) {
                return false;
            }
            Set<AScriptStep> oldDemoSteps = new LinkedHashSet<AScriptStep>();
            final int atIndex = demo.replaceSteps(demoStep, emptyDemoSteps, oldDemoSteps);
            final Collection<ComputationUndoRedo> scriptsUndoRedos = DemoScriptCmd.regenerateScripts(demo, atIndex, demoStep, interaction);
            edit = new DemoStateManager.ADemoUndoableEdit(SEDemoLID.Delete, demo, emptyDemoSteps, oldDemoSteps, demoStateMgr) {

                @Override
                public String getPresentationName() {
                    return DELETE_STEP;
                }

                @Override
                public void redo() {
                    super.redo();
                    demo.replaceSteps(atIndex, redoDemoSteps);
                    DemoScriptCmd.redoAllChanges(scriptsUndoRedos);
                }

                @Override
                public void undo() {
                    super.undo();
                    demo.replaceSteps(atIndex, undoDemoSteps);
                    DemoScriptCmd.undoAllChanges(scriptsUndoRedos);
                }
            };
        }
    }
    CompoundUndoableEdit editSequence = new CompoundUndoableEdit(DELETE_STEP, SEDemoLID.Delete);
    editSequence.addEdit(edit);
    if (CogToolPref.REGENERATE_AUTOMATICALLY.getBoolean()) {
        DemoScriptCmd.regenerateScripts(project, demo, demoStateMgr, interaction, editSequence);
    }
    editSequence.end();
    undoMgr.addEdit(editSequence);
    return true;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ComputationUndoRedo(edu.cmu.cs.hcii.cogtool.controller.DemoScriptCmd.ComputationUndoRedo) CognitiveModelGenerator(edu.cmu.cs.hcii.cogtool.model.CognitiveModelGenerator) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) AScriptStep(edu.cmu.cs.hcii.cogtool.model.AScriptStep) ThinkScriptStep(edu.cmu.cs.hcii.cogtool.model.ThinkScriptStep) UndoManager(edu.cmu.cs.hcii.cogtool.util.UndoManager) ComputationUndoRedo(edu.cmu.cs.hcii.cogtool.controller.DemoScriptCmd.ComputationUndoRedo) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit) Demonstration(edu.cmu.cs.hcii.cogtool.model.Demonstration) DefaultModelGeneratorState(edu.cmu.cs.hcii.cogtool.model.DefaultModelGeneratorState) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

IUndoableEdit (edu.cmu.cs.hcii.cogtool.util.IUndoableEdit)44 AUndoableEdit (edu.cmu.cs.hcii.cogtool.util.AUndoableEdit)34 DoublePoint (edu.cmu.cs.hcii.cogtool.model.DoublePoint)12 CompoundUndoableEdit (edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit)12 AUndertaking (edu.cmu.cs.hcii.cogtool.model.AUndertaking)10 TaskApplication (edu.cmu.cs.hcii.cogtool.model.TaskApplication)10 ComputationUndoRedo (edu.cmu.cs.hcii.cogtool.controller.DemoScriptCmd.ComputationUndoRedo)9 Frame (edu.cmu.cs.hcii.cogtool.model.Frame)8 IListenerAction (edu.cmu.cs.hcii.cogtool.util.IListenerAction)8 Demonstration (edu.cmu.cs.hcii.cogtool.model.Demonstration)7 UndoManager (edu.cmu.cs.hcii.cogtool.util.UndoManager)7 ITaskDesign (edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 DefaultModelGeneratorState (edu.cmu.cs.hcii.cogtool.model.DefaultModelGeneratorState)5 FrameElementGroup (edu.cmu.cs.hcii.cogtool.model.FrameElementGroup)5 TaskGroup (edu.cmu.cs.hcii.cogtool.model.TaskGroup)5 Design (edu.cmu.cs.hcii.cogtool.model.Design)4 DoubleRectangle (edu.cmu.cs.hcii.cogtool.model.DoubleRectangle)4 IWidget (edu.cmu.cs.hcii.cogtool.model.IWidget)4