Search in sources :

Example 31 with AUndoableEdit

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

the class DesignEditorController method spaceFramesEqually.

/**
	 * Spaces the selected frames equally along a certain axis
	 * @param vertical true for vertical axis; false for horizontal
	 */
protected boolean spaceFramesEqually(Map<Frame, DoubleRectangle> frameMap, final boolean vertical) {
    final String undoRedoLabel = vertical ? SPACE_VERTICALLY : SPACE_HORIZONTALLY;
    CogToolLID lid = vertical ? DesignEditorLID.SpaceVertically : DesignEditorLID.SpaceHorizontally;
    CompoundUndoableEdit editSequence = new CompoundUndoableEdit(undoRedoLabel, lid);
    // Order the widgets according to location
    // (either from the left or from the top)
    Comparator<Map.Entry<Frame, DoubleRectangle>> c = vertical ? frameVerticalComparator : frameHorizontalComparator;
    @SuppressWarnings("unchecked") Map.Entry<Frame, DoubleRectangle>[] entries = new Map.Entry[frameMap.size()];
    frameMap.entrySet().toArray(entries);
    Arrays.sort(entries, c);
    // Calculate the spacing between widgets
    double sum = 0;
    double min = Double.MAX_VALUE;
    double max = Double.MIN_VALUE;
    // this can then be used to do spacing.
    for (Entry<Frame, DoubleRectangle> entrie : entries) {
        DoubleRectangle bounds = entrie.getValue();
        double size = vertical ? bounds.height : bounds.width;
        double position = vertical ? bounds.y : bounds.x;
        sum += size;
        min = Math.min(min, position);
        max = Math.max(max, size + position);
    }
    // Get the spacing to use between each item.
    double spacing = ((max - min) - sum) / (entries.length - 1);
    // Adjust the spacings to the correct values
    for (Entry<Frame, DoubleRectangle> entrie : entries) {
        // go through each frame and set the frame's origin
        final Frame f = entrie.getKey();
        final DoubleRectangle bounds = entrie.getValue();
        // TODO (dfm) this is probably the place to walk over the design pre-flighting
        //            problems we want to warn the user about, and give the opportunity
        //            to skip the export if desired
        // Determine the old point...
        final double p_old = vertical ? bounds.y : bounds.x;
        final double p_new = min;
        // Set the new location
        f.setFrameOrigin(vertical ? bounds.x : p_new, vertical ? p_new : bounds.y);
        // Advance the pointer to the next location
        min += spacing + (vertical ? bounds.height : bounds.width);
        IUndoableEdit edit = new AUndoableEdit(lid) {

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

            @Override
            public void redo() {
                super.redo();
                f.setFrameOrigin(vertical ? bounds.x : p_new, vertical ? p_new : bounds.y);
            }

            @Override
            public void undo() {
                super.undo();
                f.setFrameOrigin(vertical ? bounds.x : p_old, vertical ? p_old : bounds.y);
            }
        };
        editSequence.addEdit(edit);
    }
    editSequence.end();
    // Only add this edit if it is significant
    if (editSequence.isSignificant()) {
        undoMgr.addEdit(editSequence);
    }
    return true;
}
Also used : Frame(edu.cmu.cs.hcii.cogtool.model.Frame) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) DoubleRectangle(edu.cmu.cs.hcii.cogtool.model.DoubleRectangle) Entry(java.util.Map.Entry) CogToolLID(edu.cmu.cs.hcii.cogtool.CogToolLID) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit)

Example 32 with AUndoableEdit

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

the class DesignEditorController method renameDesign.

/**
	 * Update the design's name.
	 * The name must be unique.
	 *
	 * @param design
	 * @param newName
	 * @return
	 */
protected boolean renameDesign(final Design design, String tryName) {
    final String oldName = design.getName();
    boolean notDone = true;
    do {
        if (tryName.length() == 0) {
            tryName = interaction.protestNameCannotBeEmpty("Design");
            // If canceled, indicate so; otherwise, test again
            if (tryName == null) {
                return false;
            }
        } else {
            Design designForName = project.getDesign(tryName);
            // then no change is necessary!
            if (designForName == design) {
                notDone = false;
            } else if (designForName != null) {
                // A non-null design for the tryName indicates a collision
                tryName = interaction.protestNameCollision("Design");
                // 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;
                design.setName(newName);
                IUndoableEdit edit = new AUndoableEdit(ProjectLID.RenameDesign) {

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

                    @Override
                    public void redo() {
                        super.redo();
                        design.setName(newName);
                        makeDesignNameUnique(design);
                    }

                    @Override
                    public void undo() {
                        super.undo();
                        design.setName(oldName);
                        makeDesignNameUnique(design);
                    }
                };
                undoMgr.addEdit(edit);
            }
        }
    } while (notDone);
    return true;
}
Also used : Design(edu.cmu.cs.hcii.cogtool.model.Design) ITaskDesign(edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit)

Example 33 with AUndoableEdit

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

