Search in sources :

Example 36 with IUndoableEdit

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

the class ProjectController method moveTaskAction.

protected boolean moveTaskAction(TaskSelectionState seln, boolean moveEarlier) {
    AUndertaking[] tasks = seln.getSelectedTasks(TaskSelectionState.PRUNE_SELECTION | TaskSelectionState.ORDER_SELECTION);
    if ((tasks == null) || (tasks.length == 0)) {
        interaction.protestNoSelection();
        return false;
    }
    if (tasks.length > 1) {
        interaction.protestTooManySelectedTasks();
        return false;
    }
    final AUndertaking task = tasks[0];
    final TaskParent parent = project.getTaskParent(task);
    List<AUndertaking> siblings = parent.getUndertakings();
    int siblingCount = siblings.size();
    if (siblingCount == 1) {
        interaction.setStatusMessage(taskIsOnlyChild);
    } else {
        final int oldTaskIndex = siblings.indexOf(task);
        IUndoableEdit edit;
        if (moveEarlier) {
            if (oldTaskIndex == 0) {
                interaction.protestCannotMoveEarlier();
                return false;
            }
            parent.removeUndertaking(task);
            parent.addUndertaking(oldTaskIndex - 1, task);
            edit = new AUndoableEdit(ProjectLID.MoveTaskEarlier) {

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

                @Override
                public void redo() {
                    super.redo();
                    parent.removeUndertaking(task);
                    parent.addUndertaking(oldTaskIndex - 1, task);
                }

                @Override
                public void undo() {
                    super.undo();
                    parent.removeUndertaking(task);
                    parent.addUndertaking(oldTaskIndex, task);
                }
            };
        } else {
            if (oldTaskIndex == siblingCount - 1) {
                interaction.protestCannotMoveLater();
                return false;
            }
            parent.removeUndertaking(task);
            parent.addUndertaking(oldTaskIndex + 1, task);
            edit = new AUndoableEdit(ProjectLID.MoveTaskEarlier) {

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

                @Override
                public void redo() {
                    super.redo();
                    parent.removeUndertaking(task);
                    parent.addUndertaking(oldTaskIndex + 1, task);
                }

                @Override
                public void undo() {
                    super.undo();
                    parent.removeUndertaking(task);
                    parent.addUndertaking(oldTaskIndex, task);
                }
            };
        }
        undoMgr.addEdit(edit);
    }
    return true;
}
Also used : 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) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint)

Example 37 with IUndoableEdit

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

the class ProjectController method openProjectOnCompute.

/**
     * editSeq will be null if no regeneration occurred; otherwise,
     * the edit sequence will contain the undoable edit for the regeneration.
     */
public static boolean openProjectOnCompute(Project project, final Script script, final APredictionResult newResult, CompoundUndoableEdit editSeq) {
    boolean notModified;
    try {
        notModified = UndoManager.isAtSavePoint(project);
    } catch (IllegalStateException ex) {
        System.err.println("Ignoring that isAtSavePoint failed.");
        notModified = false;
    }
    ProjectController c = ProjectController.openController(project, false, notModified);
    final CognitiveModelGenerator modelGen = script.getModelGenerator();
    final IPredictionAlgo computeAlg = newResult.getPredictionAlgorithm();
    final TaskApplication taskApp = script.getDemonstration().getTaskApplication();
    final APredictionResult oldResult = taskApp.getResult(modelGen, computeAlg);
    taskApp.setResult(modelGen, computeAlg, PredictionResultProxy.getLatestResult(newResult));
    UndoManager scriptUndoMgr = UndoManager.getUndoManager(script, project);
    IUndoableEdit edit = new AUndoableEdit(ProjectLID.RecomputeScript) {

        protected APredictionResult redoResult = newResult;

        protected APredictionResult undoResult = oldResult;

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

        @Override
        public void redo() {
            super.redo();
            redoResult = PredictionResultProxy.getLatestResult(redoResult);
            taskApp.setResult(modelGen, computeAlg, redoResult);
        }

        @Override
        public void undo() {
            super.undo();
            undoResult = PredictionResultProxy.getLatestResult(undoResult);
            taskApp.setResult(modelGen, computeAlg, undoResult);
        }
    };
    if (editSeq != null) {
        editSeq.addEdit(edit);
        editSeq.end();
        edit = editSeq;
    }
    // Add to script's undo mgr first to set owner properly
    scriptUndoMgr.addEdit(edit);
    c.undoMgr.addEdit(edit);
    c.takeFocus();
    return true;
}
Also used : RcvrIllegalStateException(edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException) IPredictionAlgo(edu.cmu.cs.hcii.cogtool.model.IPredictionAlgo) UndoManager(edu.cmu.cs.hcii.cogtool.util.UndoManager) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) APredictionResult(edu.cmu.cs.hcii.cogtool.model.APredictionResult) CognitiveModelGenerator(edu.cmu.cs.hcii.cogtool.model.CognitiveModelGenerator) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit)

