Search in sources :

Example 36 with AUndoableEdit

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

the class ProjectController method createImportAction.

protected IListenerAction createImportAction() {
    return new IListenerAction() {

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

        public boolean performAction(Object prms) {
            boolean computeScripts = CogToolPref.COMPSCR.getBoolean();
            if (importFile == null) {
                importFile = interaction.selectXMLFile(true, null);
            } else {
                computeScripts = importFileComputes;
            }
            if (importFile == null) {
                return false;
            }
            CompoundUndoableEdit editSeq = new CompoundUndoableEdit(importXMLPresentation, ProjectLID.ImportXML);
            Map<Design, Collection<Demonstration>> parsedDesigns = null;
            Set<AUndertaking> newUndertakings = null;
            TaskParent parent = project;
            try {
                if (CogToolPref.HCIPA.getBoolean()) {
                    TaskGroup importGroup = new TaskGroup(importFile.getName(), GroupNature.SUM);
                    parent = importGroup;
                    project.addUndertaking(importGroup);
                    editSeq.addEdit(createNewTaskUndo(project, Project.AT_END, importGroup, ProjectLID.ImportXML, importXMLPresentation));
                }
                ImportCogToolXML importer = new ImportCogToolXML();
                if (!importer.importXML(importFile, parent, MODELGEN_ALG) || (importer.getDesigns().size() == 0)) {
                    List<String> errors = importer.getObjectFailures();
                    if ((errors != null) && (errors.size() > 0)) {
                        interaction.reportProblems(importXMLPresentation, errors);
                    } else {
                        interaction.reportProblem(importXMLPresentation, xmlParseFailed);
                    }
                    return false;
                }
                List<String> warnings = importer.getGenerationWarnings();
                if (warnings.size() > 0) {
                    interaction.reportWarnings(importXMLPresentation, warnings);
                }
                parsedDesigns = importer.getDesigns();
                newUndertakings = importer.getNewUndertakings();
            } catch (ImportCogToolXML.ImportFailedException ex) {
                throw new RcvrXMLParsingException("Missing XML component", ex);
            } catch (GraphicsUtil.ImageException ex) {
                throw new RcvrImageException("Image error during loading XML", ex);
            } catch (IOException ex) {
                throw new RcvrXMLParsingException("IO error loading XML", ex);
            } catch (SAXException ex) {
                throw new RcvrXMLParsingException("Error parsing XML", ex);
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                importFile = null;
                importFileComputes = false;
            }
            for (AUndertaking u : newUndertakings) {
                parent.addUndertaking(u);
                final AUndertaking uu = u;
                final TaskParent pp = parent;
                editSeq.addEdit(new AUndoableEdit(ProjectLID.ImportXML) {

                    @Override
                    public void redo() {
                        super.redo();
                        pp.addUndertaking(uu);
                    }

                    @Override
                    public void undo() {
                        super.undo();
                        pp.removeUndertaking(uu);
                    }
                });
            }
            Iterator<Map.Entry<Design, Collection<Demonstration>>> designDemos = parsedDesigns.entrySet().iterator();
            while (designDemos.hasNext()) {
                Map.Entry<Design, Collection<Demonstration>> entry = designDemos.next();
                importDesign(parent, (DesignSelectionState) prms, entry.getKey(), entry.getValue(), newUndertakings, editSeq);
            }
            editSeq.end();
            undoMgr.addEdit(editSeq);
            if (computeScripts) {
                //compute predictions for imported project
                ProjectContextSelectionState seln = new ProjectContextSelectionState(project);
                seln.addSelectedDesigns(project.getDesigns());
                ui.selectAllTasks();
                recomputeScripts(seln);
                ui.deselectAllTasks();
            }
            return true;
        }
    };
}
Also used : ProjectContextSelectionState(edu.cmu.cs.hcii.cogtool.ui.ProjectContextSelectionState) SAXException(org.xml.sax.SAXException) Design(edu.cmu.cs.hcii.cogtool.model.Design) ITaskDesign(edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign) URLCrawlEntry(edu.cmu.cs.hcii.cogtool.model.URLCrawlEntry) ImportCogToolXML(edu.cmu.cs.hcii.cogtool.model.ImportCogToolXML) IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) RcvrImageException(edu.cmu.cs.hcii.cogtool.util.RcvrImageException) TaskParent(edu.cmu.cs.hcii.cogtool.model.TaskParent) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) ProjectSelectionState(edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) IOException(java.io.IOException) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException) AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) Collection(java.util.Collection) GraphicsUtil(edu.cmu.cs.hcii.cogtool.util.GraphicsUtil) RcvrXMLParsingException(edu.cmu.cs.hcii.cogtool.util.RcvrXMLParsingException) Demonstration(edu.cmu.cs.hcii.cogtool.model.Demonstration) TaskGroup(edu.cmu.cs.hcii.cogtool.model.TaskGroup) Map(java.util.Map) HashMap(java.util.HashMap)

