Search in sources :

Example 1 with IUndoableEdit

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

the class ProjectController method createMoveTaskAppAction.

protected IListenerAction createMoveTaskAppAction() {
    return new IListenerAction() {

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

        public boolean performAction(Object p) {
            if (p != null) {
                ProjectUI.MoveCopyTaskApplicationParms parms = (ProjectUI.MoveCopyTaskApplicationParms) p;
                // Must have selected tasks and design
                if ((parms.design == null) || (parms.fromTask == null) || (parms.toTask == null)) {
                    interaction.protestNoSelection();
                    return false;
                }
                final AUndertaking fromTask = parms.fromTask;
                final AUndertaking toTask = parms.toTask;
                final TaskApplication taskApp = project.removeTaskApplication(parms.fromTask, parms.design);
                if (taskApp == null) {
                    interaction.protestNoTaskApplication();
                    return false;
                }
                final TaskApplication oldTaskApp = project.removeTaskApplication(parms.toTask, parms.design);
                taskApp.setTask(toTask);
                project.setTaskApplication(taskApp);
                IUndoableEdit edit = new AUndoableEdit(ProjectLID.MoveTaskApplication) {

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

                    @Override
                    public void redo() {
                        super.redo();
                        project.removeTaskApplication(taskApp);
                        if (oldTaskApp != null) {
                            project.removeTaskApplication(oldTaskApp);
                        }
                        taskApp.setTask(toTask);
                        project.setTaskApplication(taskApp);
                    }

                    @Override
                    public void undo() {
                        super.undo();
                        project.removeTaskApplication(taskApp);
                        taskApp.setTask(fromTask);
                        if (oldTaskApp != null) {
                            project.setTaskApplication(oldTaskApp);
                        }
                        project.setTaskApplication(taskApp);
                    }
                };
                undoMgr.addEdit(edit);
            }
            return true;
        }
    };
}
Also used : ProjectUI(edu.cmu.cs.hcii.cogtool.ui.ProjectUI) IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit)

Example 2 with IUndoableEdit

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

the class ProjectController method promoteTask.

protected String promoteTask(final AUndertaking task, CogToolLID lid, IUndoableEditSequence editSeq) {
    final TaskGroup parent = task.getTaskGroup();
    if (parent == null) {
        return cannotPromoteTaskError + ": " + task.getFullName();
    }
    List<AUndertaking> siblings = parent.getUndertakings();
    final int indexInParent = siblings.indexOf(task);
    final TaskParent grandparent = project.getTaskParent(parent);
    List<AUndertaking> uncles = grandparent.getUndertakings();
    final int newTaskIndex = uncles.indexOf(parent) + 1;
    final String oldTaskName = task.getName();
    final int adoptedSiblingCount = siblings.size() - indexInParent - 1;
    final AUndertaking asUndertaking = ((task instanceof TaskGroup) || (adoptedSiblingCount == 0)) ? task : new TaskGroup(task.getName(), GroupNature.SUM);
    final TaskGroup asTaskGroup = asUndertaking.isTaskGroup() ? (TaskGroup) asUndertaking : null;
    // Must treat associated task-applications as deleted if the
    // promotion changed the undertaking from a task to a task group.
    final Map<ITaskDesign, TaskApplication> assocTAs = (asUndertaking != task) ? project.taskApplicationsForRemovedTask(task) : null;
    final AUndertaking[] adoptedSiblings = (adoptedSiblingCount > 0) ? new AUndertaking[adoptedSiblingCount] : null;
    final String[] oldSiblingNames = (adoptedSiblingCount > 0) ? new String[adoptedSiblingCount] : null;
    // Delete task and later siblings from parent
    parent.removeUndertaking(task);
    for (int i = 0; i < adoptedSiblingCount; i++) {
        adoptedSiblings[i] = siblings.get(indexInParent);
        if (oldSiblingNames != null) {
            oldSiblingNames[i] = adoptedSiblings[i].getName();
        }
        parent.removeUndertaking(adoptedSiblings[i]);
        // Insert later siblings as children of asTaskGroup
        makeTaskNameUnique(asTaskGroup, adoptedSiblings[i]);
        asTaskGroup.addUndertaking(adoptedSiblings[i]);
    }
    // Insert task at newTaskIndex into grandparent
    makeTaskNameUnique(grandparent, asUndertaking);
    grandparent.addUndertaking(newTaskIndex, asUndertaking);
    // Create undoable edit
    IUndoableEdit edit = new AUndoableEdit(lid) {

        protected Map<ITaskDesign, TaskApplication> associatedTAs = assocTAs;

        protected boolean recoverMgrs = true;

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

        @Override
        public void redo() {
            super.redo();
            recoverMgrs = true;
            associatedTAs = (asUndertaking != task) ? project.taskApplicationsForRemovedTask(task) : null;
            for (int i = 0; i < adoptedSiblingCount; i++) {
                parent.removeUndertaking(adoptedSiblings[i]);
                makeTaskNameUnique(asTaskGroup, adoptedSiblings[i]);
                asTaskGroup.addUndertaking(adoptedSiblings[i]);
            }
            parent.removeUndertaking(task);
            makeTaskNameUnique(grandparent, asUndertaking);
            grandparent.addUndertaking(newTaskIndex, asUndertaking);
        }

        @Override
        public void undo() {
            super.undo();
            recoverMgrs = false;
            grandparent.removeUndertaking(asUndertaking);
            task.setName(oldTaskName);
            parent.addUndertaking(indexInParent, task);
            if (asUndertaking != task) {
                project.restoreRemovedTaskApplications(associatedTAs);
            }
            for (int i = 0; i < adoptedSiblingCount; i++) {
                asTaskGroup.removeUndertaking(adoptedSiblings[i]);
                if (oldSiblingNames != null) {
                    adoptedSiblings[i].setName(oldSiblingNames[i]);
                }
                parent.addUndertaking(indexInParent + i + 1, adoptedSiblings[i]);
            }
        }

        @Override
        public void die() {
            super.die();
            if (recoverMgrs && (asUndertaking != task)) {
                recoverManagers(task, associatedTAs);
            }
        }
    };
    editSeq.addEdit(edit);
    return null;
}
Also used : ITaskDesign(edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint) 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) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit) TaskGroup(edu.cmu.cs.hcii.cogtool.model.TaskGroup) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with IUndoableEdit

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

