Search in sources :

Example 11 with FrameElementGroup

use of edu.cmu.cs.hcii.cogtool.model.FrameElementGroup in project cogtool by cogtool.

the class FrameEditorController method getSelectedWidgets.

/**
     * Fetch all widgets referenced by the given selection sorted by
     * the given comparison function.
     */
private IWidget[] getSelectedWidgets(FrameEditorSelectionState seln, Comparator<? super IWidget> c) {
    IWidget[] widgets;
    Set<IWidget> allWidgets = new HashSet<IWidget>();
    Iterator<FrameElement> s = seln.getSelectedElementsIterator();
    while (s.hasNext()) {
        FrameElement elt = s.next();
        if (elt instanceof IWidget) {
            IWidget w = (IWidget) elt;
            SimpleWidgetGroup group = w.getParentGroup();
            if (group != null) {
                Iterator<IWidget> mbrs = group.iterator();
                while (mbrs.hasNext()) {
                    allWidgets.add(mbrs.next());
                }
            } else {
                allWidgets.add(w);
            }
        } else if (elt instanceof FrameElementGroup) {
            Iterator<IWidget> eltGrpWidgets = new ElementAllWidgetIterator((FrameElementGroup) elt);
            while (eltGrpWidgets.hasNext()) {
                allWidgets.add(eltGrpWidgets.next());
            }
        }
    }
    widgets = new IWidget[allWidgets.size()];
    allWidgets.toArray(widgets);
    Arrays.sort(widgets, c);
    return widgets;
}
Also used : SimpleWidgetGroup(edu.cmu.cs.hcii.cogtool.model.SimpleWidgetGroup) EmptyIterator(edu.cmu.cs.hcii.cogtool.util.EmptyIterator) Iterator(java.util.Iterator) FrameElementGroup(edu.cmu.cs.hcii.cogtool.model.FrameElementGroup) FrameElement(edu.cmu.cs.hcii.cogtool.model.FrameElement) IWidget(edu.cmu.cs.hcii.cogtool.model.IWidget) HashSet(java.util.HashSet)

Example 12 with FrameElementGroup

use of edu.cmu.cs.hcii.cogtool.model.FrameElementGroup 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 13 with FrameElementGroup

use of edu.cmu.cs.hcii.cogtool.model.FrameElementGroup 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 14 with FrameElementGroup

use of edu.cmu.cs.hcii.cogtool.model.FrameElementGroup in project cogtool by cogtool.

the class FrameEditorController method renameEltGroup.

private boolean renameEltGroup(final FrameElementGroup eltGroup, String tryName) {
    final String oldName = eltGroup.getName();
    boolean notDone = true;
    do {
        if (tryName.length() == 0) {
            tryName = interaction.protestNameCannotBeEmpty(DEFAULT_GROUP_PREFIX);
            // If canceled, indicate so; otherwise, test again
            if (tryName == null) {
                return false;
            }
        } else {
            FrameElementGroup groupForName = model.getEltGroup(tryName);
            // then no change is necessary!
            if (groupForName == eltGroup) {
                notDone = false;
            } else if (groupForName != null) {
                // A non-null widget for the tryName indicates a collision
                tryName = interaction.protestNameCollision(DEFAULT_GROUP_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;
                model.setEltGroupName(newName, eltGroup);
                IUndoableEdit edit = new AUndoableEdit(FrameEditorLID.ChangeNameProperty) {

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

                    @Override
                    public void redo() {
                        super.redo();
                        model.setEltGroupName(newName, eltGroup);
                    }

                    @Override
                    public void undo() {
                        super.undo();
                        model.setEltGroupName(oldName, eltGroup);
                    }
                };
                undoMgr.addEdit(edit);
            }
        }
    } while (notDone);
    return true;
}
Also used : AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) FrameElementGroup(edu.cmu.cs.hcii.cogtool.model.FrameElementGroup) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit)

Example 15 with FrameElementGroup

use of edu.cmu.cs.hcii.cogtool.model.FrameElementGroup in project cogtool by cogtool.

the class FrameEditorController method resizeElement.

