Search in sources :

Example 1 with UndoManager

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

Example 2 with UndoManager

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

the class ComputePredictionCmd method addUndoableEditToScripts.

/**
     * Adds the edit for each of the scripts in the demonstration.
     * @param seq
     * @param demo
     * @param project
     */
protected static void addUndoableEditToScripts(IUndoableEdit edit, TaskApplication taskApp, Project project) {
    Iterator<CognitiveModelGenerator> modelGens = taskApp.getModelGenerators();
    while (modelGens.hasNext()) {
        CognitiveModelGenerator modelGen = modelGens.next();
        Script script = taskApp.getScript(modelGen);
        if (script != null) {
            UndoManager undoMgr = UndoManager.getUndoManager(script, project);
            if (undoMgr != null) {
                undoMgr.addEdit(edit);
            }
        }
    }
}
Also used : Script(edu.cmu.cs.hcii.cogtool.model.Script) UndoManager(edu.cmu.cs.hcii.cogtool.util.UndoManager) CognitiveModelGenerator(edu.cmu.cs.hcii.cogtool.model.CognitiveModelGenerator)

Example 3 with UndoManager

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

the class DesignEditorController method setBackgroundImageOnFrames.

/**
	 * Sets the background image for the array of frames
	 *
	 * @param frames an array of IFrames
	 * @param imageData the new image, or null if none
	 * @param imageURL the file path of the new image, or
	 * <code>WidgetAttributes.NO_IMAGE</code>
	 */
protected void setBackgroundImageOnFrames(final Frame[] frames, final byte[] imageData, final String imageURL, CogToolLID editLID) {
    try {
        // compute the size of the new image.
        final DoubleRectangle imageSize = GraphicsUtil.getImageBounds(imageData);
        // save previous data for undo/redo
        final Map<Frame, ImageData> previousImageData = getBackgroundImageData(frames);
        // do operation on all frames
        for (Frame frame : frames) {
            frame.setBackgroundImage(imageData, imageSize);
            frame.setAttribute(WidgetAttributes.IMAGE_PATH_ATTR, imageURL);
        }
        // Add the undo edit
        IUndoableEdit edit = new AUndoableEdit(editLID) {

            @Override
            public String getPresentationName() {
                return (imageData == null) ? removeBackgroundImage : setBackgroundImage;
            }

            @Override
            public void redo() {
                super.redo();
                // selected frames
                try {
                    for (Frame frame : frames) {
                        frame.setBackgroundImage(imageData, imageSize);
                        frame.setAttribute(WidgetAttributes.IMAGE_PATH_ATTR, imageURL);
                    }
                } catch (GraphicsUtil.ImageException ex) {
                    throw new RcvrImageException("Redo set background image failed", ex);
                }
            }

            @Override
            public void undo() {
                super.undo();
                try {
                    // iterate through frames
                    Iterator<Entry<Frame, ImageData>> imageEntryIterator = previousImageData.entrySet().iterator();
                    while (imageEntryIterator.hasNext()) {
                        Entry<Frame, ImageData> imageEntry = imageEntryIterator.next();
                        Frame f = imageEntry.getKey();
                        ImageData id = imageEntry.getValue();
                        f.setBackgroundImage(id.data, id.bounds);
                        f.setAttribute(WidgetAttributes.IMAGE_PATH_ATTR, id.imageURL);
                    }
                } catch (GraphicsUtil.ImageException ex) {
                    throw new RcvrImageException("Undo set background image failed", ex);
                }
            }
        };
        undoMgr.addEdit(edit);
        for (Frame frame : frames) {
            UndoManager frameMgr = UndoManager.getUndoManager(frame, project);
            frameMgr.addEdit(edit);
        }
    } catch (GraphicsUtil.ImageException e) {
        // setBackgroundImage and GraphicsUtil.getImageBounds may
        // throw ImageException, translating any other exception.
        interaction.protestInvalidImageFile();
    }
}
Also used : Frame(edu.cmu.cs.hcii.cogtool.model.Frame) DoubleRectangle(edu.cmu.cs.hcii.cogtool.model.DoubleRectangle) Entry(java.util.Map.Entry) UndoManager(edu.cmu.cs.hcii.cogtool.util.UndoManager) RcvrImageException(edu.cmu.cs.hcii.cogtool.util.RcvrImageException) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit) GraphicsUtil(edu.cmu.cs.hcii.cogtool.util.GraphicsUtil)

Example 4 with UndoManager

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

the class DesignEditorCmd method addFrame.

