Search in sources :

Example 6 with AUndoableEdit

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

the class ProjectController method deleteDesign.

/**
     * Delete the specified design from the project, copying to the
     * clipboard if an <code>ObjectSaver</code> is provided.
     *
     * @param designToDelete design to delete
     * @param saver the clipboard's saver to hold the copied design
     * @throw java.io.IOException if saving a copy to the clipboard fails
     * @author mlh
     */
protected void deleteDesign(final Design designToDelete, ObjectSaver saver) {
    // Find the location of the design to delete
    final int designIndex = project.getDesigns().indexOf(designToDelete);
    // Delete the design, saving a copy to the clipboard if requested
    project.removeDesign(designToDelete);
    if (saver != null) {
        saveDesignToClipboard(designToDelete, saver);
    }
    // Create undo/redo step and add to the undo manager
    IUndoableEdit edit = new AUndoableEdit(ProjectLID.DeleteDesign) {

        // Save any associated ITaskApplications for undo restoration
        protected Map<ITaskDesign, TaskApplication> associatedTAs = project.taskApplicationsForRemovedDesign(designToDelete);

        protected boolean recoverMgr = true;

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

        @Override
        public void redo() {
            super.redo();
            recoverMgr = true;
            associatedTAs = project.taskApplicationsForRemovedDesign(designToDelete);
            project.removeDesign(designToDelete);
        }

        @Override
        public void undo() {
            super.undo();
            recoverMgr = false;
            project.addDesign(designIndex, designToDelete);
            project.restoreRemovedTaskApplications(associatedTAs);
        }

        @Override
        public void die() {
            super.die();
            if (recoverMgr) {
                recoverManagers(designToDelete, associatedTAs);
            }
        }
    };
    undoMgr.addEdit(edit);
}
Also used : AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit) Map(java.util.Map) HashMap(java.util.HashMap) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint)

Example 7 with AUndoableEdit

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

the class ProjectController method computeSnifAct.

