Search in sources :

Example 1 with ReadOnlyList

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

the class FrameEditorController method duplicateWidgetsAction.

// duplicateFrameEltGroup
private IListenerAction duplicateWidgetsAction() {
    return new IListenerAction() {

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

        public boolean performAction(Object prms) {
            FrameEditorUI.DuplicateParameters parameters = (FrameEditorUI.DuplicateParameters) prms;
            int elementCount = parameters.selection.getElementSelectionCount();
            // If selection is non zero, copy widgets
            if (elementCount > 0) {
                Map<IWidget, IWidget> widgetCopies = new LinkedHashMap<IWidget, IWidget>();
                final Map<FrameElementGroup, FrameElementGroup> groupCopies = new LinkedHashMap<FrameElementGroup, FrameElementGroup>();
                widgetSituator.reset(widgetCopies, groupCopies);
                Set<FrameElement> selectedElements = getSelectedElements(parameters.selection, elementLevelComparator);
                Iterator<FrameElement> elements = selectedElements.iterator();
                // Iterate through the widgets and duplicate.
                while (elements.hasNext()) {
                    FrameElement elt = elements.next();
                    if (elt instanceof IWidget) {
                        duplicateWidget((IWidget) elt, parameters.selection, parameters.moveByX, parameters.moveByY);
                    } else if (elt instanceof FrameElementGroup) {
                        duplicateFrameEltGroup((FrameElementGroup) elt, parameters.moveByX, parameters.moveByY);
                    }
                }
                widgetSituator.completeWork();
                final Iterable<? extends IWidget> widgetDuplicates = new ReadOnlyList<IWidget>(new ArrayList<IWidget>(widgetCopies.values()));
                Iterator<? extends IWidget> copiedWidgets = widgetDuplicates.iterator();
                Set<GridButtonGroup> dupGridGroups = new HashSet<GridButtonGroup>();
                while (copiedWidgets.hasNext()) {
                    IWidget widgetCopy = copiedWidgets.next();
                    // Warning: it is important that each widget be added
                    // to the frame *before* we make the next widget name
                    // unique or we can end up with non-unique names.
                    makeWidgetNameUnique(widgetCopy);
                    model.addWidget(widgetCopy);
                    if (widgetCopy instanceof GridButton) {
                        GridButtonGroup gbg = (GridButtonGroup) widgetCopy.getParentGroup();
                        // for each grid button group
                        if (!dupGridGroups.contains(gbg)) {
                            gbg.recalculateOffsets();
                            dupGridGroups.add(gbg);
                        }
                    }
                }
                Iterator<FrameElementGroup> copiedGroups = groupCopies.values().iterator();
                while (copiedGroups.hasNext()) {
                    FrameElementGroup copiedGroup = copiedGroups.next();
                    // Ensure name is unique, then add to frame immediately
                    // Otherwise, it would be possible to generate presumed
                    // unique names that weren't.
                    makeEltGroupNameUnique(copiedGroup);
                    model.addEltGroup(copiedGroup);
                }
                DemoStateManager.IDesignUndoableEdit edit = new DemoStateManager.InvalidatingEdit(FrameEditorLID.Duplicate, demoStateMgr) {

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

                    @Override
                    public void redo() {
                        super.redo();
                        Iterator<? extends IWidget> addCopies = widgetDuplicates.iterator();
                        while (addCopies.hasNext()) {
                            IWidget widgetCopy = addCopies.next();
                            model.addWidget(widgetCopy);
                        }
                        Iterator<FrameElementGroup> copiedGroups = groupCopies.values().iterator();
                        while (copiedGroups.hasNext()) {
                            model.addEltGroup(copiedGroups.next());
                        }
                        demoStateMgr.noteWidgetsEdit(widgetDuplicates, this);
                    }

                    @Override
                    public void undo() {
                        super.undo();
                        Iterator<? extends IWidget> removeCopies = widgetDuplicates.iterator();
                        while (removeCopies.hasNext()) {
                            IWidget widgetCopy = removeCopies.next();
                            model.removeWidget(widgetCopy);
                        }
                        Iterator<FrameElementGroup> copiedGroups = groupCopies.values().iterator();
                        while (copiedGroups.hasNext()) {
                            model.removeEltGroup(copiedGroups.next());
                        }
                        demoStateMgr.noteWidgetsEdit(widgetDuplicates, this);
                    }
                };
                demoStateMgr.noteWidgetsEdit(widgetDuplicates, edit);
                undoMgr.addEdit(edit);
                return true;
            }
            // tell user to select something.
            interaction.protestNoSelection();
            return false;
        }
    };
}
Also used : IDesignUndoableEdit(edu.cmu.cs.hcii.cogtool.controller.DemoStateManager.IDesignUndoableEdit) LinkedHashMap(java.util.LinkedHashMap) GridButton(edu.cmu.cs.hcii.cogtool.model.GridButton) IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) HashSet(java.util.HashSet) GridButtonGroup(edu.cmu.cs.hcii.cogtool.model.GridButtonGroup) FrameElementGroup(edu.cmu.cs.hcii.cogtool.model.FrameElementGroup) FrameEditorUI(edu.cmu.cs.hcii.cogtool.ui.FrameEditorUI) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint) ReadOnlyList(edu.cmu.cs.hcii.cogtool.util.ReadOnlyList) FrameElement(edu.cmu.cs.hcii.cogtool.model.FrameElement) IWidget(edu.cmu.cs.hcii.cogtool.model.IWidget)