Example 37 with AUndoableEdit

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

the class SEDemoController method setMouseHandAction.

// resetComputations
protected void setMouseHandAction(final boolean mouseHand) {
    final Demonstration demo = script.getDemonstration();
    final boolean oldMouseHand = demo.getMouseHand();
    final DefaultModelGeneratorState initialState = demo.getInitialState();
    final HandLocation mouseHandLoc = initialState.getHandLocation(oldMouseHand);
    final DemoStateManager.IConformanceUndoRedo conformanceUndoRedo = demoStateMgr.restoreConformance(demo);
    demo.setMouseHand(mouseHand);
    initialState.setHandLocation(mouseHand, mouseHandLoc);
    initialState.setHandLocation(!mouseHand, HandLocation.OnKeyboard);
    final Collection<ComputationUndoRedo> scriptsUndoRedos = DemoScriptCmd.regenerateScripts(demo, 0, demo.getStepAt(0), interaction);
    IUndoableEdit edit = new AUndoableEdit(SEDemoLID.SetMouseHand) {

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

        @Override
        public void redo() {
            super.redo();
            conformanceUndoRedo.redo();
            initialState.setHandLocation(mouseHand, mouseHandLoc);
            initialState.setHandLocation(!mouseHand, HandLocation.OnKeyboard);
            // Do this last as it will alert
            demo.setMouseHand(mouseHand);
            DemoScriptCmd.redoAllChanges(scriptsUndoRedos);
        }

        @Override
        public void undo() {
            super.undo();
            conformanceUndoRedo.undo();
            initialState.setHandLocation(oldMouseHand, mouseHandLoc);
            initialState.setHandLocation(!oldMouseHand, HandLocation.OnKeyboard);
            // Do this last as it will alert
            demo.setMouseHand(oldMouseHand);
            DemoScriptCmd.undoAllChanges(scriptsUndoRedos);
        }
    };
    undoMgr.addEdit(edit);
}
Also used : HandLocation(edu.cmu.cs.hcii.cogtool.model.HandLocation) ComputationUndoRedo(edu.cmu.cs.hcii.cogtool.controller.DemoScriptCmd.ComputationUndoRedo) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit) Demonstration(edu.cmu.cs.hcii.cogtool.model.Demonstration) DefaultModelGeneratorState(edu.cmu.cs.hcii.cogtool.model.DefaultModelGeneratorState)

Example 38 with AUndoableEdit

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

the class SEDemoController method performChangeThink.

// performInsertThink
protected boolean performChangeThink(SEDemoSelectionState selection) {
    DefaultModelGeneratorState selectedState = selection.getSelectedState();
    if ((selectedState == null) || !(selectedState.getScriptStep() instanceof ThinkScriptStep)) {
        interaction.protestNotThinkStep();
        return false;
    }
    final ThinkScriptStep thinkStep = (ThinkScriptStep) selectedState.getScriptStep();
    final double oldDuration = thinkStep.getThinkDuration();
    final String oldLabel = thinkStep.getLabel();
    final SEDemoInteraction.TimedActionData data = getTimedActionData(oldDuration, oldLabel, IS_THINK);
    if (data == null) {
        return false;
    }
    thinkStep.setThinkDuration(data.duration);
    thinkStep.setLabel(data.labelString);
    final Collection<ComputationUndoRedo> computeUndoRedos = resetComputations();
    IUndoableEdit edit = new AUndoableEdit(SEDemoLID.ChangeThinkProperties) {

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

        @Override
        public void redo() {
            super.redo();
            thinkStep.setThinkDuration(data.duration);
            thinkStep.setLabel(data.labelString);
            DemoScriptCmd.redoAllChanges(computeUndoRedos);
        }

        @Override
        public void undo() {
            super.undo();
            thinkStep.setThinkDuration(oldDuration);
            thinkStep.setLabel(oldLabel);
            DemoScriptCmd.undoAllChanges(computeUndoRedos);
        }
    };
    undoMgr.addEdit(edit);
    return true;
}
Also used : ComputationUndoRedo(edu.cmu.cs.hcii.cogtool.controller.DemoScriptCmd.ComputationUndoRedo) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit) SEDemoInteraction(edu.cmu.cs.hcii.cogtool.ui.SEDemoInteraction) DefaultModelGeneratorState(edu.cmu.cs.hcii.cogtool.model.DefaultModelGeneratorState) ThinkScriptStep(edu.cmu.cs.hcii.cogtool.model.ThinkScriptStep)

Example 39 with AUndoableEdit

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

the class SEFrameChooserController method createOpenDemonstrationWindowAction.