// assignActions
protected boolean computeSnifAct(Design design, AUndertaking task, IUndoableEditSequence editSequence, SNIFACTGroupParameters defaults) {
    // TODO: L10N required for error titles and messages.
    if (design == null) {
        interaction.reportProblem("SNIF-ACT", "No design is selected.");
        return false;
    }
    if (design.getFrames().size() == 0) {
        interaction.reportProblem("SNIF-ACT", "Selected design is empty.");
        return false;
    }
    if (task.isTaskGroup()) {
        // We can only recompute with this algorithm if the
        // group was generated by a previous SNIF-ACT
        // computation (and thus has the execution
        // context attribute)
        Object contextAttr = task.getAttribute(WidgetAttributes.SNIFACT_CONTEXT_ATTR);
        if (NullSafe.equals(contextAttr, WidgetAttributes.NO_CONTEXT)) {
            interaction.reportProblem("SNIF-ACT", "Can't recompute with this algorithm from this cell.");
            return false;
        }
    }
    ISimilarityDictionary dict = (ISimilarityDictionary) design.getAttribute(WidgetAttributes.DICTIONARY_ATTR);
    if (dict == null) {
        interaction.reportProblem("Export Dictionary", "No dictionary exists for the selected design");
        return false;
    }
    List<Frame> sortedFrames = NamedObjectUtil.getSortedList(design.getFrames());
    SNIFACTParameters parms;
    int addGroupMode;
    boolean hasScript = false;
    final SNIFACTExecContext prevContext;
    if (task.isTaskGroup()) {
        // Since we got this far, we know the group already has
        // parameters associated with it, so use those values as
        // defaults in the dialog box
        prevContext = (SNIFACTExecContext) task.getAttribute(WidgetAttributes.SNIFACT_CONTEXT_ATTR);
        parms = prevContext.getParameters();
        addGroupMode = SNIFACTDialog.ENABLED;
    } else {
        // Otherwise, create a new set of default values for this
        // execution
        String defaultName = sortedFrames.get(0).getName();
        List<String> targets = new ArrayList<String>();
        targets.add(defaultName);
        parms = new SNIFACTParameters(task.getName(), NUM_SNIF_ACT_TRIALS, SNIF_ACT_K_VALUE, defaultName, targets, ITermSimilarity.ALL);
        addGroupMode = SNIFACTDialog.NONE;
        prevContext = null;
        TaskApplication ta = project.getTaskApplication(task, design);
        hasScript = (ta != null) && ta.hasScript();
    }
    SNIFACTGroupParameters groupParms = null;
    if (defaults == null) {
        groupParms = interaction.requestSNIFACTParameters(hasScript, sortedFrames, parms, dict.getAlgorithmsInUse(), addGroupMode);
    } else {
        groupParms = new SNIFACTGroupParameters(defaults.taskName, defaults.numRuns, defaults.kValue, defaults.startFrame, defaults.targetFrames, defaults.algorithm, ((addGroupMode != SNIFACTDialog.NONE) && defaults.addToGroup));
    }
    if (groupParms == null) {
        return false;
    }
    SNIFACTPredictionAlgo.ONLY.setParameters(groupParms);
    TaskParent parent = task.getTaskGroup();
    if (parent == null) {
        parent = project;
    }
    final TaskGroup group;
    if (groupParms.addToGroup) {
        // user wants to add new trial tasks to the same group
        group = (TaskGroup) task;
    } else {
        Collection<AUndertaking> siblings = parent.getUndertakings();
        group = new TaskGroup(SNIFACTCmd.getGroupName(groupParms, siblings), GroupNature.MEAN);
    }
    final SNIFACTExecContext context = SNIFACTCmd.computeInBackground(project, design, interaction, group, groupParms);
    if (context == null) {
        return true;
    }
    group.setAttribute(WidgetAttributes.SNIFACT_CONTEXT_ATTR, context);
    CompoundUndoableEdit snifActEditSeq = new CompoundUndoableEdit(SNIFACT_COMPUTATION, ProjectLID.RecomputeScript);
    if (!task.isTaskGroup()) {
        snifActEditSeq.addEdit(HCIPACmd.replaceTask(project, parent, task, group, SNIFACT_COMPUTATION));
    } else {
        if (groupParms.addToGroup) {
            snifActEditSeq.addEdit(SNIFACTCmd.addTasksToGroup(project, group, context, SNIFACT_COMPUTATION));
        } else {
            snifActEditSeq.addEdit(SNIFACTCmd.addGroup(project, parent, group, SNIFACT_COMPUTATION));
        }
    }
    snifActEditSeq.addEdit(new AUndoableEdit(ProjectLID.RecomputeScript) {

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

        @Override
        public void redo() {
            super.redo();
            group.setAttribute(WidgetAttributes.SNIFACT_CONTEXT_ATTR, context);
        }

        @Override
        public void undo() {
            super.undo();
            group.setAttribute(WidgetAttributes.SNIFACT_CONTEXT_ATTR, prevContext);
        }
    });
    snifActEditSeq.end();
    if (editSequence != null) {
        editSequence.addEdit(snifActEditSeq);
    }
    return true;
}
Also used : Frame(edu.cmu.cs.hcii.cogtool.model.Frame) ArrayList(java.util.ArrayList) SNIFACTGroupParameters(edu.cmu.cs.hcii.cogtool.model.SNIFACTPredictionAlgo.SNIFACTGroupParameters) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint) SNIFACTParameters(edu.cmu.cs.hcii.cogtool.model.SNIFACTPredictionAlgo.SNIFACTParameters) TaskParent(edu.cmu.cs.hcii.cogtool.model.TaskParent) AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication) SNIFACTExecContext(edu.cmu.cs.hcii.cogtool.model.SNIFACTExecContext) ISimilarityDictionary(edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary) TaskGroup(edu.cmu.cs.hcii.cogtool.model.TaskGroup)

Example 8 with AUndoableEdit

use of edu.cmu.cs.hcii.cogtool.util.AUndoableEdit 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 9 with AUndoableEdit

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

the class ProjectController method createNewTaskGroupUndo.