the class ProjectController method demoteTask.

protected String demoteTask(final AUndertaking task, IUndoableEditSequence editSeq) {
    final TaskParent parent = project.getTaskParent(task);
    List<AUndertaking> siblings = parent.getUndertakings();
    final int indexInParent = siblings.indexOf(task);
    if (indexInParent == 0) {
        return cannotDemoteTaskError + ": " + task.getFullName();
    }
    final String oldTaskName = task.getName();
    final AUndertaking prevSibling = siblings.get(indexInParent - 1);
    final TaskGroup siblingAsTaskGroup = (prevSibling instanceof TaskGroup) ? (TaskGroup) prevSibling : new TaskGroup(prevSibling.getName(), GroupNature.SUM);
    if (siblingAsTaskGroup.getUndertakings().size() > 0) {
        AUndertaking firstTask = siblingAsTaskGroup.getUndertakings().get(0);
        if (firstTask.isSpawned()) {
            return cannotDemoteIntoGroupError;
        }
    }
    // Must treat sibling's task-applications as deleted if the
    // demotion changed it from a task to a task group
    final Map<ITaskDesign, TaskApplication> siblingAssocTAs = (siblingAsTaskGroup != prevSibling) ? project.taskApplicationsForRemovedTask(prevSibling) : null;
    // Remove task from parent and replace previousSibling with
    // new task group if not already one.
    parent.removeUndertaking(task);
    if (siblingAsTaskGroup != prevSibling) {
        parent.removeUndertaking(prevSibling);
        parent.addUndertaking(indexInParent - 1, siblingAsTaskGroup);
    }
    // Insert task as the last child of siblingAsTaskGroup
    makeTaskNameUnique(siblingAsTaskGroup, task);
    siblingAsTaskGroup.addUndertaking(task);
    // Create undoable edit
    IUndoableEdit edit = new AUndoableEdit(ProjectLID.DemoteTask) {

        protected Map<ITaskDesign, TaskApplication> siblingTAs = siblingAssocTAs;

        protected boolean recoverMgrs = true;

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

        @Override
        public void redo() {
            super.redo();
            recoverMgrs = true;
            siblingTAs = (siblingAsTaskGroup != prevSibling) ? project.taskApplicationsForRemovedTask(prevSibling) : null;
            // Remove task from parent and replace previousSibling with
            // new task group if not already one.
            parent.removeUndertaking(task);
            if (siblingAsTaskGroup != prevSibling) {
                parent.removeUndertaking(prevSibling);
                parent.addUndertaking(indexInParent - 1, siblingAsTaskGroup);
            }
            // Insert task as the last child of siblingAsTaskGroup
            makeTaskNameUnique(siblingAsTaskGroup, task);
            siblingAsTaskGroup.addUndertaking(task);
        }

        @Override
        public void undo() {
            super.undo();
            recoverMgrs = false;
            // Remove task from sibling group; rename back
            siblingAsTaskGroup.removeUndertaking(task);
            task.setName(oldTaskName);
            // Restore sibling as a task if necessary
            if (siblingAsTaskGroup != prevSibling) {
                parent.removeUndertaking(siblingAsTaskGroup);
                parent.addUndertaking(indexInParent - 1, prevSibling);
                project.restoreRemovedTaskApplications(siblingTAs);
            }
            // Add task back to parent
            parent.addUndertaking(indexInParent, task);
        }

        @Override
        public void die() {
            super.die();
            if (recoverMgrs && (siblingAsTaskGroup != prevSibling)) {
                recoverManagers(task, siblingTAs);
            }
        }
    };
    editSeq.addEdit(edit);
    return null;
}
Also used : ITaskDesign(edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign) 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) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit) TaskGroup(edu.cmu.cs.hcii.cogtool.model.TaskGroup) Map(java.util.Map) HashMap(java.util.HashMap) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint)

