Search in sources :

Example 41 with IUndoableEdit

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

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

the class DesignEditorController method createNewTransition.

// Return null to indicate cancel
protected IUndoableEdit createNewTransition(final TransitionSource source, Frame target) {
    int deviceTypes = DeviceType.buildDeviceSet(design.getDeviceTypes());
    // Helps control which options are available for editing the action
    int limitMode = ActionProperties.determineChangeActionMode(source);
    AAction action = createDefaultAction(source);
    if (action == null) {
        if (!interaction.determineNewAction(properties, deviceTypes, limitMode, L10N.get("DE.SetActionType", "Set Action Type"))) {
            // Indicate that the user canceled
            return null;
        }
        action = EditActionCmd.buildActionFromProperties(properties, deviceTypes, limitMode, interaction);
        if (action == null) {
            return null;
        }
    }
    // action is not null at this point; no transition yet for this action!
    action = EditActionCmd.ensureActionIsUnique(source, action, properties, deviceTypes, limitMode, null, interaction);
    if (action == null) {
        return null;
    }
    Transition newTransition = new Transition(source, target, action);
    newTransition.setDelayInfo(properties.delayInSecs, properties.delayLabel);
    IUndoableEdit edit = DesignEditorCmd.addTransition(demoStateMgr, newTransition);
    interaction.setTransitionStatusMessage(newTransition);
    return edit;
}
Also used : Transition(edu.cmu.cs.hcii.cogtool.model.Transition) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint) AAction(edu.cmu.cs.hcii.cogtool.model.AAction)

Example 43 with IUndoableEdit

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

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

the class DesignEditorController method renameFrame.

protected boolean renameFrame(final Frame renamedFrame, String tryName) {
    final String oldName = renamedFrame.getName();
    boolean notDone = true;
    do {
        if (tryName.length() == 0) {
            tryName = interaction.protestNameCannotBeEmpty(DEFAULT_FRAME_PREFIX);
            // If canceled, indicate so; otherwise, test again
            if (tryName == null) {
                return false;
            }
        } else {
            Frame frameForName = design.getFrame(tryName);
            // then no change is necessary!
            if (frameForName == renamedFrame) {
                notDone = false;
            } else if (frameForName != null) {
                // A non-null frame for the tryName indicates a collision
                tryName = interaction.protestNameCollision(DEFAULT_FRAME_PREFIX);
                // If canceled, indicate so; otherwise, test again
                if (tryName == null) {
                    return false;
                }
            } else {
                // Not canceled, not empty, and not a collision
                notDone = false;
                final String newName = tryName;
                renamedFrame.setName(newName);
                IUndoableEdit edit = new AUndoableEdit(DesignEditorLID.RenameFrame) {

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

                    @Override
                    public void redo() {
                        super.redo();
                        Frame testFrame = design.getFrame(newName);
                        renamedFrame.setName(newName);
                        if (testFrame != null) {
                            makeFrameNameUnique(renamedFrame);
                        }
                    }

                    @Override
                    public void undo() {
                        super.undo();
                        Frame testFrame = design.getFrame(oldName);
                        renamedFrame.setName(oldName);
                        if (testFrame != null) {
                            makeFrameNameUnique(renamedFrame);
                        }
                    }
                };
                undoMgr.addEdit(edit);
            }
        }
    } while (notDone);
    return true;
}
Also used : Frame(edu.cmu.cs.hcii.cogtool.model.Frame) 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