the class DesignEditorController method createSetSkinAction.

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

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

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

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

                @Override
                public void undo() {
                    super.undo();
                    design.setSkin(oldSkin);
                }
            });
            return true;
        }
    };
}
Also used : AListenerAction(edu.cmu.cs.hcii.cogtool.util.AListenerAction) SkinType(edu.cmu.cs.hcii.cogtool.model.SkinType) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit)

Example 34 with AUndoableEdit

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

the class HCIPACmd method setFunctionName.

public static void setFunctionName(Project project, final AUndertaking renamedTask, final String newName, CognitiveModelGenerator modelGen, final String undoRenameLabel, IUndoableEditSequence editSeq) {
    final TaskGroup parentTask = renamedTask.getTaskGroup();
    final String oldName = renamedTask.getName();
    final Object oldAttr = parentTask.getAttribute(WidgetAttributes.HCIPA_FUNCTION_ATTR);
    final String newAttr = parseFunctionName(newName);
    renamedTask.setName(newName);
    parentTask.setAttribute(WidgetAttributes.HCIPA_FUNCTION_ATTR, newAttr);
    CompoundUndoableEdit edits = new CompoundUndoableEdit(undoRenameLabel, ProjectLID.HCIPARenameTask);
    edits.addEdit(new AUndoableEdit(ProjectLID.HCIPARenameTask) {

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

        @Override
        public void redo() {
            super.redo();
            renamedTask.setName(newName);
            parentTask.setAttribute(WidgetAttributes.HCIPA_FUNCTION_ATTR, newAttr);
        }

        @Override
        public void undo() {
            super.undo();
            renamedTask.setName(oldName);
            parentTask.setAttribute(WidgetAttributes.HCIPA_FUNCTION_ATTR, oldAttr);
        }
    });
    // change the task's think step label
    Iterator<Design> designs = project.getDesigns().iterator();
    while (designs.hasNext()) {
        final ThinkScriptStep thinkStep = chgFnName(project, renamedTask, designs.next(), modelGen, DECIDE_TO_USE + newAttr);
        // Create undo/redo step and add to undo manager
        if (thinkStep != null) {
            IUndoableEdit edit = new AUndoableEdit(ProjectLID.HCIPARenameTask) {

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

                @Override
                public void redo() {
                    super.redo();
                    thinkStep.setLabel(DECIDE_TO_USE + newAttr);
                }

                @Override
                public void undo() {
                    super.undo();
                    thinkStep.setLabel(DECIDE_TO_USE + oldAttr);
                }
            };
            edits.addEdit(edit);
        }
    }
    edits.end();
    editSeq.addEdit(edits);
}
Also used : Design(edu.cmu.cs.hcii.cogtool.model.Design) ITaskDesign(edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit) TaskGroup(edu.cmu.cs.hcii.cogtool.model.TaskGroup) ThinkScriptStep(edu.cmu.cs.hcii.cogtool.model.ThinkScriptStep)

Example 35 with AUndoableEdit

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

the class ProjectController method createShowNatureAction.

// Action for ShowSum, ShowMean, ShowMin, and ShowMax
protected IListenerAction createShowNatureAction(final GroupNature nature, final CogToolLID lid) {
    return new IListenerAction() {

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

        public boolean performAction(Object prms) {
            if (prms != null) {
                final List<TaskGroup> groups = new ArrayList<TaskGroup>();
                final List<GroupNature> oldNatures = new ArrayList<GroupNature>();
                TaskSelectionState seln = (TaskSelectionState) prms;
                AUndertaking[] tasks = seln.getSelectedTasks(TaskSelectionState.FAST_SELECTION);
                // Change applies only to task group instances
                for (AUndertaking task : tasks) {
                    if (task.isTaskGroup()) {
                        TaskGroup group = (TaskGroup) task;
                        GroupNature oldNature = group.getNature();
                        // record old value for undo support
                        if (oldNature != nature) {
                            oldNatures.add(oldNature);
                            groups.add(group);
                            group.setNature(nature);
                        }
                    }
                }
                // undo/redo step and add to undo manager
                if (groups.size() > 0) {
                    undoMgr.addEdit(new AUndoableEdit(lid) {

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

                        @Override
                        public void redo() {
                            super.redo();
                            for (int i = 0; i < groups.size(); i++) {
                                TaskGroup group = groups.get(i);
                                group.setNature(nature);
                            }
                        }

                        @Override
                        public void undo() {
                            super.undo();
                            for (int i = 0; i < groups.size(); i++) {
                                TaskGroup group = groups.get(i);
                                GroupNature oldNat = oldNatures.get(i);
                                group.setNature(oldNat);
                            }
                        }
                    });
                }
            }
            return true;
        }
    };
}
Also used : ArrayList(java.util.ArrayList) IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) TaskSelectionState(edu.cmu.cs.hcii.cogtool.ui.TaskSelectionState) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) TaskGroup(edu.cmu.cs.hcii.cogtool.model.TaskGroup) GroupNature(edu.cmu.cs.hcii.cogtool.model.GroupNature)

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