/**
     * Create the undo/redo step for the placement of a new task group and
     * the re-parenting of the selected tasks into the new task group.
     *
     * @param parent the parent task group (or project if null) for the
     *               new task group
     * @param index the location for inserting the new task group into
     *              the parent
     * @param taskGroup the new task group itself
     * @param oldParents the corresponding parent task parents for the given
     *                   child tasks
     * @param indexes the insertion index of each child in its old parent for
     *                use in undo
     * @param children the tasks to be re-parented
     * @param lid the undo/redo command token
     * @param presentationName the undo/redo edit label
     * @return the undo/redo step for the placement of the new task group and
     *         the re-parenting of the selected tasks into the new task group
     */
protected IUndoableEdit createNewTaskGroupUndo(final TaskParent parent, final int index, final TaskGroup taskGroup, final TaskParent[] oldParents, final int[] indexes, final AUndertaking[] children, CogToolLID lid, final String presentationName) {
    return new AUndoableEdit(lid) {

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

        @Override
        public void redo() {
            super.redo();
            // Add new group
            parent.addUndertaking(index, taskGroup);
            // Move children
            removeChildTasks(children, null, null);
            addToTaskGroup(children, taskGroup);
        }

        @Override
        public void undo() {
            super.undo();
            // Un-move children; IMPORTANT: reverse order!
            for (int i = children.length - 1; 0 <= i; i--) {
                // Remove from new group
                taskGroup.removeUndertaking(children[i]);
                // Add to old parent at old index
                oldParents[i].addUndertaking(indexes[i], children[i]);
            }
            // Remove new group
            parent.removeUndertaking(taskGroup);
        }
    };
}
Also used : AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint)

Example 10 with AUndoableEdit

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

the class ProjectController method createSetProjDefaultAlg.

protected IListenerAction createSetProjDefaultAlg(final ProjectLID lid, final String label, final IPredictionAlgo alg) {
    return new IListenerAction() {

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

        public boolean performAction(Object actionParms) {
            final IPredictionAlgo oldDefaultAlg = project.getDefaultAlgo();
            project.setDefaultAlgo(alg);
            undoMgr.addEdit(new AUndoableEdit(lid) {

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

                @Override
                public void redo() {
                    super.redo();
                    project.setDefaultAlgo(alg);
                }

                @Override
                public void undo() {
                    super.undo();
                    project.setDefaultAlgo(oldDefaultAlg);
                }
            });
            return true;
        }
    };
}
Also used : IPredictionAlgo(edu.cmu.cs.hcii.cogtool.model.IPredictionAlgo) IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) ProjectSelectionState(edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit)

Aggregations

AUndoableEdit (edu.cmu.cs.hcii.cogtool.util.AUndoableEdit)66 IUndoableEdit (edu.cmu.cs.hcii.cogtool.util.IUndoableEdit)34 IListenerAction (edu.cmu.cs.hcii.cogtool.util.IListenerAction)21 DoublePoint (edu.cmu.cs.hcii.cogtool.model.DoublePoint)16 AUndertaking (edu.cmu.cs.hcii.cogtool.model.AUndertaking)14 TaskApplication (edu.cmu.cs.hcii.cogtool.model.TaskApplication)14 ITaskDesign (edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign)12 CompoundUndoableEdit (edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit)10 Demonstration (edu.cmu.cs.hcii.cogtool.model.Demonstration)9 ComputationUndoRedo (edu.cmu.cs.hcii.cogtool.controller.DemoScriptCmd.ComputationUndoRedo)7 DefaultModelGeneratorState (edu.cmu.cs.hcii.cogtool.model.DefaultModelGeneratorState)7 Design (edu.cmu.cs.hcii.cogtool.model.Design)7 Frame (edu.cmu.cs.hcii.cogtool.model.Frame)7 TaskGroup (edu.cmu.cs.hcii.cogtool.model.TaskGroup)7 TaskParent (edu.cmu.cs.hcii.cogtool.model.TaskParent)7 DictEntry (edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictEntry)6 ProjectSelectionState (edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState)6 UndoManager (edu.cmu.cs.hcii.cogtool.util.UndoManager)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6