Search in sources :

Example 6 with CompoundUndoableEdit

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

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

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

the class ProjectController method regenerateScripts.

protected boolean regenerateScripts(ProjectSelectionState seln) {
    Design design = seln.getSelectedDesign();
    AUndertaking[] tasks = seln.getSelectedTasks(TaskSelectionState.PRUNE_SELECTION);
    String editLabel = ui.hasMultipleScripts(seln) ? REGENERATE_SCRIPTS : REGENERATE_SCRIPT;
    CompoundUndoableEdit editSequence = new CompoundUndoableEdit(editLabel, ProjectLID.RegenerateScript);
    editSequence.setManager(undoMgr);
    if (design != null) {
        DemoStateManager demoStateMgr = DemoStateManager.getStateManager(project, design);
        if ((tasks != null) && (tasks.length > 0)) {
            for (int i = 0; i < tasks.length; i++) {
                if (!regenerateScripts(tasks[i], design, demoStateMgr, editSequence)) {
                    return false;
                }
            }
        } else {
            Iterator<AUndertaking> allTasks = project.getUndertakings().iterator();
            while (allTasks.hasNext()) {
                if (!regenerateScripts(allTasks.next(), design, demoStateMgr, editSequence)) {
                    return false;
                }
            }
        }
    } else if ((tasks != null) && (tasks.length > 0)) {
        for (int i = 0; i < tasks.length; i++) {
            Iterator<Design> allDesigns = project.getDesigns().iterator();
            while (allDesigns.hasNext()) {
                design = allDesigns.next();
                DemoStateManager demoStateMgr = DemoStateManager.getStateManager(project, design);
                if (!regenerateScripts(tasks[i], design, demoStateMgr, editSequence)) {
                    return false;
                }
            }
        }
    }
    if (editSequence.isSignificant()) {
        editSequence.end();
        undoMgr.addEdit(editSequence);
    }
    return true;
}
Also used : Design(edu.cmu.cs.hcii.cogtool.model.Design) ITaskDesign(edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign) AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) Iterator(java.util.Iterator) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint)

Example 9 with CompoundUndoableEdit

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

the class DesignEditorController method createNewTransitionAction.

// renameFrame
protected IListenerAction createNewTransitionAction() {
    return new IListenerAction() {

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

        public boolean performAction(Object prms) {
            DesignEditorUI.NewTransitionParameters newPrms = (DesignEditorUI.NewTransitionParameters) prms;
            if (newPrms != null) {
                CompoundUndoableEdit editSequence = null;
                if (newPrms.target == null) {
                    editSequence = new CompoundUndoableEdit(DesignEditorCmd.NEW_TRANSITION, DesignEditorLID.NewTransition);
                    newPrms.target = createNewFrame(newPrms.x, newPrms.y, editSequence);
                }
                IUndoableEdit edit = createNewTransition(newPrms.source, newPrms.target);
                // null is returned if the operation is canceled.
                if (edit != null) {
                    if (editSequence != null) {
                        editSequence.addEdit(edit);
                        editSequence.end();
                        edit = editSequence;
                    }
                    undoMgr.addEdit(edit);
                    if (editSequence != null) {
                        ui.initiateFrameRename(newPrms.target);
                    }
                    return true;
                } else if (editSequence != null) {
                    editSequence.end();
                    editSequence.undo();
                }
                return false;
            } else {
                throw new RcvrUIException("Cannot create transition without parameters.");
            }
        }
    };
}
Also used : DesignEditorUI(edu.cmu.cs.hcii.cogtool.ui.DesignEditorUI) IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit) RcvrUIException(edu.cmu.cs.hcii.cogtool.util.RcvrUIException)

Example 10 with CompoundUndoableEdit

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

the class DesignEditorController method createPasteAction.