Example 38 with IUndoableEdit

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

the class FrameEditorController method createUngroupElementsAction.

private IListenerAction createUngroupElementsAction() {
    return new IListenerAction() {

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

        public boolean performAction(Object prms) {
            FrameEditorSelectionState selection = (FrameEditorSelectionState) prms;
            Iterator<FrameElement> selectedElts = selection.getSelectedElementsIterator();
            final Set<FrameElementGroup> selectedGroups = new HashSet<FrameElementGroup>();
            while (selectedElts.hasNext()) {
                FrameElement elt = selectedElts.next();
                if (elt instanceof FrameElementGroup) {
                    selectedGroups.add((FrameElementGroup) elt);
                } else {
                    selectedGroups.addAll(elt.getRootElement().getEltGroups());
                }
            }
            if (selectedGroups.size() > 0) {
                CompoundUndoableEdit editSequence = new CompoundUndoableEdit(UNGROUP_ELEMENTS, FrameEditorLID.Ungroup);
                Iterator<FrameElementGroup> groups = selectedGroups.iterator();
                while (groups.hasNext()) {
                    FrameElementGroup group = groups.next();
                    ungroup(group, editSequence);
                    removeRootElement(UNGROUP_ELEMENTS, group, null, editSequence);
                }
                IUndoableEdit edit = new AUndoableEdit(FrameEditorLID.Ungroup) {

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

                    @Override
                    public void redo() {
                        super.redo();
                        Iterator<FrameElementGroup> groups = selectedGroups.iterator();
                        while (groups.hasNext()) {
                            FrameElementGroup group = groups.next();
                            ungroup(group, null);
                        }
                    }

                    @Override
                    public void undo() {
                        super.undo();
                        Iterator<FrameElementGroup> groups = selectedGroups.iterator();
                        while (groups.hasNext()) {
                            FrameElementGroup group = groups.next();
                            regroup(group);
                        }
                    }
                };
                editSequence.addEdit(edit);
                // Commit the edit
                editSequence.end();
                // Only add this edit if it is significant
                if (editSequence.isSignificant()) {
                    undoMgr.addEdit(editSequence);
                }
                return true;
            }
            interaction.protestNoSelection();
            return false;
        }
    };
}
Also used : IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) FrameEditorSelectionState(edu.cmu.cs.hcii.cogtool.ui.FrameEditorSelectionState) FrameElementGroup(edu.cmu.cs.hcii.cogtool.model.FrameElementGroup) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) FrameElement(edu.cmu.cs.hcii.cogtool.model.FrameElement) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit) HashSet(java.util.HashSet)

Example 39 with IUndoableEdit

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

the class DesignEditorCmd method pasteElements.

