Search in sources :

Example 26 with CompoundUndoableEdit

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

the class ProjectController method createDuplicateTasksAction.

protected IListenerAction createDuplicateTasksAction() {
    return new IListenerAction() {

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

        public boolean performAction(Object prms) {
            TaskSelectionState seln = (TaskSelectionState) prms;
            AUndertaking[] tasks = seln.getSelectedTasks(TaskSelectionState.ORDER_SELECTION);
            // Can only duplicate if one or more tasks are selected
            if ((tasks != null) && (tasks.length > 0)) {
                String presentationName = (tasks.length > 1) ? DUPLICATE_TASKS : DUPLICATE_TASK;
                CompoundUndoableEdit editSeq = new CompoundUndoableEdit(presentationName, ProjectLID.DuplicateTask);
                AUndertaking lastDuplicate = null;
                for (AUndertaking task : tasks) {
                    TaskParent parent = project.getTaskParent(task);
                    List<AUndertaking> parentUndertakings = parent.getUndertakings();
                    int atIndex = parentUndertakings.indexOf(task) + 1;
                    lastDuplicate = duplicateTask(task, atIndex, parent, parentUndertakings, ProjectLID.DuplicateTask, presentationName, editSeq);
                }
                // Done with undo/redo steps; add the compound change
                // to the undo manager.
                editSeq.end();
                undoMgr.addEdit(editSeq);
                if (tasks.length == 1) {
                    ui.initiateTaskRename(lastDuplicate);
                }
            } else {
                interaction.protestNoSelection();
            }
            return true;
        }
    };
}
Also used : IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) TaskParent(edu.cmu.cs.hcii.cogtool.model.TaskParent) AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) TaskSelectionState(edu.cmu.cs.hcii.cogtool.ui.TaskSelectionState) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint)

Example 27 with CompoundUndoableEdit

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

the class FrameEditorController method createChangeTypeAction.

/**
     * Create a listener action for changing the type.
     * Changing the type can be a dangerous option.
     * If you change the type and thus change what kind of
     * transition you can use, it may play havoc on scripts, and design view.
     * @return
     */
private IListenerAction createChangeTypeAction() {
    return new IListenerAction() {

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

        public boolean performAction(Object prms) {
            FrameEditorUI.TypeChangeParameters p = (FrameEditorUI.TypeChangeParameters) prms;
            Iterator<IWidget> selected = p.selection.getSelectedWidgetsIterator();
            CompoundUndoableEdit editSequence = new CompoundUndoableEdit(CHG_WIDGET_TYPE, FrameEditorLID.ChangeTypeProperty);
            while (selected.hasNext()) {
                IWidget widget = selected.next();
                // Check if the widget types match
                // TODO: deal with the following
                // WidgetType.compatibleTransitions(oldWidgetType,
                //                                  p.newWidgetType)
                // if it returns false, show interaction..
                // auto delete transition?
                changeWidgetType(FrameEditorLID.ChangeTypeProperty, CHG_WIDGET_TYPE, widget, p.newWidgetType, editSequence);
            }
            editSequence.end();
            // Don't add empty edits!
            if (editSequence.isSignificant()) {
                undoMgr.addEdit(editSequence);
            }
            return true;
        }
    };
}
Also used : IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) FrameEditorUI(edu.cmu.cs.hcii.cogtool.ui.FrameEditorUI) IWidget(edu.cmu.cs.hcii.cogtool.model.IWidget)

Example 28 with CompoundUndoableEdit

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

the class FrameEditorController method spaceElementsEqually.

/**
     * Spaces the selected frame elements equally along a certain axis
     * @param vertical true for vertical axis; false for horizontal
     */
private boolean spaceElementsEqually(FrameEditorSelectionState selection, boolean vertical) {
    // Order the widgets according to location
    // (either from the left or from the top)
    Comparator<FrameElement> c = vertical ? elementVerticalComparator : elementHorizontalComparator;
    Set<FrameElement> elements = getSelectedElements(selection, c);
    if (elements.size() <= 2) {
        interaction.protestTooFewElements();
        return false;
    }
    // Calculate the spacing between widgets
    double sum = 0;
    double min = Double.MAX_VALUE;
    double max = Double.MIN_VALUE;
    // Go through each element that is selected
    // Determine the size, min & max of the region.
    // this can then be used to do spacing.
    Iterator<FrameElement> eltIter = elements.iterator();
    while (eltIter.hasNext()) {
        DoubleRectangle bounds = eltIter.next().getEltBounds();
        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) / (elements.size() - 1);
    String undoRedoLabel = vertical ? SPACE_VERTICALLY : SPACE_HORIZONTALLY;
    CogToolLID lid = vertical ? FrameEditorLID.SpaceVertically : FrameEditorLID.SpaceHorizontally;
    CompoundUndoableEdit editSequence = new CompoundUndoableEdit(undoRedoLabel, lid);
    // Avoid moving a group more than once
    Set<SimpleWidgetGroup> movedGroups = new HashSet<SimpleWidgetGroup>();
    Set<IWidget> movedWidgets = new HashSet<IWidget>();
    // Adjust the spacings to the correct values and go through
    // each element performing the appropriate move.
    eltIter = elements.iterator();
    while (eltIter.hasNext()) {
        FrameElement elt = eltIter.next();
        DoubleRectangle bounds = elt.getEltBounds();
        // Determine the amount to move each element
        double deltaX = vertical ? 0.0 : (min - bounds.x);
        double deltaY = vertical ? (min - bounds.y) : 0.0;
        // Set the new location, adding the undoable edit
        moveElement(lid, undoRedoLabel, elt, deltaX, deltaY, true, movedGroups, movedWidgets, editSequence);
        // Advance the pointer to the next location
        min += spacing + (vertical ? bounds.height : bounds.width);
    }
    editSequence.end();
    // Only add this edit if it is significant
    if (editSequence.isSignificant()) {
        undoMgr.addEdit(editSequence);
    }
    return true;
}
Also used : SimpleWidgetGroup(edu.cmu.cs.hcii.cogtool.model.SimpleWidgetGroup) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) DoubleRectangle(edu.cmu.cs.hcii.cogtool.model.DoubleRectangle) CogToolLID(edu.cmu.cs.hcii.cogtool.CogToolLID) FrameElement(edu.cmu.cs.hcii.cogtool.model.FrameElement) IWidget(edu.cmu.cs.hcii.cogtool.model.IWidget) HashSet(java.util.HashSet)