protected IListenerAction createPasteAction() {
    return new AListenerAction() {

        public boolean performAction(Object prms) {
            try {
                Collection<Object> objects = CogToolClipboard.fetchCogToolObjects();
                if ((objects != null) && (objects.size() > 0)) {
                    CompoundUndoableEdit editSequence = new CompoundUndoableEdit(L10N.get("UNDO.Paste", "Paste"), DesignEditorLID.Paste);
                    Set<DeviceType> devTypes = design.getDeviceTypes();
                    int numPasted = 0;
                    Iterator<Object> objIt = objects.iterator();
                    while (objIt.hasNext()) {
                        Object o = objIt.next();
                        if (o instanceof Frame) {
                            Frame frame = (Frame) o;
                            makeFrameNameUnique(frame);
                            // Find an unoccupied starting position
                            // by cascading.
                            DoublePoint origin = frame.getFrameOrigin();
                            DesignUtil.findDistinctOrigin(design, origin, 16.0, 16.0);
                            // Union devices
                            Iterator<InputDevice> frameDevices = frame.getInputDevices().iterator();
                            while (frameDevices.hasNext()) {
                                InputDevice inputDevice = frameDevices.next();
                                DeviceType devType = inputDevice.getDeviceType();
                                if (!devTypes.contains(devType)) {
                                    DesignCmd.addDevice(design, devType);
                                }
                            }
                            Iterator<DeviceType> designDevTypes = devTypes.iterator();
                            while (designDevTypes.hasNext()) {
                                DeviceType devType = designDevTypes.next();
                                if (frame.getInputDevice(devType) == null) {
                                    frame.addInputDevice(devType);
                                }
                            }
                            addFrame(frame, editSequence);
                            numPasted++;
                        } else if (o instanceof Transition) {
                            Transition t = (Transition) o;
                            DeviceType device = t.getAction().getDefaultDeviceType();
                            if (!devTypes.contains(device)) {
                                DesignCmd.addDevice(design, device);
                            }
                            IUndoableEdit edit = DesignEditorCmd.addTransition(demoStateMgr, t);
                            editSequence.addEdit(edit);
                            numPasted++;
                        }
                    }
                    editSequence.end();
                    undoMgr.addEdit(editSequence);
                    interaction.setStatusMessage(numPasted + " " + pasteComplete);
                } else {
                    interaction.setStatusMessage(nothingPasted);
                }
            } catch (IOException e) {
                throw new RcvrClipboardException(e);
            } catch (ParserConfigurationException e) {
                throw new RcvrClipboardException(e);
            } catch (SAXException e) {
                throw new RcvrClipboardException(e);
            } catch (ClipboardUtil.ClipboardException e) {
                throw new RcvrClipboardException(e);
            }
            return true;
        }
    };
}
Also used : Frame(edu.cmu.cs.hcii.cogtool.model.Frame) InputDevice(edu.cmu.cs.hcii.cogtool.model.InputDevice) AListenerAction(edu.cmu.cs.hcii.cogtool.util.AListenerAction) RcvrClipboardException(edu.cmu.cs.hcii.cogtool.util.RcvrClipboardException) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException) IOException(java.io.IOException) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint) SAXException(org.xml.sax.SAXException) DeviceType(edu.cmu.cs.hcii.cogtool.model.DeviceType) ClipboardUtil(edu.cmu.cs.hcii.cogtool.util.ClipboardUtil) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint) Transition(edu.cmu.cs.hcii.cogtool.model.Transition) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

CompoundUndoableEdit (edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit)42 IListenerAction (edu.cmu.cs.hcii.cogtool.util.IListenerAction)19 AUndertaking (edu.cmu.cs.hcii.cogtool.model.AUndertaking)13 IWidget (edu.cmu.cs.hcii.cogtool.model.IWidget)13 IUndoableEdit (edu.cmu.cs.hcii.cogtool.util.IUndoableEdit)12 AUndoableEdit (edu.cmu.cs.hcii.cogtool.util.AUndoableEdit)10 DoublePoint (edu.cmu.cs.hcii.cogtool.model.DoublePoint)9 FrameEditorUI (edu.cmu.cs.hcii.cogtool.ui.FrameEditorUI)8 FrameElement (edu.cmu.cs.hcii.cogtool.model.FrameElement)7 Demonstration (edu.cmu.cs.hcii.cogtool.model.Demonstration)6 Design (edu.cmu.cs.hcii.cogtool.model.Design)6 Frame (edu.cmu.cs.hcii.cogtool.model.Frame)6 TaskApplication (edu.cmu.cs.hcii.cogtool.model.TaskApplication)6 TaskGroup (edu.cmu.cs.hcii.cogtool.model.TaskGroup)6 ITaskDesign (edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign)5 SimpleWidgetGroup (edu.cmu.cs.hcii.cogtool.model.SimpleWidgetGroup)5 IOException (java.io.IOException)5 HashSet (java.util.HashSet)5 CogToolLID (edu.cmu.cs.hcii.cogtool.CogToolLID)4 ComputationUndoRedo (edu.cmu.cs.hcii.cogtool.controller.DemoScriptCmd.ComputationUndoRedo)4