Search in sources :

Example 61 with AUndoableEdit

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

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

Example 63 with AUndoableEdit

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

the class DesignEditorController method setWidgetColorForFrames.

/**
	 * Sets the widget color on all widgets contained within the given frames
	 *
	 * @param frames an array of IFrames
	 * @param color integer containing new widget color
	 */
protected void setWidgetColorForFrames(final Frame[] frames, final int color) {
    // save previous data for undo/redo
    final Map<Frame, Integer> previousWidgetColorData = getWidgetColorData(frames);
    // do operation on all frames
    for (Frame frame : frames) {
        frame.setWidgetColor(color);
    }
    // Add the undo edit
    IUndoableEdit edit = new AUndoableEdit(DesignEditorLID.SetWidgetColor) {

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

        @Override
        public void redo() {
            super.redo();
            // selected frames
            for (Frame frame : frames) {
                frame.setWidgetColor(color);
            }
        }

        @Override
        public void undo() {
            super.undo();
            // Iterate through frames, resetting widget colors
            // to old values
            Iterator<Entry<Frame, Integer>> colorEntryIterator = previousWidgetColorData.entrySet().iterator();
            while (colorEntryIterator.hasNext()) {
                Entry<Frame, Integer> colorEntry = colorEntryIterator.next();
                Frame f = colorEntry.getKey();
                int oldColor = colorEntry.getValue().intValue();
                f.setWidgetColor(oldColor);
            }
        }
    };
    undoMgr.addEdit(edit);
    for (Frame frame : frames) {
        UndoManager frameMgr = UndoManager.getUndoManager(frame, project);
        frameMgr.addEdit(edit);
    }
}
Also used : Frame(edu.cmu.cs.hcii.cogtool.model.Frame) Entry(java.util.Map.Entry) UndoManager(edu.cmu.cs.hcii.cogtool.util.UndoManager) 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 64 with AUndoableEdit

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

the class ComputePredictionCmd method computeAllPredictions.

/**
     * Utility to recompute in the main thread all the results for
     * all the scripts of a TaskApplication, using the given callback.
     */
public static IUndoableEdit computeAllPredictions(Project project, final TaskApplication ta, final IPredictionAlgo compute, boolean inBackground, Interaction interaction) {
    // The list of old results that were replaced.
    final Map<CognitiveModelGenerator, APredictionResult> oldResults = new HashMap<CognitiveModelGenerator, APredictionResult>();
    // It is possible the set of old results does not include one for the
    // specified computation algorithm; after enumerating, track new
    // results for that algorithm (needed so that we can unset results
    // in the undo action!).
    final List<APredictionResult> ensuredResults = new ArrayList<APredictionResult>();
    Iterator<CognitiveModelGenerator> modelGens = ta.getModelGenerators();
    int obsoleteWaitContainingResults = 0;
    while (modelGens.hasNext()) {
        CognitiveModelGenerator modelGen = modelGens.next();
        APredictionResult oldResult = ta.getResult(modelGen, compute);
        // oldResult may be null if not set
        oldResults.put(modelGen, oldResult);
        Script script = ta.getScript(modelGen);
        APredictionResult ensureResult;
        if (inBackground) {
            ensureResult = computeInBackground(compute, script, interaction);
        } else {
            ensureResult = computePrediction(compute, script, null);
        }
        if (ACTRPredictionAlgo.usesObsoleteWaits(ensureResult)) {
            ++obsoleteWaitContainingResults;
        }
        ensuredResults.add(ensureResult);
        ta.setResult(modelGen, compute, ensureResult);
    }
    if (obsoleteWaitContainingResults > 0) {
        interaction.protestObsoleteWaits();
    }
    if (ensuredResults.size() > 0) {
        IUndoableEdit edit = new AUndoableEdit(ProjectLID.RecomputeScript) {

            @Override
            public String getPresentationName() {
                return L10N.get("UNDO.PM.RecomputeScript(s)", "Recompute Script(s)");
            }

            protected void setResults(List<APredictionResult> results) {
                int numResults = results.size();
                for (int i = 0; i < numResults; i++) {
                    APredictionResult result = results.get(i);
                    result = PredictionResultProxy.getLatestResult(result);
                    results.set(i, result);
                    ta.setResult(result.getScript().getModelGenerator(), result.getPredictionAlgorithm(), result);
                }
            }

            @Override
            public void redo() {
                super.redo();
                setResults(ensuredResults);
            }

            @Override
            public void undo() {
                super.undo();
                Iterator<Map.Entry<CognitiveModelGenerator, APredictionResult>> resetResults = oldResults.entrySet().iterator();
                while (resetResults.hasNext()) {
                    Map.Entry<CognitiveModelGenerator, APredictionResult> entry = resetResults.next();
                    // key is modelGen, value part is the old result
                    CognitiveModelGenerator modelGen = entry.getKey();
                    APredictionResult oldResult = entry.getValue();
                    if (oldResult == null) {
                        ta.unsetResult(modelGen, compute);
                    } else {
                        ta.setResult(modelGen, compute, oldResult);
                    }
                }
            }
        };
        addUndoableEditToScripts(edit, ta, project);
        return edit;
    }
    return null;
}
Also used : Script(edu.cmu.cs.hcii.cogtool.model.Script) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) APredictionResult(edu.cmu.cs.hcii.cogtool.model.APredictionResult) CognitiveModelGenerator(edu.cmu.cs.hcii.cogtool.model.CognitiveModelGenerator) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) ArrayList(java.util.ArrayList) List(java.util.List) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit) HashMap(java.util.HashMap) Map(java.util.Map)

Example 65 with AUndoableEdit

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

the class DesignEditorController method moveFrame.

protected void moveFrame(final Frame frame, final double dx, final double dy, IUndoableEditSequence editSequence) {
    // Create a new point, so we can modify it without side-effects
    final DoublePoint oldP = new DoublePoint(frame.getFrameOrigin());
    final DoublePoint p = new DoublePoint(oldP);
    // Compute the new location.
    p.x += dx;
    p.y += dy;
    // Ensure that it is not less then 0,0
    if (p.x < 0) {
        p.x = 0;
    }
    if (p.y < 0) {
        p.y = 0;
    }
    frame.setFrameOrigin(p);
    editSequence.addEdit(new AUndoableEdit(DesignEditorLID.MoveFrames) {

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

        @Override
        public void redo() {
            super.redo();
            frame.setFrameOrigin(p);
        }

        @Override
        public void undo() {
            super.undo();
            frame.setFrameOrigin(oldP);
        }
    });
}
Also used : DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit)

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