Example 29 with CompoundUndoableEdit

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

the class FrameEditorController method deleteElements.

/**
     * Delete currently selected widgets.
     */
protected boolean deleteElements(FrameEditorSelectionState selection) {
    String editLabel = (selection.getElementSelectionCount() > 1) ? DELETE_WIDGETS : DELETE_WIDGET;
    CompoundUndoableEdit editSequence = new CompoundUndoableEdit(editLabel, FrameEditorLID.Delete);
    FrameElement[] selectedElts = selection.getSelectedIFrameElements();
    // Check to see if any items are selected
    if (selectedElts.length > 0) {
        if (interaction.confirmDeleteElements(selectedElts)) {
            Set<IWidget> frameWidgets = model.getWidgets();
            Set<FrameElementGroup> frameAssocs = model.getEltGroups();
            for (FrameElement selectedElt : selectedElts) {
                if (selectedElt instanceof FrameElementGroup) {
                    // component of another group
                    if (frameAssocs.contains(selectedElt)) {
                        deleteFrameEltGroup((FrameElementGroup) selectedElt, null, editSequence);
                    }
                } else if (selectedElt instanceof IWidget) {
                    // of another widget (e.g., menu item part of a menu)
                    if (frameWidgets.contains(selectedElt)) {
                        deleteWidget((IWidget) selectedElt, true, null, editSequence);
                    }
                }
            }
            editSequence.end();
            undoMgr.addEdit(editSequence);
            return true;
        }
    } else {
        interaction.protestNoSelection();
    }
    return false;
}
Also used : FrameElementGroup(edu.cmu.cs.hcii.cogtool.model.FrameElementGroup) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) FrameElement(edu.cmu.cs.hcii.cogtool.model.FrameElement) IWidget(edu.cmu.cs.hcii.cogtool.model.IWidget)

Example 30 with CompoundUndoableEdit

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

the class FrameEditorController method captureImageAction.

// createChangeAuxTextPropertyAction
/**
     * Create a ListenerAction which will handle capturing a background image.
     * @return
     */
private IListenerAction captureImageAction() {
    return new IListenerAction() {

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

        public boolean performAction(Object prms) {
            CompoundUndoableEdit editSequence = new CompoundUndoableEdit(CAPTURE_BKG_IMG, FrameEditorLID.CaptureImageProperty);
            // Get the selection.
            FrameEditorSelectionState selection = (FrameEditorSelectionState) prms;
            Iterator<IWidget> selected = selection.getSelectedWidgetsIterator();
            // Iterate over every selected widget
            while (selected.hasNext()) {
                final IWidget w = selected.next();
                DoubleRectangle bounds = w.getEltBounds();
                // Get the image from the background, and crop to shape
                final byte[] bg = GraphicsUtil.cropImage(model.getBackgroundImage(), bounds.x, bounds.y, bounds.width, bounds.height);
                // Get the old image, could be null.
                final byte[] old = w.getImage();
                final String previousImagePath = (String) w.getAttribute(WidgetAttributes.IMAGE_PATH_ATTR);
                w.setImage(bg);
                w.setAttribute(WidgetAttributes.IMAGE_PATH_ATTR, WidgetAttributes.NO_IMAGE);
                editSequence.addEdit(new AUndoableEdit(FrameEditorLID.CaptureImageProperty) {

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

                    @Override
                    public void redo() {
                        super.redo();
                        w.setImage(bg);
                        w.setAttribute(WidgetAttributes.IMAGE_PATH_ATTR, WidgetAttributes.NO_IMAGE);
                    }

                    @Override
                    public void undo() {
                        super.undo();
                        w.setImage(old);
                        w.setAttribute(WidgetAttributes.IMAGE_PATH_ATTR, previousImagePath);
                    }
                });
            }
            editSequence.end();
            // Only add this edit if it is significant
            if (editSequence.isSignificant()) {
                undoMgr.addEdit(editSequence);
            }
            return true;
        }
    };
}
Also used : IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) FrameEditorSelectionState(edu.cmu.cs.hcii.cogtool.ui.FrameEditorSelectionState) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) DoubleRectangle(edu.cmu.cs.hcii.cogtool.model.DoubleRectangle) IWidget(edu.cmu.cs.hcii.cogtool.model.IWidget)

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