// deleteWidget
private void resizeElement(FrameElement elt, double oldResizeX, double oldResizeY, double newResizeX, double newResizeY, double ratioX, double ratioY, Set<SimpleWidgetGroup> resizedGroups, boolean childrenToo, CompoundUndoableEdit editSequence) {
    if (elt instanceof IWidget) {
        IWidget widget = (IWidget) elt;
        SimpleWidgetGroup group = widget.getParentGroup();
        if ((widget instanceof TraversableWidget) && (group != null)) {
            if (!resizedGroups.contains(group)) {
                resizeGroup(group, oldResizeX, oldResizeY, newResizeX, newResizeY, ratioX, ratioY, childrenToo, editSequence);
                resizedGroups.add(group);
            }
        } else {
            // Resize the actual widget.
            resizeWidget(widget, oldResizeX, oldResizeY, newResizeX, newResizeY, ratioX, ratioY, childrenToo, editSequence);
        }
    } else if (elt instanceof SimpleWidgetGroup) {
        Iterator<IWidget> widgets = ((SimpleWidgetGroup) elt).iterator();
        while (widgets.hasNext()) {
            resizeElement(widgets.next(), oldResizeX, oldResizeY, newResizeX, newResizeY, ratioX, ratioY, resizedGroups, childrenToo, editSequence);
        }
    } else if (elt instanceof FrameElementGroup) {
        Iterator<FrameElement> members = ((FrameElementGroup) elt).iterator();
        while (members.hasNext()) {
            resizeElement(members.next(), oldResizeX, oldResizeY, newResizeX, newResizeY, ratioX, ratioY, resizedGroups, true, editSequence);
        }
    }
}
Also used : TraversableWidget(edu.cmu.cs.hcii.cogtool.model.TraversableWidget) SimpleWidgetGroup(edu.cmu.cs.hcii.cogtool.model.SimpleWidgetGroup) EmptyIterator(edu.cmu.cs.hcii.cogtool.util.EmptyIterator) Iterator(java.util.Iterator) FrameElementGroup(edu.cmu.cs.hcii.cogtool.model.FrameElementGroup) FrameElement(edu.cmu.cs.hcii.cogtool.model.FrameElement) IWidget(edu.cmu.cs.hcii.cogtool.model.IWidget)

Aggregations

FrameElementGroup (edu.cmu.cs.hcii.cogtool.model.FrameElementGroup)22 IWidget (edu.cmu.cs.hcii.cogtool.model.IWidget)15 FrameElement (edu.cmu.cs.hcii.cogtool.model.FrameElement)14 SimpleWidgetGroup (edu.cmu.cs.hcii.cogtool.model.SimpleWidgetGroup)10 DoublePoint (edu.cmu.cs.hcii.cogtool.model.DoublePoint)8 HashSet (java.util.HashSet)6 IUndoableEdit (edu.cmu.cs.hcii.cogtool.util.IUndoableEdit)5 AUndoableEdit (edu.cmu.cs.hcii.cogtool.util.AUndoableEdit)4 Iterator (java.util.Iterator)4 EmptyIterator (edu.cmu.cs.hcii.cogtool.util.EmptyIterator)3 IListenerAction (edu.cmu.cs.hcii.cogtool.util.IListenerAction)3 IDesignUndoableEdit (edu.cmu.cs.hcii.cogtool.controller.DemoStateManager.IDesignUndoableEdit)2 DoubleRectangle (edu.cmu.cs.hcii.cogtool.model.DoubleRectangle)2 GridButton (edu.cmu.cs.hcii.cogtool.model.GridButton)2 GridButtonGroup (edu.cmu.cs.hcii.cogtool.model.GridButtonGroup)2 FrameEditorSelectionState (edu.cmu.cs.hcii.cogtool.ui.FrameEditorSelectionState)2 FrameEditorUI (edu.cmu.cs.hcii.cogtool.ui.FrameEditorUI)2 CompoundUndoableEdit (edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit)2 Point (org.eclipse.draw2d.geometry.Point)2 Text (org.eclipse.swt.widgets.Text)2