Example 4 with IUndoableEdit

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

the class ProjectController method deleteTasks.

// modifyTaskGroup
/**
     * Utility to delete the specified tasks.
     * If an object saver is specified (that is, not <code>null</code>),
     * the deleted tasks are serialized as well.
     *
     * @param tasksToDelete the subtasks to delete
     * @param saver if not null, the saver to use for serializing the
     *              deleted tasks (used saving to the system clipboard for the
     *              "cut" action)
     * @param editSequence to hold the undoable edit
     * @throws RcvrClipboardException if the saver is given and serialization
     *         failed for some reason; NOTE!!! Delete will have succeeded!
     * @author mlh
     */
protected void deleteTasks(final AUndertaking[] tasksToDelete, ObjectSaver saver, IUndoableEditSequence editSequence) {
    @SuppressWarnings("unchecked") final Map<ITaskDesign, TaskApplication>[] assocTAs = new Map[tasksToDelete.length];
    RcvrClipboardException firstException = null;
    // Delete each task and serialize if desired
    for (int i = 0; i < tasksToDelete.length; i++) {
        assocTAs[i] = project.taskApplicationsForRemovedTask(tasksToDelete[i]);
        if (saver != null) {
            try {
                saver.saveObject(tasksToDelete[i]);
            } catch (IOException ex) {
                if (firstException != null) {
                    firstException = new RcvrClipboardException("Could not save object" + " to clipboard.", ex);
                }
            }
        }
    }
    final TaskParent[] oldParents = new TaskParent[tasksToDelete.length];
    final int[] indexes = new int[tasksToDelete.length];
    removeChildTasks(tasksToDelete, oldParents, indexes);
    // Create undo/redo step and add to undo manager
    IUndoableEdit edit = new AUndoableEdit(ProjectLID.DeleteTask) {

        protected Map<ITaskDesign, TaskApplication>[] associatedTAs = assocTAs;

        protected boolean recoverMgrs = true;

        @Override
        public String getPresentationName() {
            return (tasksToDelete.length > 1) ? DELETE_TASKS : DELETE_TASK;
        }

        @Override
        public void redo() {
            super.redo();
            recoverMgrs = true;
            for (int i = 0; i < tasksToDelete.length; i++) {
                associatedTAs[i] = project.taskApplicationsForRemovedTask(tasksToDelete[i]);
            }
            removeChildTasks(tasksToDelete, null, null);
        }

        @Override
        public void undo() {
            super.undo();
            recoverMgrs = false;
            // Un-delete children; IMPORTANT: reverse order!
            for (int i = tasksToDelete.length - 1; 0 <= i; i--) {
                // Add to old parent at old index
                oldParents[i].addUndertaking(indexes[i], tasksToDelete[i]);
                project.restoreRemovedTaskApplications(associatedTAs[i]);
            }
        }

        @Override
        public void die() {
            super.die();
            if (recoverMgrs) {
                for (int i = 0; i < tasksToDelete.length; i++) {
                    recoverManagers(tasksToDelete[i], associatedTAs[i]);
                }
            }
        }
    };
    editSequence.addEdit(edit);
    if (firstException != null) {
        throw firstException;
    }
}
Also used : ITaskDesign(edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign) RcvrClipboardException(edu.cmu.cs.hcii.cogtool.util.RcvrClipboardException) IOException(java.io.IOException) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint) TaskParent(edu.cmu.cs.hcii.cogtool.model.TaskParent) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit) Map(java.util.Map) HashMap(java.util.HashMap)

Example 5 with IUndoableEdit

use of edu.cmu.cs.hcii.cogtool.util.IUndoableEdit 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)

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