// pasteElements
public static void addFrame(final Project project, final Design design, final DemoStateManager demoStateMgr, final Frame frame, IUndoableEditSequence editSequence) {
    design.addFrame(frame);
    Collection<Object> objects = FrameTemplateSupport.getFrameTemplate(design);
    if ((objects != null) && (objects.size() > 0)) {
        CompoundUndoableEdit tplEdit = new CompoundUndoableEdit(USE_TEMPLATE, null);
        pasteElements(design, frame, objects, demoStateMgr, tplEdit);
        UndoManager undoMgr = UndoManager.getUndoManager(frame, project);
        tplEdit.end();
        undoMgr.addEdit(tplEdit);
    }
    IUndoableEdit edit = new DemoStateManager.InvalidatingEdit(DesignEditorLID.NewFrame, demoStateMgr) {

        private boolean recoverMgr = false;

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

        @Override
        public void redo() {
            super.redo();
            recoverMgr = false;
            design.addFrame(frame);
            demoStateMgr.noteFrameEdit(frame, this);
        }

        @Override
        public void undo() {
            super.undo();
            recoverMgr = true;
            design.removeFrame(frame);
            demoStateMgr.noteFrameEdit(frame, this);
        }

        @Override
        public void die() {
            super.die();
            if (recoverMgr) {
                UndoManagerRecovery.recoverManagers(project, frame);
            }
        }
    };
    editSequence.addEdit(edit);
}
Also used : UndoManager(edu.cmu.cs.hcii.cogtool.util.UndoManager) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit)

Example 5 with UndoManager

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

the class FrameEditorController method createSetSkinAction.

private AListenerAction createSetSkinAction(final SkinType newSkin, final CogToolLID lid) {
    return new AListenerAction() {

        public boolean performAction(Object prms) {
            final SkinType oldSkin = design.getSkin();
            design.setSkin(newSkin);
            IUndoableEdit edit = new AUndoableEdit(lid) {

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

                @Override
                public void redo() {
                    super.redo();
                    design.setSkin(newSkin);
                }

                @Override
                public void undo() {
                    super.undo();
                    design.setSkin(oldSkin);
                }
            };
            UndoManager designUndoMgr = UndoManager.getUndoManager(design, project);
            designUndoMgr.addEdit(edit);
            undoMgr.addEdit(edit);
            return true;
        }
    };
}
Also used : UndoManager(edu.cmu.cs.hcii.cogtool.util.UndoManager) AListenerAction(edu.cmu.cs.hcii.cogtool.util.AListenerAction) SkinType(edu.cmu.cs.hcii.cogtool.model.SkinType) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit)

Aggregations

UndoManager (edu.cmu.cs.hcii.cogtool.util.UndoManager)8 IUndoableEdit (edu.cmu.cs.hcii.cogtool.util.IUndoableEdit)7 AUndoableEdit (edu.cmu.cs.hcii.cogtool.util.AUndoableEdit)6 CognitiveModelGenerator (edu.cmu.cs.hcii.cogtool.model.CognitiveModelGenerator)3 ComputationUndoRedo (edu.cmu.cs.hcii.cogtool.controller.DemoScriptCmd.ComputationUndoRedo)2 Demonstration (edu.cmu.cs.hcii.cogtool.model.Demonstration)2 Frame (edu.cmu.cs.hcii.cogtool.model.Frame)2 Script (edu.cmu.cs.hcii.cogtool.model.Script)2 TaskApplication (edu.cmu.cs.hcii.cogtool.model.TaskApplication)2 AListenerAction (edu.cmu.cs.hcii.cogtool.util.AListenerAction)2 CompoundUndoableEdit (edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit)2 GraphicsUtil (edu.cmu.cs.hcii.cogtool.util.GraphicsUtil)2 Entry (java.util.Map.Entry)2 APredictionResult (edu.cmu.cs.hcii.cogtool.model.APredictionResult)1 AScriptStep (edu.cmu.cs.hcii.cogtool.model.AScriptStep)1 DefaultModelGeneratorState (edu.cmu.cs.hcii.cogtool.model.DefaultModelGeneratorState)1 DoublePoint (edu.cmu.cs.hcii.cogtool.model.DoublePoint)1 DoubleRectangle (edu.cmu.cs.hcii.cogtool.model.DoubleRectangle)1 IPredictionAlgo (edu.cmu.cs.hcii.cogtool.model.IPredictionAlgo)1 SkinType (edu.cmu.cs.hcii.cogtool.model.SkinType)1