/**
     * Create the window for demonstrating the entire script.
     */
protected IListenerAction createOpenDemonstrationWindowAction() {
    return new AListenerAction() {

        public boolean performAction(Object prms) {
            // Ensure there is a selected start frame.
            final Demonstration demo = taskApp.getDemonstration();
            final Script script = taskApp.getScript(modelGen);
            if (demo.getStartFrame() != null) {
                demo.setStartFrameChosen(true);
                final Collection<ComputationUndoRedo> scriptsUndoRedos = DemoScriptCmd.regenerateScripts(demo, 0, demo.getStepAt(0), interaction);
                // Close the current window.
                closeWindow(false);
                // Open the new demo view window
                try {
                    SEDemoController.openController(taskApp, modelGen, project);
                } catch (GraphicsUtil.ImageException ex) {
                    interaction.protestInvalidImageFile();
                }
                IUndoableEdit edit = new AUndoableEdit(SEFrameChooserLID.OpenScriptEditor) {

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

                    @Override
                    public void redo() {
                        super.redo();
                        if (demo.getStartFrame() != null) {
                            demo.setStartFrameChosen(true);
                            DemoScriptCmd.redoAllChanges(scriptsUndoRedos);
                            // Close the frame chooser window.
                            DefaultController frameController = ControllerRegistry.ONLY.findOpenController(taskApp);
                            if (frameController != null) {
                                frameController.closeWindow(false);
                            }
                            // Open the new demo view window
                            try {
                                SEDemoController.openController(taskApp, modelGen, project);
                            } catch (GraphicsUtil.ImageException ex) {
                                interaction.protestInvalidImageFile();
                            }
                        }
                    }

                    @Override
                    public void undo() {
                        super.undo();
                        DefaultController seDemoController = ControllerRegistry.ONLY.findOpenController(script);
                        if (seDemoController != null) {
                            seDemoController.closeWindow(false);
                        }
                        demo.setStartFrameChosen(false);
                        DemoScriptCmd.undoAllChanges(scriptsUndoRedos);
                        SEFrameChooserController.openController(taskApp, modelGen, project);
                    }
                };
                UndoManager scriptUndoMgr = UndoManager.getUndoManager(script, project);
                scriptUndoMgr.addEdit(edit);
                undoMgr.addEdit(edit);
                return true;
            }
            interaction.protestNoSelection();
            return false;
        }
    };
}
Also used : Script(edu.cmu.cs.hcii.cogtool.model.Script) UndoManager(edu.cmu.cs.hcii.cogtool.util.UndoManager) AListenerAction(edu.cmu.cs.hcii.cogtool.util.AListenerAction) ComputationUndoRedo(edu.cmu.cs.hcii.cogtool.controller.DemoScriptCmd.ComputationUndoRedo) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) GraphicsUtil(edu.cmu.cs.hcii.cogtool.util.GraphicsUtil) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit) Demonstration(edu.cmu.cs.hcii.cogtool.model.Demonstration)

Example 40 with AUndoableEdit

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

the class SNIFACTCmd method addTasksToGroup.

/**
     * Creates an undoable edit for the action of adding the list of tasks
     * stored in the execution context to the given task group.
     */
protected static IUndoableEdit addTasksToGroup(final Project project, final TaskGroup group, final SNIFACTExecContext context, final String undoLabel) {
    return new AUndoableEdit(ProjectLID.RecomputeScript) {

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

        protected boolean recoverMgrs = false;

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

        @Override
        public void redo() {
            super.redo();
            recoverMgrs = false;
            List<AUndertaking> tasks = context.getTasks();
            for (int i = 0; i < tasks.size(); i++) {
                AUndertaking curTask = tasks.get(i);
                group.addUndertaking(curTask);
                project.restoreRemovedTaskApplications(associatedTAs[i]);
            }
        }

        @Override
        @SuppressWarnings("unchecked")
        public void undo() {
            super.undo();
            recoverMgrs = true;
            List<AUndertaking> tasks = context.getTasks();
            int size = tasks.size();
            if (associatedTAs == null) {
                associatedTAs = new Map[size];
            }
            // delete children; IMPORTANT: reverse order!
            for (int i = tasks.size() - 1; 0 <= i; i--) {
                AUndertaking curTask = tasks.get(i);
                associatedTAs[i] = project.taskApplicationsForRemovedTask(curTask);
                group.removeUndertaking(curTask);
            }
        }

        @Override
        public void die() {
            super.die();
            if (recoverMgrs) {
                for (Map<ITaskDesign, TaskApplication> associatedTA : associatedTAs) {
                    UndoManagerRecovery.recoverScriptManagers(project, associatedTA, true);
                }
            }
        }
    };
}
Also used : ITaskDesign(edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign) AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication)

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