// pasteFrameElementGroup
public static int pasteElements(Design design, final Frame frame, Collection<Object> objects, DemoStateManager mgr, IUndoableEditSequence editSequence) {
    // If given objects contain widgets, insert into the given frame
    if ((objects != null) && (objects.size() > 0)) {
        Iterator<Object> objIt = objects.iterator();
        Set<SimpleWidgetGroup> addedGroups = new HashSet<SimpleWidgetGroup>();
        final Set<IWidget> addedRemoteLabels = new HashSet<IWidget>();
        final Set<FrameElementGroup> addedEltGroups = new HashSet<FrameElementGroup>();
        int numPasted = 0;
        // May need to add a device
        int currentDeviceTypes = DeviceType.buildDeviceSet(design.getDeviceTypes());
        // create them.
        while (objIt.hasNext()) {
            Object o = objIt.next();
            if (o instanceof IWidget) {
                IWidget widget = (IWidget) o;
                numPasted += pasteWidget(widget, design, currentDeviceTypes, frame, mgr, editSequence, addedEltGroups, addedRemoteLabels);
                SimpleWidgetGroup group = widget.getParentGroup();
                if (group != null) {
                    addedGroups.add(group);
                }
            } else if (o instanceof FrameElementGroup) {
                numPasted += pasteFrameElementGroup((FrameElementGroup) o, design, currentDeviceTypes, frame, mgr, editSequence, addedEltGroups, addedRemoteLabels);
            }
        }
        Iterator<SimpleWidgetGroup> groupsIter = addedGroups.iterator();
        while (groupsIter.hasNext()) {
            SimpleWidgetGroup group = groupsIter.next();
            repositionChildren(group);
            if (group instanceof GridButtonGroup) {
                ((GridButtonGroup) group).recalculateOffsets();
            }
            addedEltGroups.addAll(group.getEltGroups());
        }
        Iterator<FrameElementGroup> eltGroups = addedEltGroups.iterator();
        while (eltGroups.hasNext()) {
            frame.addEltGroup(eltGroups.next());
        }
        Iterator<IWidget> remoteLabels = addedRemoteLabels.iterator();
        while (remoteLabels.hasNext()) {
            IWidget remoteLabel = remoteLabels.next();
            if (!frame.containsWidget(remoteLabel)) {
                String uniqueName = NamedObjectUtil.makeNameUnique(remoteLabel.getName(), frame.getWidgets());
                remoteLabel.setName(uniqueName);
                frame.addWidget(remoteLabel);
            }
        }
        IUndoableEdit edit = new AUndoableEdit(CogToolLID.Paste) {

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

            @Override
            public void redo() {
                super.redo();
                Iterator<FrameElementGroup> eltGroups = addedEltGroups.iterator();
                while (eltGroups.hasNext()) {
                    frame.addEltGroup(eltGroups.next());
                }
                Iterator<IWidget> remoteLabels = addedRemoteLabels.iterator();
                while (remoteLabels.hasNext()) {
                    IWidget remoteLabel = remoteLabels.next();
                    if (!frame.containsWidget(remoteLabel)) {
                        frame.addWidget(remoteLabel);
                    }
                }
            }

            @Override
            public void undo() {
                super.undo();
                Iterator<FrameElementGroup> eltGroups = addedEltGroups.iterator();
                while (eltGroups.hasNext()) {
                    frame.removeEltGroup(eltGroups.next());
                }
                Iterator<IWidget> remoteLabels = addedRemoteLabels.iterator();
                while (remoteLabels.hasNext()) {
                    IWidget remoteLabel = remoteLabels.next();
                    if (frame.containsWidget(remoteLabel)) {
                        frame.removeWidget(remoteLabel);
                    }
                }
            }
        };
        editSequence.addEdit(edit);
        return numPasted;
    }
    return 0;
}
Also used : SimpleWidgetGroup(edu.cmu.cs.hcii.cogtool.model.SimpleWidgetGroup) FrameElementGroup(edu.cmu.cs.hcii.cogtool.model.FrameElementGroup) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit) IWidget(edu.cmu.cs.hcii.cogtool.model.IWidget) HashSet(java.util.HashSet) GridButtonGroup(edu.cmu.cs.hcii.cogtool.model.GridButtonGroup)

Example 40 with IUndoableEdit

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

the class DemoScriptCmd method regenerateScripts.

public static boolean regenerateScripts(Project project, Demonstration demo, Script exceptScript, DemoStateManager demoStateMgr, Interaction interaction, IUndoableEditSequence editSeq) {
    if (demo.isObsolete()) /*&& ! demo.isInvalid()*/
    {
        final DemoStateManager.IConformanceUndoRedo conformanceUndoRedo = demoStateMgr.restoreConformance(demo);
        // Collection of ScriptUndoRedo instances
        final Collection<ComputationUndoRedo> scriptsUndoRedoData = regenerateScripts(demo, 0, demo.getStepAt(0), interaction);
        IUndoableEdit edit = new AUndoableEdit(SEDemoLID.RegenerateScript) {

            @Override
            public String getPresentationName() {
                return L10N.get("UNDO.FC.RegenerateScript", "Regenerate Script(s)");
            }

            @Override
            public void redo() {
                super.redo();
                conformanceUndoRedo.redo();
                redoAllChanges(scriptsUndoRedoData);
            }

            @Override
            public void undo() {
                super.undo();
                conformanceUndoRedo.undo();
                undoAllChanges(scriptsUndoRedoData);
            }
        };
        editSeq.addEdit(edit);
        addUndoableEditToScripts(editSeq, demo, exceptScript, project);
    }
    return true;
}
Also used : AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit)

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