Example 2 with ReadOnlyList

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

the class FrameEditorController method moveWidgetGroup.

// moveWidget
private void moveWidgetGroup(ListenerIdentifier lid, final String presentationLabel, final SimpleWidgetGroup group, double widgetMoveByX, double widgetMoveByY, IUndoableEditSequence editSequence) {
    // Keep track of old and new locations, ensuring not less than zero
    IWidget firstChildWidget = group.get(0);
    DoublePoint oldLocation = firstChildWidget.getShape().getOrigin();
    final double deltaX = Math.max(widgetMoveByX, -oldLocation.x);
    final double deltaY = Math.max(widgetMoveByY, -oldLocation.y);
    final int numWidgets = group.size();
    List<IWidget> groupWidgets = new ArrayList<IWidget>();
    // Change model and create undo.
    for (int i = 0; i < numWidgets; i++) {
        IWidget widget = group.get(i);
        groupWidgets.add(widget);
        widget.moveElement(deltaX, deltaY);
    }
    final ReadOnlyList<? extends IWidget> roGroupWidgets = new ReadOnlyList<IWidget>(groupWidgets);
    DemoStateManager.ObsoletingEdit edit = new DemoStateManager.ObsoletingEdit(lid, demoStateMgr) {

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

        @Override
        public void redo() {
            super.redo();
            for (int i = 0; i < numWidgets; i++) {
                IWidget w = roGroupWidgets.get(i);
                w.moveElement(deltaX, deltaY);
            }
            noteEditCheckRegenerate(roGroupWidgets, this);
        }

        @Override
        public void undo() {
            super.undo();
            for (int i = 0; i < numWidgets; i++) {
                IWidget w = roGroupWidgets.get(i);
                w.moveElement(-deltaX, -deltaY);
            }
            noteEditCheckRegenerate(roGroupWidgets, this);
        }
    };
    noteEditCheckRegenerate(roGroupWidgets, edit);
    editSequence.addEdit(edit);
}
Also used : ReadOnlyList(edu.cmu.cs.hcii.cogtool.util.ReadOnlyList) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint) ArrayList(java.util.ArrayList) IWidget(edu.cmu.cs.hcii.cogtool.model.IWidget) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint)

Aggregations

DoublePoint (edu.cmu.cs.hcii.cogtool.model.DoublePoint)2 IWidget (edu.cmu.cs.hcii.cogtool.model.IWidget)2 ReadOnlyList (edu.cmu.cs.hcii.cogtool.util.ReadOnlyList)2 IDesignUndoableEdit (edu.cmu.cs.hcii.cogtool.controller.DemoStateManager.IDesignUndoableEdit)1 FrameElement (edu.cmu.cs.hcii.cogtool.model.FrameElement)1 FrameElementGroup (edu.cmu.cs.hcii.cogtool.model.FrameElementGroup)1 GridButton (edu.cmu.cs.hcii.cogtool.model.GridButton)1 GridButtonGroup (edu.cmu.cs.hcii.cogtool.model.GridButtonGroup)1 FrameEditorUI (edu.cmu.cs.hcii.cogtool.ui.FrameEditorUI)1 IListenerAction (edu.cmu.cs.hcii.cogtool.util.IListenerAction)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1